KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > repositorypersistence > HibernateRepositoryTransaction


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

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

17 public class HibernateRepositoryTransaction implements RepositoryTransaction
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 HibernateRepositoryTransaction(
30         net.sf.hibernate.Transaction transaction) {
31         if (HibernateRepositoryPersistenceManager._debug_) {
32             synchronized (HibernateRepositoryTransaction.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 (HibernateRepositoryPersistenceManager._debug_) {
46             synchronized (HibernateRepositoryTransaction.class) {
47                 System.out.println("COMMITING T ");
48             }
49         }
50         try {
51             transactionDB.commit();
52             ThreadLocalSession.closeSession();
53             transactionDB = null;
54             if (HibernateRepositoryPersistenceManager._debug_) {
55                 synchronized (HibernateRepositoryTransaction.class) {
56                     noOfCommits++;
57                     System.out.println("COMMITED T No" + noOfCommits);
58                 }
59             }
60         }
61         /*catch (JDBCException et) {
62             et.printStackTrace();
63             SQLException ext = et.getSQLException();
64             while (ext != null) {
65                 System.out.println("next" + ext);
66                 ext = ext.getNextException();
67             }
68             System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!PROBLEM COMMITING!!!!!!!!!!!!!!!!!!!!!");
69         }*/

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