KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > transaction > SharkHibernateTransaction


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

4
5 package org.enhydra.shark.transaction;
6
7 import net.sf.hibernate.HibernateException;
8 import net.sf.hibernate.Session;
9
10 import org.enhydra.shark.api.SharkTransaction;
11 import org.enhydra.shark.api.TransactionException;
12 import org.enhydra.shark.utilities.hibernate.Buffer;
13
14 /**
15  * Class used to handle transaction relative operation like commit, rollback or
16  * release.
17  * @author Vladislav Pernin
18  */

19 public class SharkHibernateTransaction extends Buffer implements SharkTransaction {
20
21     private static int noOfCreations = 0;
22     private static int noOfCommits = 0;
23     private static int noOfRollbacks = 0;
24     private static int noOfReleases = 0;
25
26     public SharkHibernateTransaction(net.sf.hibernate.Transaction transaction) throws TransactionException {
27         super(transaction);
28         try {
29             super.setSession(getSession());
30         } catch (Exception JavaDoc e) {
31             throw new TransactionException(e);
32         }
33         if (HibernateTransactionFactory._debug_) {
34             synchronized (SharkHibernateTransaction.class) {
35                 noOfCreations++;
36                 System.out.println("CREATING T No" + noOfCreations);
37             }
38         }
39     }
40
41     public net.sf.hibernate.Transaction getHibernateTransaction() {
42         return transaction;
43     }
44
45     public void setDeleteFinishedProcesses(boolean deleteProcesses) {
46         deleteFinishedProcesses = deleteProcesses;
47     }
48
49     public void commit() throws TransactionException {
50         if (HibernateTransactionFactory._debug_) {
51             synchronized (SharkHibernateTransaction.class) {
52                 System.out.println("COMMITING T ");
53             }
54         }
55         try {
56             if (transaction != null) {
57                 write();
58                 transaction.commit();
59                 ThreadLocalSession.closeSession();
60                 super.setSession(null);
61                 transaction = null;
62             }
63
64             if (HibernateTransactionFactory._debug_) {
65                 synchronized (SharkHibernateTransaction.class) {
66                     noOfCommits++;
67                     System.out.println("COMMITED T No" + noOfCommits);
68                 }
69             }
70         }
71         /*
72          * catch (JDBCException et) { et.printStackTrace(); SQLException ext =
73          * et.getSQLException(); while (ext != null) {
74          * System.out.println("next" + ext); ext = ext.getNextException(); }
75          * System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!PROBLEM
76          * COMMITING!!!!!!!!!!!!!!!!!!!!!");
77          */

78         catch (Throwable JavaDoc ex) {
79             ex.printStackTrace();
80             throw new TransactionException(ex);
81         }
82     }
83
84     public void rollback() throws TransactionException {
85         try {
86             if (transaction != null) {
87                 clear();
88                 transaction.rollback();
89                 ThreadLocalSession.closeSession();
90                 super.setSession(null);
91                 transaction = null;
92             }
93
94             if (HibernateTransactionFactory._debug_) {
95                 synchronized (SharkHibernateTransaction.class) {
96                     noOfRollbacks++;
97                     System.out.println("ROLLING BACK T " + noOfRollbacks);
98                 }
99             }
100         } catch (Exception JavaDoc ex) {
101             if (HibernateTransactionFactory._debug_) {
102                 System.out.println("ROLLING BACK FAILED");
103             }
104             throw new TransactionException(ex);
105         }
106     }
107
108     public static synchronized void info() {
109         if (noOfCommits + noOfRollbacks != noOfCreations) {
110             System.err.println("PANIC!!!\nI've lost transcaton counts.");
111         }
112         System.err.println("CRE=" + noOfCreations + ", COMM=" + noOfCommits + ", ROLL=" + noOfRollbacks);
113     }
114
115     public Session getSession() throws HibernateException {
116         if (transaction != null) {
117             return ThreadLocalSession.currentSession();
118         } else {
119             System.err.println("BE CAREFUL, YOU ARE TRYING TO USE AN ALREADY COMMITED OR ROLLBACKED TRANSACTION!");
120             return null;
121         }
122     }
123
124     public void release() throws TransactionException {
125         try {
126             super.setSession(null);
127             ThreadLocalSession.closeSession();
128             transaction = null;
129
130             if (HibernateTransactionFactory._debug_) {
131                 synchronized (SharkHibernateTransaction.class) {
132                     noOfReleases++;
133                     System.out.println("RELEASE T " + noOfReleases);
134                 }
135             }
136         } catch (Exception JavaDoc ex) {
137             if (HibernateTransactionFactory._debug_) {
138                 System.out.println("RELEASE FAILED");
139             }
140             throw new TransactionException(ex);
141         }
142     }
143
144 }
145
Popular Tags