KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > appmappersistence > HibernateApplicationMappingTransaction


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

4
5 package org.enhydra.shark.appmappersistence;
6
7 import net.sf.hibernate.HibernateException;
8 import net.sf.hibernate.Session;
9
10 import org.enhydra.shark.api.ApplicationMappingTransaction;
11 import org.enhydra.shark.api.TransactionException;
12
13 /**
14  * Implementation of ParticipantMappingTransaction interface
15  * @author Vladislav Pernin
16  */

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