KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > usertransaction > SharkHibernateUserTransaction


1 /*
2  * Shark Hibernate UserTransaction - Open Wide
3  */

4
5 package org.enhydra.shark.usertransaction;
6
7 import net.sf.hibernate.HibernateException;
8 import net.sf.hibernate.Session;
9
10 import org.enhydra.shark.api.TransactionException;
11 import org.enhydra.shark.api.UserTransaction;
12
13 /**
14  * Implementation of UserTransaction interface that creates Hibernate
15  * transaction.
16  * @author Vladislav Pernin
17  */

18 public class SharkHibernateUserTransaction implements UserTransaction {
19
20     private static int noOfCreations = 0;
21     private static int noOfCommits = 0;
22     private static int noOfRollbacks = 0;
23     private static int noOfReleases = 0;
24
25     private net.sf.hibernate.Transaction transactionDB;
26
27     private Session session;
28
29     public SharkHibernateUserTransaction(net.sf.hibernate.Transaction transaction) {
30         if (HibernateUserTransactionFactory._debug_) {
31             synchronized (SharkHibernateUserTransaction.class) {
32                 noOfCreations++;
33                 System.out.println("CREATING User T No" + noOfCreations);
34             }
35         }
36         this.transactionDB = transaction;
37     }
38
39     public net.sf.hibernate.Transaction getHibernateTransaction() {
40         return transactionDB;
41     }
42
43     public Session getSession() throws HibernateException {
44         if (transactionDB != null) {
45             return ThreadLocalSession.currentSession();
46         } else {
47             System.err.println("BE CAREFUL, YOU ARE TRYING TO USE AN ALREADY COMMITED OR ROLLBACKED TRANSACTION!");
48             return null;
49         }
50     }
51
52     public void commit() throws TransactionException {
53         if (HibernateUserTransactionFactory._debug_) {
54             synchronized (SharkHibernateUserTransaction.class) {
55                 System.out.println("COMMITING User T ");
56             }
57         }
58         try {
59             if (transactionDB != null) {
60                 transactionDB.commit();
61                 ThreadLocalSession.closeSession();
62                 getSession().close();
63                 transactionDB = null;
64             }
65
66             if (HibernateUserTransactionFactory._debug_) {
67                 synchronized (SharkHibernateUserTransaction.class) {
68                     noOfCommits++;
69                     System.out.println("COMMITED User T No" + noOfCommits);
70                 }
71             }
72         } catch (Exception JavaDoc ex) {
73             throw new TransactionException(ex);
74         }
75     }
76
77     public void rollback() throws TransactionException {
78         try {
79             if (transactionDB != null) {
80                 transactionDB.rollback();
81                 ThreadLocalSession.closeSession();
82                 transactionDB = null;
83             }
84
85             if (HibernateUserTransactionFactory._debug_) {
86                 synchronized (SharkHibernateUserTransaction.class) {
87                     noOfRollbacks++;
88                     System.out.println("ROLLING BACK User T" + noOfRollbacks);
89                 }
90             }
91
92         } catch (Exception JavaDoc ex) {
93             if (HibernateUserTransactionFactory._debug_) {
94                 System.out.println("ROLLING BACK FAILED");
95             }
96             throw new TransactionException(ex);
97         }
98     }
99
100     public void release() throws TransactionException {
101         try {
102             transactionDB = null;
103             ThreadLocalSession.closeSession();
104             if (HibernateUserTransactionFactory._debug_) {
105                 synchronized (SharkHibernateUserTransaction.class) {
106                     noOfReleases++;
107                     System.out.println("RELEASE User T " + noOfReleases);
108                 }
109             }
110         } catch (Exception JavaDoc ex) {
111             if (HibernateUserTransactionFactory._debug_) {
112                 System.out.println("RELEASE User T FAILED");
113             }
114             throw new TransactionException(ex);
115         }
116     }
117
118     public static synchronized void info() {
119         if (noOfCommits + noOfRollbacks + noOfReleases != noOfCreations) {
120             System.err.println("PANIC!!!\nI've lost user transaction counts.");
121         }
122         System.err.println("UTCRE=" + noOfCreations + ", UTCOMM=" + noOfCommits + ", UTROLL=" + noOfRollbacks + ", UTREL=" + noOfReleases);
123     }
124 }
125
Popular Tags