KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > partmappersistence > HibernateParticipantMappingTransaction


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

4
5 package org.enhydra.shark.partmappersistence;
6
7 import java.sql.SQLException JavaDoc;
8
9 import net.sf.hibernate.HibernateException;
10 import net.sf.hibernate.JDBCException;
11 import net.sf.hibernate.Session;
12
13 import org.enhydra.shark.api.ParticipantMappingTransaction;
14 import org.enhydra.shark.api.TransactionException;
15
16 /**
17  * Implementation of ParticipantMappingTransaction interface
18  * @author Vladislav Pernin
19  */

20 public class HibernateParticipantMappingTransaction implements ParticipantMappingTransaction
21 {
22  
23     private static int noOfCreations = 0;
24     private static int noOfCommits = 0;
25     private static int noOfRollbacks = 0;
26     private static int noOfReleases=0;
27
28     private net.sf.hibernate.Transaction transactionDB;
29
30     private Session session;
31
32     public HibernateParticipantMappingTransaction(
33         net.sf.hibernate.Transaction transaction) {
34         if (HibernateParticipantMappingMgr._debug_) {
35             synchronized (HibernateParticipantMappingTransaction.class) {
36                 noOfCreations++;
37                 System.out.println("CREATING T No" + noOfCreations);
38             }
39         }
40         this.transactionDB = transaction;
41     }
42
43     public net.sf.hibernate.Transaction getHibernateTransaction() {
44         return transactionDB;
45     }
46
47     public void commit() throws TransactionException {
48         if (HibernateParticipantMappingMgr._debug_) {
49             synchronized (HibernateParticipantMappingTransaction.class) {
50                 System.out.println("COMMITING T ");
51             }
52         }
53         try {
54             transactionDB.commit();
55             ThreadLocalSession.closeSession();
56             transactionDB = null;
57             if (HibernateParticipantMappingMgr._debug_) {
58                 synchronized (HibernateParticipantMappingTransaction.class) {
59                     noOfCommits++;
60                     System.out.println("COMMITED T No" + noOfCommits);
61                 }
62             }
63         }
64         /*catch (JDBCException et) {
65             et.printStackTrace();
66             SQLException ext =et.getSQLException();
67             while (ext != null) {
68                 System.out.println("next" + ext); ext = ext.getNextException(); }
69             System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!PROBLEM COMMITING!!!!!!!!!!!!!!!!!!!!!");
70             }*/

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