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

public Class ThreadLocalSessionContext

extends AbstractCurrentSessionContext
Class Inheritance
Imports
java.io.IOException, .ObjectInputStream, .ObjectOutputStream, .Serializable, java.lang.reflect.InvocationHandler, .InvocationTargetException, .Method, .Proxy, java.util.HashMap, .Locale, .Map, javax.transaction.Synchronization, org.hibernate.ConnectionReleaseMode, .HibernateException, .Session, .SessionFactory, .Transaction, org.hibernate.context.spi.AbstractCurrentSessionContext, org.hibernate.engine.jdbc.LobCreationContext, org.hibernate.engine.spi.SessionFactoryImplementor, .SessionImplementor, org.hibernate.event.spi.EventSource, org.hibernate.internal.CoreMessageLogger, org.hibernate.resource.transaction.spi.TransactionStatus, org.jboss.logging.Logger

A org.hibernate.context.spi.CurrentSessionContext impl which scopes the notion of current session by the current thread of execution. Unlike the JTA counterpart, threads do not give us a nice hook to perform any type of cleanup making it questionable for this impl to actually generate Session instances. In the interest of usability, it was decided to have this default impl actually generate a session upon first request and then clean it up after the org.hibernate.Transaction associated with that session is committed/rolled-back. In order for ensuring that happens, the sessions generated here are unusable until after Session#beginTransaction() has been called. If close() is called on a session managed by this class, it will be automatically unbound. Additionally, the static bind and unbind methods are provided to allow application code to explicitly control opening and closing of these sessions. This, with some from of interception, is the preferred approach. It also allows easy framework integration and one possible approach for implementing long-sessions. The buildOrObtainSession, isAutoCloseEnabled, isAutoFlushEnabled, getConnectionReleaseMode, and buildCleanupSynch methods are all provided to allow easy subclassing (for long-running session scenarios, for example).
Authors
Steve Ebersole, Sanne Grinovero

Nested and Inner Type Summary

Modifier and TypeClass and Description
protected static class
ThreadLocalSessionContext.CleanupSync

Transaction sync used for cleanup of the internal session map.

private class

Field Summary

Modifier and TypeField and Description
private static final ThreadLocal<Map<SessionFactory, Session>>
CONTEXT_TL

A ThreadLocal maintaining current sessions for the given execution thread.

private static final CoreMessageLogger
private static final Class<T>[]

Constructor Summary

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

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

Method Summary

Modifier and TypeMethod and Description
public static void
bind(Session
The session to bind.
session
)

Deprecated as a consequence of Session being deprecated.
Associates the given session with the current thread of execution.
protected ThreadLocalSessionContext.CleanupSync
protected Session

Returns:

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

Deprecated as a consequence of Session being deprecated.
Strictly provided for sub-classing purposes; specifically to allow long-session support.
public final Session
currentSession()

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

Deprecated as a consequence of Session being deprecated.
Retrieve the current session according to the scoping defined by this implementation.
private static void
doBind(Session session, SessionFactory factory)

Deprecated as a consequence of Session and SessionFactory being deprecated.
private static Session
doUnbind(SessionFactory factory, boolean releaseMapIfEmpty)

Deprecated as a consequence of SessionFactory and Session being deprecated.
private static Session
existingSession(SessionFactory factory)

Deprecated as a consequence of SessionFactory and Session being deprecated.
protected ConnectionReleaseMode

Returns:

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

Mainly for subclass usage.

protected SessionFactoryImplementor

Returns:

Value for property 'factory'.
getFactory
()

Deprecated as a consequence of SessionFactoryImplementor being deprecated.
Getter for property 'factory'.
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 to transaction completion.
isAutoFlushEnabled
()

Mainly for subclass usage.

private boolean
needsWrapping(Session session)

Deprecated as a consequence of Session being deprecated.
protected static Map<SessionFactory, Session>
private static void
terminateOrphanedSession(Session orphan)

Deprecated as a consequence of Session being deprecated.
public static Session

Returns:

The session which was unbound.
unbind
(SessionFactory
The factory for which the session should be unbound.
factory
)

Deprecated as a consequence of SessionFactory and Session being deprecated.
Disassociates a previously bound session from the current thread of execution.
protected Session
wrap(Session session)

