Package org.hibernate.context.internal
Class ThreadLocalSessionContext
java.lang.Object
org.hibernate.context.spi.AbstractCurrentSessionContext
org.hibernate.context.internal.ThreadLocalSessionContext
- All Implemented Interfaces:
Serializable
,CurrentSessionContext
A
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 Transaction
associated with that session is committed/rolled-back. In order for ensuring that happens, the
sessions generated here are unusable until after SharedSessionContract.beginTransaction()
has been
called. If close()
is called on a session managed by this class, it will be automatically
unbound.
Additionally, the static bind(org.hibernate.Session)
and unbind(org.hibernate.SessionFactory)
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()
,
getConnectionHandlingMode()
, and buildCleanupSynch()
methods are all provided to allow easy
subclassing (for long-running session scenarios, for example).
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static class
Transaction sync used for cleanup of the internal session map. -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a ThreadLocal -
Method Summary
Modifier and TypeMethodDescriptionstatic void
Associates the given session with the current thread of execution.protected ThreadLocalSessionContext.CleanupSync
protected Session
Strictly provided for sub-classing purposes; specifically to allow long-session support.final Session
Retrieve the current session according to the scoping defined by this implementation.protected PhysicalConnectionHandlingMode
Mainly for subclass usage.protected SessionFactoryImplementor
Getter for property 'factory'.protected boolean
Mainly for subclass usage.protected boolean
Mainly for subclass usage.protected static Map<SessionFactory,
Session> static Session
unbind
(SessionFactory factory) Disassociates a previously bound session from the current thread of execution.protected Session
Methods inherited from class org.hibernate.context.spi.AbstractCurrentSessionContext
baseSessionBuilder, factory, validateExistingSession
-
Constructor Details
-
ThreadLocalSessionContext
Constructs a ThreadLocal- Parameters:
factory
- The factory this context will service
-
-
Method Details
-
currentSession
Description copied from interface:CurrentSessionContext
Retrieve the current session according to the scoping defined by this implementation.- Returns:
- The current session.
- Throws:
HibernateException
- Typically indicates an issue locating or creating the current session.
-
getFactory
Getter for property 'factory'.- Returns:
- Value for property 'factory'.
-
buildOrObtainSession
Strictly provided for sub-classing purposes; specifically to allow long-session support.This implementation always just opens a new session.
- Returns:
- the built or (re)obtained session.
-
buildCleanupSynch
-
isAutoCloseEnabled
protected boolean isAutoCloseEnabled()Mainly for subclass usage. This impl always returns true.- Returns:
- Whether the session should be closed by transaction completion.
-
isAutoFlushEnabled
protected boolean isAutoFlushEnabled()Mainly for subclass usage. This impl always returns true.- Returns:
- Whether the session should be flushed prior to transaction completion.
-
getConnectionHandlingMode
Mainly for subclass usage. This impl always returns after_transaction.- Returns:
- The connection release mode for any built sessions.
-
wrap
-
bind
Associates the given session with the current thread of execution.- Parameters:
session
- The session to bind.
-
unbind
Disassociates a previously bound session from the current thread of execution.- Parameters:
factory
- The factory for which the session should be unbound.- Returns:
- The session which was unbound.
-
sessionMap
-