Top Description Inners Fields Constructors Methods
org.hibernate.context.internal

public Class JTASessionContext

extends AbstractCurrentSessionContext
Class Inheritance
Imports
java.util.Map, java.util.concurrent.ConcurrentHashMap, jakarta.transaction.Synchronization, .Transaction, .TransactionManager, org.hibernate.FlushMode, .HibernateException, .Session, org.hibernate.context.spi.AbstractCurrentSessionContext, org.hibernate.engine.spi.SessionFactoryImplementor, org.hibernate.engine.transaction.internal.jta.JtaStatusHelper, org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform, org.hibernate.internal.CoreMessageLogger, org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode, org.jboss.logging.Logger

An implementation of org.hibernate.context.spi.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 is opened and is associated with the JTA transaction.

Note that the sessions returned from this method are automatically configured with both the properties hibernate.transaction.flush_before_completion and hibernate.transaction.auto_close_session set to true, meaning that the session will be automatically flushed and closed as part of the lifecycle of the JTA transaction with which it is associated. Additionally, it will be configured to aggressively release JDBC connections after each statement is executed. These settings are governed by the isAutoFlushEnabled(), isAutoCloseEnabled(), and getConnectionHandlingMode() 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.CleanupSync

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

Field Summary

Modifier and TypeField and Description
private final transient Map<Object, Session>
private static final CoreMessageLogger

Constructor Summary

AccessConstructor and Description
public
JTASessionContext(SessionFactoryImplementor
The factory this context will service
factory
)

Deprecated as a consequence of SessionFactoryImplementor being deprecated.
Constructs a JTASessionContext

Method Summary

Modifier and TypeMethod and Description
private JTASessionContext.CleanupSync

Returns:

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

Builds a CleanupSync capable of cleaning up 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.spi.CurrentSessionContext.currentSession.

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

Returns:

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

Mainly for subclass usage.

protected boolean

Returns:

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

Mainly for subclass usage.

protected boolean

Returns:

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

Mainly for subclass usage.

Inherited from org.hibernate.context.spi.AbstractCurrentSessionContext:
baseSessionBuilderfactoryvalidateExistingSession

Field Detail

currentSessionMapback to summary
private final transient Map<Object, Session> currentSessionMap
LOGback to summary
private static final CoreMessageLogger LOG

Constructor Detail

JTASessionContextback to summary
public JTASessionContext(SessionFactoryImplementor factory)

Deprecated

as a consequence of SessionFactoryImplementor being deprecated. See corresponding docs for further information.

Constructs a JTASessionContext

Parameters
factory:SessionFactoryImplementor

The factory this context will service

Method Detail

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

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

Parameters
transactionIdentifier:Object

The transaction identifier under which the current session is registered.

Returns:JTASessionContext.CleanupSync

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.

Annotations
@SuppressWarnings:deprecation
currentSessionback to summary
public Session currentSession() throws HibernateException

Implements org.hibernate.context.spi.CurrentSessionContext.currentSession.

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

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

Returns:Session

The current session.

Annotations
@Override
Exceptions
HibernateException:
Typically indicates an issue locating or creating the current session.
getConnectionHandlingModeback to summary
protected PhysicalConnectionHandlingMode getConnectionHandlingMode()

Mainly for subclass usage. This impl always returns after_statement.

Returns:PhysicalConnectionHandlingMode

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 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 the session should be flushed prior transaction completion.

org.hibernate.context.internal back to summary

protected Class JTASessionContext.CleanupSync

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

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

Field Summary

Modifier and TypeField and Description
private final JTASessionContext
private final Object

Constructor Summary

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

Method Summary

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

Implements jakarta.transaction.Synchronization.afterCompletion.

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

Implements jakarta.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 final JTASessionContext context
transactionIdentifierback to summary
private final Object transactionIdentifier

Constructor Detail

CleanupSyncback to summary
public CleanupSync(Object transactionIdentifier, JTASessionContext context)

Method Detail

afterCompletionback to summary
public void afterCompletion(int i)

Implements jakarta.transaction.Synchronization.afterCompletion.

Doc from jakarta.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.

Annotations
@Override
beforeCompletionback to summary
public void beforeCompletion()

Implements jakarta.transaction.Synchronization.beforeCompletion.

Doc from jakarta.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.

Annotations
@Override