FileDocCategorySizeDatePackage
JTASessionContext.javaAPI DocHibernate 3.2.55625Thu Oct 27 08:27:56 BST 2005org.hibernate.context

JTASessionContext

public class JTASessionContext extends Object implements CurrentSessionContext
An implementation of {@link CurrentSessionContext} which scopes the notion of a current session to a JTA transaction. Because JTA gives us a nice tie-in to clean up after ourselves, this implementation will generate Sessions as needed provided a JTA transaction is in effect. If a session is not already associated with the current JTA transaction at the time {@link #currentSession()} is called, a new session will be opened and it will be associated with that JTA transaction.

Note that the sessions returned from this method are automatically configured with both the {@link org.hibernate.cfg.Environment#FLUSH_BEFORE_COMPLETION auto-flush} and {@link org.hibernate.cfg.Environment#AUTO_CLOSE_SESSION auto-close} attributes set to true, meaning that the Session will be automatically flushed and closed as part of the lifecycle for the JTA transaction to which it is associated. Additionally, it will also be configured to aggressively release JDBC connections after each statement is executed. These settings are governed by the {@link #isAutoFlushEnabled()}, {@link #isAutoCloseEnabled()}, and {@link #getConnectionReleaseMode()} methods; these are provided (along with the {@link #buildOrObtainSession()} method) for easier subclassing for custom JTA-based session tracking logic (like maybe long-session semantics).

author
Steve Ebersole

Fields Summary
private static final Log
log
protected final org.hibernate.engine.SessionFactoryImplementor
factory
private transient Map
currentSessionMap
Constructors Summary
public JTASessionContext(org.hibernate.engine.SessionFactoryImplementor factory)


	   
		this.factory = factory;
	
Methods Summary
private org.hibernate.context.JTASessionContext$CleanupSynchbuildCleanupSynch(javax.transaction.Transaction txn)

		return new CleanupSynch( txn, this );
	
protected org.hibernate.classic.SessionbuildOrObtainSession()
Strictly provided for subclassing purposes; specifically to allow long-session support.

This implementation always just opens a new session.

return
the built or (re)obtained session.

		return factory.openSession(
				null,
		        isAutoFlushEnabled(),
		        isAutoCloseEnabled(),
		        getConnectionReleaseMode()
			);
	
public org.hibernate.classic.SessioncurrentSession()

		TransactionManager transactionManager = factory.getTransactionManager();
		if ( transactionManager == null ) {
			throw new HibernateException( "No TransactionManagerLookup specified" );
		}

		Transaction txn = null;
		try {
			txn = transactionManager.getTransaction();
			if ( txn == null ) {
				throw new HibernateException( "Unable to locate current JTA transaction" );
			}
			if ( !JTAHelper.isInProgress( txn.getStatus() ) ) {
				// We could register the session against the transaction even though it is
				// not started, but we'd have no guarentee of ever getting the map
				// entries cleaned up (aside from spawning threads).
				throw new HibernateException( "Current transaction is not in progress" );
			}
		}
		catch ( HibernateException e ) {
			throw e;
		}
		catch ( Throwable t ) {
			throw new HibernateException( "Problem locating/validating JTA transaction", t );
		}

		Session currentSession = ( Session ) currentSessionMap.get( txn );

		if ( currentSession == null ) {
			currentSession = buildOrObtainSession();

			try {
				txn.registerSynchronization( buildCleanupSynch( txn ) );
			}
			catch ( Throwable t ) {
				try {
					currentSession.close();
				}
				catch ( Throwable ignore ) {
					log.debug( "Unable to release generated current-session on failed synch registration", ignore );
				}
				throw new HibernateException( "Unable to register cleanup Synchronization with TransactionManager" );
			}

			currentSessionMap.put( txn, currentSession );
		}

		return currentSession;
	
protected org.hibernate.ConnectionReleaseModegetConnectionReleaseMode()
Mainly for subclass usage. This impl always returns after_statement.

return
The connection release mode for any built sessions.

		return ConnectionReleaseMode.AFTER_STATEMENT;
	
protected booleanisAutoCloseEnabled()
Mainly for subclass usage. This impl always returns true.

return
Whether or not the the session should be closed by transaction completion.

		return true;
	
protected booleanisAutoFlushEnabled()
Mainly for subclass usage. This impl always returns true.

return
Whether or not the the session should be flushed prior transaction completion.

		return true;