Deprecated as a consequence of Session being deprecated.
Inherited from org.hibernate.context.spi.AbstractCurrentSessionContext:
baseSessionBuilderfactoryvalidateExistingSession

Field Detail

CONTEXT_TLback to summary
private static final ThreadLocal<Map<SessionFactory, Session>> CONTEXT_TL

A ThreadLocal maintaining current sessions for the given execution thread. The actual ThreadLocal variable is a java.util.Map to account for the possibility for multiple SessionFactory instances being used during execution of the given thread.

LOGback to summary
private static final CoreMessageLogger LOG
SESSION_PROXY_INTERFACESback to summary
private static final Class<T>[] SESSION_PROXY_INTERFACES

Constructor Detail

ThreadLocalSessionContextback to summary
public ThreadLocalSessionContext(SessionFactoryImplementor factory)

Deprecated

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

Constructs a ThreadLocal

Parameters
factory:SessionFactoryImplementor

The factory this context will service

Method Detail

bindback to summary
public static void bind(Session session)

Deprecated

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

Associates the given session with the current thread of execution.

Parameters
session:Session

The session to bind.

buildCleanupSynchback to summary
protected ThreadLocalSessionContext.CleanupSync buildCleanupSynch()
buildOrObtainSessionback to summary
protected Session buildOrObtainSession()

Deprecated

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

Strictly provided for sub-classing 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 final Session currentSession() throws HibernateException

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

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

Deprecated

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

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.
doBindback to summary
private static void doBind(Session session, SessionFactory factory)

Deprecated

as a consequence of Session and SessionFactory being deprecated. See corresponding docs for further information.

Annotations
@SuppressWarnings:unchecked
doUnbindback to summary
private static Session doUnbind(SessionFactory factory, boolean releaseMapIfEmpty)

Deprecated

as a consequence of SessionFactory and Session being deprecated. See corresponding docs for further information.

existingSessionback to summary
private static Session existingSession(SessionFactory factory)

Deprecated

as a consequence of SessionFactory and Session being deprecated. See corresponding docs for further information.

getConnectionReleaseModeback to summary
protected ConnectionReleaseMode getConnectionReleaseMode()

Mainly for subclass usage. This impl always returns after_transaction.

Returns:ConnectionReleaseMode

The connection release mode for any built sessions.

getFactoryback to summary
protected SessionFactoryImplementor getFactory()

Deprecated

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

Getter for property 'factory'.

Returns:SessionFactoryImplementor

Value for property 'factory'.

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 to transaction completion.

needsWrappingback to summary
private boolean needsWrapping(Session session)

Deprecated

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

sessionMapback to summary
protected static Map<SessionFactory, Session> sessionMap()
terminateOrphanedSessionback to summary
private static void terminateOrphanedSession(Session orphan)

Deprecated

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

unbindback to summary
public static Session unbind(SessionFactory factory)

Deprecated

as a consequence of SessionFactory and Session being deprecated. See corresponding docs for further information.

Disassociates a previously bound session from the current thread of execution.

Parameters
factory:SessionFactory

The factory for which the session should be unbound.

Returns:Session

The session which was unbound.

wrapback to summary
protected Session wrap(Session session)

Deprecated

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

org.hibernate.context.internal back to summary

protected Class ThreadLocalSessionContext.CleanupSync

extends Object
implements Synchronization, Serializable
Class Inheritance
  • java.lang.Object
  • org.hibernate.context.internal.ThreadLocalSessionContext.CleanupSync
All Implemented Interfaces
java.io.Serializable, javax.transaction.Synchronization

Transaction sync used for cleanup of the internal session map.

Field Summary

Modifier and TypeField and Description
protected final SessionFactory
factory

Deprecated as a consequence of SessionFactory being deprecated.

Constructor Summary

AccessConstructor and Description
public
CleanupSync(SessionFactory factory)

Deprecated as a consequence of SessionFactory being deprecated.

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

factoryback to summary
protected final SessionFactory factory

Deprecated

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

Constructor Detail

CleanupSyncback to summary
public CleanupSync(SessionFactory factory)

Deprecated

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

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.

Annotations
@Override
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.

