1 package org.hibernate.jdbc; 3 4 import java.io.Serializable ; 5 import java.sql.Connection ; 6 import java.sql.SQLException ; 7 import javax.transaction.TransactionManager ; 8 9 import org.apache.commons.logging.Log; 10 import org.apache.commons.logging.LogFactory; 11 import org.hibernate.ConnectionReleaseMode; 12 import org.hibernate.HibernateException; 13 import org.hibernate.SessionException; 14 import org.hibernate.Transaction; 15 import org.hibernate.TransactionException; 16 import org.hibernate.engine.SessionFactoryImplementor; 17 import org.hibernate.exception.JDBCExceptionHelper; 18 import org.hibernate.transaction.CacheSynchronization; 19 import org.hibernate.transaction.TransactionFactory; 20 import org.hibernate.util.JTAHelper; 21 22 28 public class JDBCContext implements Serializable , ConnectionManager.Callback { 29 30 38 private static final Log log = LogFactory.getLog( JDBCContext.class ); 39 40 public static interface Context extends TransactionFactory.Context { 41 public void beforeTransactionCompletion(org.hibernate.Transaction tx); 42 public void afterTransactionCompletion(boolean success, org.hibernate.Transaction tx); 43 public ConnectionReleaseMode getConnectionReleaseMode(); 44 public boolean isAutoCloseSessionEnabled(); 45 } 46 47 private Context owner; 48 private ConnectionManager connectionManager; 49 private boolean isTransactionCallbackRegistered; 50 private boolean isHibernateTransactionActive; 51 52 public JDBCContext(Context owner, Connection connection) { 53 this.owner = owner; 54 this.connectionManager = new ConnectionManager( 55 owner.getFactory(), 56 this, 57 owner.getConnectionReleaseMode(), 58 connection 59 ); 60 61 final boolean registerSynchronization = owner.isAutoCloseSessionEnabled() 62 || owner.isFlushBeforeCompletionEnabled() 63 || owner.getConnectionReleaseMode() == ConnectionReleaseMode.AFTER_TRANSACTION; 64 if ( registerSynchronization ) { 65 registerSynchronizationIfPossible(); 66 } 67 } 68 69 70 72 public void connectionOpened() { 73 if ( !isTransactionCallbackRegistered ) { 74 registerSynchronizationIfPossible(); 80 } 81 82 if ( owner.getFactory().getStatistics().isStatisticsEnabled() ) { 83 owner.getFactory().getStatisticsImplementor().connect(); 84 } 85 } 86 87 public void connectionCleanedUp() { 88 if ( !isTransactionCallbackRegistered ) { 89 afterTransactionCompletion( false, null ); 90 } 92 } 93 94 public SessionFactoryImplementor getFactory() { 95 return owner.getFactory(); 96 } 97 98 public ConnectionManager getConnectionManager() { 99 return connectionManager; 100 } 101 102 110 114 118 128 public Connection connection() throws HibernateException { 129 if ( !owner.isOpen() ) { 130 throw new SessionException( "Session is closed" ); 131 } 132 else if ( !connectionManager.isLogicallyConnected() ) { 133 throw new SessionException( "Session is currently disconnected" ); 134 } 135 136 return connectionManager.getConnection(); 137 } 138 139 154 public boolean registerCallbackIfNecessary() { 155 if ( isTransactionCallbackRegistered ) { 156 return false; 157 } 158 else { 159 isTransactionCallbackRegistered = true; 160 return true; 161 } 162 163 } 164 165 public boolean registerSynchronizationIfPossible() { 166 if ( isTransactionCallbackRegistered ) return true; 167 TransactionManager tm = owner.getFactory().getTransactionManager(); 168 if ( tm == null ) { 169 return false; 170 } 171 else { 172 try { 173 javax.transaction.Transaction tx = tm.getTransaction(); 174 if ( JTAHelper.isTransactionInProgress(tx) ) { 175 tx.registerSynchronization( new CacheSynchronization(owner, this, tx, null) ); 176 isTransactionCallbackRegistered = true; 177 log.debug("successfully registered Synchronization"); 178 return true; 179 } 180 else { 181 log.debug("no active transaction, could not register Synchronization"); 182 return false; 183 } 184 } 185 catch (Exception e) { 186 throw new TransactionException( "could not register synchronization with JTA TransactionManager", e ); 187 } 188 } 189 } 190 191 public boolean isTransactionInProgress() { 192 if ( isHibernateTransactionActive ) { 193 return true; 194 } 195 else { 196 return JTAHelper.isTransactionInProgress( owner.getFactory() ); 197 } 198 } 199 200 218 248 263 public Transaction beginTransaction() throws HibernateException { 264 Transaction tx = owner.getFactory().getSettings().getTransactionFactory() 265 .beginTransaction( this, owner ); 266 isHibernateTransactionActive = true; 267 return tx; 268 } 269 270 public void beforeTransactionCompletion(Transaction tx) { 271 log.trace( "before transaction completion" ); 272 owner.beforeTransactionCompletion(tx); 273 } 274 275 public void afterTransactionCompletion(boolean success, Transaction tx) { 276 log.trace( "after transaction completion" ); 277 278 if ( getFactory().getStatistics().isStatisticsEnabled() ) { 279 getFactory().getStatisticsImplementor().endTransaction(success); 280 } 281 282 connectionManager.afterTransaction(); 283 285 isTransactionCallbackRegistered = false; 286 isHibernateTransactionActive = false; 287 owner.afterTransactionCompletion(success, tx); 288 } 289 290 300 310 314 public void afterNontransactionalQuery(boolean success) { 315 log.trace( "after autocommit" ); 316 try { 317 boolean isAutocommit = connectionManager.isAutoCommit(); 322 323 connectionManager.afterTransaction(); 324 325 if ( isAutocommit ) { 326 owner.afterTransactionCompletion(success, null); 327 } 328 } 329 catch (SQLException sqle) { 330 success = false; 331 throw JDBCExceptionHelper.convert( 332 owner.getFactory().getSQLExceptionConverter(), 333 sqle, 334 "could not inspect JDBC autocommit mode" 335 ); 336 } 337 } 338 } 339 | Popular Tags |