KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > utilities > hibernate > SharkHibernateUtilityTransaction


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

4
5 package org.enhydra.shark.utilities.hibernate;
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
13 /**
14  * Implementation of UserTransaction interface that creates Hibernate
15  * transaction.
16  *
17  * @author Vladislav Pernin
18  */

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