Annotations
@Override
org.hibernate.context.internal back to summary

private Class ThreadLocalSessionContext.TransactionProtectionWrapper

extends Object
implements InvocationHandler, Serializable
Class Inheritance
  • java.lang.Object
  • org.hibernate.context.internal.ThreadLocalSessionContext.TransactionProtectionWrapper
All Implemented Interfaces
java.io.Serializable, java.lang.reflect.InvocationHandler

Field Summary

Modifier and TypeField and Description
private final Session
realSession

Deprecated as a consequence of Session being deprecated.
private Session
wrappedSession

Deprecated as a consequence of Session being deprecated.

Constructor Summary

AccessConstructor and Description
public
TransactionProtectionWrapper(Session realSession)

Deprecated as a consequence of Session being deprecated.

Method Summary

Modifier and TypeMethod and Description
public Object
invoke(Object
the proxy instance that the method was invoked on
proxy
,
Method
the Method instance corresponding to the interface method invoked on the proxy instance. The declaring class of the Method object will be the interface that the method was declared in, which may be a superinterface of the proxy interface that the proxy class inherits the method through.
method
,
Object[]
an array of objects containing the values of the arguments passed in the method invocation on the proxy instance, or null if interface method takes no arguments. Arguments of primitive types are wrapped in instances of the appropriate primitive wrapper class, such as java.lang.Integer or java.lang.Boolean.
args
)

Implements java.lang.reflect.InvocationHandler.invoke.

Processes a method invocation on a proxy instance and returns the result.
private void
public void
setWrapped(Session
Value to set for property 'wrapped'.
wrapped
)

Deprecated as a consequence of Session being deprecated.
Setter for property 'wrapped'.
private void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

realSessionback to summary
private final Session realSession

Deprecated

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

wrappedSessionback to summary
private Session wrappedSession

Deprecated

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

Constructor Detail

TransactionProtectionWrapperback to summary
public TransactionProtectionWrapper(Session realSession)

Deprecated

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

Method Detail

invokeback to summary
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable

Implements java.lang.reflect.InvocationHandler.invoke.

Doc from java.lang.reflect.InvocationHandler.invoke.

Processes a method invocation on a proxy instance and returns the result. This method will be invoked on an invocation handler when a method is invoked on a proxy instance that it is associated with.

Parameters
proxy:Object

the proxy instance that the method was invoked on

method:Method

the Method instance corresponding to the interface method invoked on the proxy instance. The declaring class of the Method object will be the interface that the method was declared in, which may be a superinterface of the proxy interface that the proxy class inherits the method through.

args:Object[]

an array of objects containing the values of the arguments passed in the method invocation on the proxy instance, or null if interface method takes no arguments. Arguments of primitive types are wrapped in instances of the appropriate primitive wrapper class, such as java.lang.Integer or java.lang.Boolean.

Returns:Object

the value to return from the method invocation on the proxy instance. If the declared return type of the interface method is a primitive type, then the value returned by this method must be an instance of the corresponding primitive wrapper class; otherwise, it must be a type assignable to the declared return type. If the value returned by this method is null and the interface method's return type is primitive, then a NullPointerException will be thrown by the method invocation on the proxy instance. If the value returned by this method is otherwise not compatible with the interface method's declared return type as described above, a ClassCastException will be thrown by the method invocation on the proxy instance.

Annotations
@Override
@SuppressWarnings:SimplifiableIfStatement
Exceptions
Throwable:
the exception to throw from the method invocation on the proxy instance. The exception's type must be assignable either to any of the exception types declared in the throws clause of the interface method or to the unchecked exception types java.lang.RuntimeException or java.lang.Error. If a checked exception is thrown by this method that is not assignable to any of the exception types declared in the throws clause of the interface method, then an UndeclaredThrowableException containing the exception that was thrown by this method will be thrown by the method invocation on the proxy instance.
readObjectback to summary
private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException
setWrappedback to summary
public void setWrapped(Session wrapped)

Deprecated

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

Setter for property 'wrapped'.

Parameters
wrapped:Session

Value to set for property 'wrapped'.

writeObjectback to summary
private void writeObject(ObjectOutputStream oos) throws IOException