KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > engine > TransactionHelper


1 //$Id: TransactionHelper.java,v 1.6 2005/06/22 04:19:31 oneovthafew Exp $
2
package org.hibernate.engine;
3
4 import org.apache.commons.logging.Log;
5 import org.apache.commons.logging.LogFactory;
6 import org.hibernate.HibernateException;
7 import org.hibernate.exception.JDBCExceptionHelper;
8
9 import javax.transaction.SystemException JavaDoc;
10 import javax.transaction.Transaction JavaDoc;
11 import javax.transaction.TransactionManager JavaDoc;
12 import java.io.Serializable JavaDoc;
13 import java.sql.Connection JavaDoc;
14 import java.sql.SQLException JavaDoc;
15
16 /**
17  * Allows work to be done outside the current transaction, by suspending it,
18  * and performing work in a new transaction
19  *
20  * @author Emmanuel Bernard
21  */

22 public abstract class TransactionHelper {
23     private static final Log log = LogFactory.getLog( TransactionHelper.class );
24     
25     /**
26      * The work to be done
27      */

28     protected abstract Serializable JavaDoc doWorkInCurrentTransaction(Connection JavaDoc conn, String JavaDoc sql) throws SQLException JavaDoc;
29
30     /**
31      * Suspend the current transaction and perform work in a new transaction
32      */

33     public Serializable JavaDoc doWorkInNewTransaction(SessionImplementor session)
34     throws HibernateException {
35     
36         // This has to be done using a different connection to the
37
// containing transaction because the new hi value must
38
// remain valid even if the containing transaction rolls
39
// back
40
TransactionManager JavaDoc tm = session.getFactory().getTransactionManager();
41         Transaction JavaDoc surroundingTransaction = null; // for resuming in finally block
42
Connection JavaDoc conn = null; // for ressource cleanup
43
String JavaDoc sql = null; // for exception
44
boolean isJta = tm != null;
45         boolean catchedException = false;
46         try {
47             if ( isJta ) {
48                 //JTA environment
49
// prepare a new transaction context for the generator
50
surroundingTransaction = tm.suspend();
51                 if ( log.isDebugEnabled() ) {
52                     log.debug( "surrounding tx suspended" );
53                 }
54                 tm.begin();
55                 // get connection from managed environment
56
conn = session.getBatcher().openConnection();
57             }
58             else {
59                 // get connection from the hibernate defined pool
60
conn = session.getBatcher().openConnection();
61                 if ( conn.getAutoCommit() ) conn.setAutoCommit( false );
62             }
63
64             Serializable JavaDoc result = doWorkInCurrentTransaction( conn, sql );
65
66             // commit transaction to ensure updated sequence is not rolled back
67
if ( isJta ) {
68                 tm.commit();
69             }
70             else {
71                 conn.commit();
72             }
73             return result;
74         }
75         catch ( SQLException JavaDoc sqle ) {
76             catchedException = true;
77             if ( isJta ) {
78                 try {
79                     tm.rollback();
80                 }
81                 catch( Throwable JavaDoc t ) {
82                     //clean as much as we can
83
}
84             }
85             throw JDBCExceptionHelper.convert(
86                     session.getFactory().getSQLExceptionConverter(),
87                     sqle,
88                     "could not get or update next value",
89                     sql
90                 );
91         }
92         catch ( Exception JavaDoc e ) {
93             catchedException = true;
94             if ( isJta ) {
95                 try {
96                     tm.rollback();
97                     throw new HibernateException( e );
98                 }
99                 catch ( SystemException JavaDoc e1 ) {
100                     throw new HibernateException( e1 );
101                 }
102             }
103             else {
104                 throw new HibernateException( e );
105             }
106         }
107         finally {
108             if ( isJta ) {
109                 try {
110                     session.getBatcher().closeConnection( conn );
111                 }
112                 catch ( Exception JavaDoc e ) {
113                     //swallow it to put JTA in a proper context
114
}
115                 // switch back to surrounding transaction context
116
if ( isJta && surroundingTransaction != null ) {
117                     try {
118                         tm.resume( surroundingTransaction );
119                         if ( log.isDebugEnabled() ) {
120                             log.debug( "surrounding tx resumed" );
121                         }
122                     }
123                     catch ( Exception JavaDoc e ) {
124                         //do not hide a previous exception
125
if ( ! catchedException ) throw new HibernateException( e );
126                     }
127                 }
128             }
129             else {
130                 session.getBatcher().closeConnection( conn );
131             }
132         }
133     }
134 }
135
Popular Tags