Top Description Inners Fields Constructors Methods
org.hibernate.context

public Class JTASessionContext

extends Object
implements CurrentSessionContext
Class Inheritance
All Implemented Interfaces
org.hibernate.context.CurrentSessionContext, java.io.Serializable
Imports
org.hibernate.HibernateException, .ConnectionReleaseMode, org.hibernate.classic.Session, org.hibernate.engine.SessionFactoryImplementor, org.hibernate.util.JTAHelper, org.slf4j.Logger, .LoggerFactory, javax.transaction.Transaction, .TransactionManager, .Synchronization, java.util.Map, .Hashtable

An implementation of 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 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 auto-flush and 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 isAutoFlushEnabled(), isAutoCloseEnabled(), and getConnectionReleaseMode() methods; these are provided (along with the buildOrObtainSession() method) for easier subclassing for custom JTA-based session tracking logic (like maybe long-session semantics).

Author
Steve Ebersole

Nested and Inner Type Summary

Modifier and TypeClass and Description
protected static class
JTASessionContext.CleanupSynch

JTA transaction synch used for cleanup of the internal session map.

Field Summary

Modifier and TypeField and Description
private transient Map<K, V>
protected final SessionFactoryImplementor
private static final Logger

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private JTASessionContext.CleanupSynch

Returns:

The cleanup synch.
buildCleanupSynch
(Object
The transaction identifier under which the current session is registered.
transactionIdentifier
)

Builds a CleanupSynch capable of cleaning up the the current session map as an after transaction callback.

protected Session

Returns:

the built or (re)obtained session.
buildOrObtainSession
()

Strictly provided for subclassing purposes; specifically to allow long-session support.

public Session
currentSession()

Implements org.hibernate.context.CurrentSessionContext.currentSession.

Retrieve the current session according to the scoping defined by this implementation.
protected ConnectionReleaseMode

Returns:

The connection release mode for any built sessions.
getConnectionReleaseMode
()

Mainly for subclass usage.

protected boolean

Returns:

Whether or not the the session should be closed by transaction completion.
isAutoCloseEnabled
()

Mainly for subclass usage.

protected boolean

Returns:

Whether or not the the session should be flushed prior transaction completion.
isAutoFlushEnabled
()

Mainly for subclass usage.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

currentSessionMapback to summary
private transient Map<K, V> currentSessionMap
factoryback to summary
protected final SessionFactoryImplementor factory
logback to summary
private static final Logger log

Constructor Detail

JTASessionContextback to summary
public JTASessionContext(SessionFactoryImplementor factory)

Method Detail

buildCleanupSynchback to summary
private JTASessionContext.CleanupSynch buildCleanupSynch(Object transactionIdentifier)

Builds a CleanupSynch capable of cleaning up the the current session map as an after transaction callback.

Parameters
transactionIdentifier:Object

The transaction identifier under which the current session is registered.

Returns:JTASessionContext.CleanupSynch

The cleanup synch.

buildOrObtainSessionback to summary
protected Session buildOrObtainSession()

Strictly provided for subclassing purposes; specifically to allow long-session support.

This implementation always just opens a new session.

Returns:Session

the built or (re)obtained session.

currentSessionback to summary
public Session currentSession() throws HibernateException

Implements org.hibernate.context.CurrentSessionContext.currentSession.

Doc from org.hibernate.context.CurrentSessionContext.currentSession.

Retrieve the current session according to the scoping defined by this implementation.

Returns:Session

The current session.

Exceptions
HibernateException:
Typically indicates an issue locating or creating the current session.
getConnectionReleaseModeback to summary
protected ConnectionReleaseMode getConnectionReleaseMode()

Mainly for subclass usage. This impl always returns after_statement.

Returns:ConnectionReleaseMode

The connection release mode for any built sessions.

isAutoCloseEnabledback to summary
protected boolean isAutoCloseEnabled()

Mainly for subclass usage. This impl always returns true.

Returns:boolean

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

isAutoFlushEnabledback to summary
protected boolean isAutoFlushEnabled()

Mainly for subclass usage. This impl always returns true.

Returns:boolean

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

org.hibernate.context back to summary

protected Class JTASessionContext.CleanupSynch

extends Object
implements Synchronization
Class Inheritance
All Implemented Interfaces
javax.transaction.Synchronization

JTA transaction synch used for cleanup of the internal session map.

Field Summary

Modifier and TypeField and Description
private JTASessionContext
private Object

Constructor Summary

AccessConstructor and Description
public
CleanupSynch(Object transactionIdentifier, JTASessionContext context)

Method Summary

Modifier and TypeMethod and Description
public void
afterCompletion(int
The status of the transaction completion.
i
)

Implements javax.transaction.Synchronization.afterCompletion.

This method is called by the transaction manager after the transaction is committed or rolled back.
public void
beforeCompletion()

Implements javax.transaction.Synchronization.beforeCompletion.

The beforeCompletion method is called by the transaction manager prior to the start of the two-phase transaction commit process.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

contextback to summary
private JTASessionContext context
transactionIdentifierback to summary
private Object transactionIdentifier

Constructor Detail

CleanupSynchback to summary
public CleanupSynch(Object transactionIdentifier, JTASessionContext context)

Method Detail

afterCompletionback to summary
public void afterCompletion(int i)

Implements javax.transaction.Synchronization.afterCompletion.

Doc from javax.transaction.Synchronization.afterCompletion.

This method is called by the transaction manager after the transaction is committed or rolled back.

Parameters
i:int

The status of the transaction completion.

beforeCompletionback to summary
public void beforeCompletion()

Implements javax.transaction.Synchronization.beforeCompletion.

Doc from javax.transaction.Synchronization.beforeCompletion.

The beforeCompletion method is called by the transaction manager prior to the start of the two-phase transaction commit process. This call is executed with the transaction context of the transaction that is being committed.