public class ThreadLocalSessionContext extends AbstractCurrentSessionContext
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()
, getConnectionReleaseMode()
, and
buildCleanupSynch()
methods are all provided to allow easy
subclassing (for long-running session scenarios, for example).Modifier and Type | Class and Description |
---|---|
protected static class |
ThreadLocalSessionContext.CleanupSynch
JTA transaction synch used for cleanup of the internal session map.
|
Constructor and Description |
---|
ThreadLocalSessionContext(SessionFactoryImplementor factory) |
Modifier and Type | Method and Description |
---|---|
static void |
bind(Session session)
Associates the given session with the current thread of execution.
|
protected ThreadLocalSessionContext.CleanupSynch |
buildCleanupSynch() |
protected Session |
buildOrObtainSession()
Strictly provided for subclassing purposes; specifically to allow long-session
support.
|
Session |
currentSession()
Retrieve the current session according to the scoping defined
by this implementation.
|
protected ConnectionReleaseMode |
getConnectionReleaseMode()
Mainly for subclass usage.
|
protected SessionFactoryImplementor |
getFactory()
Getter for property 'factory'.
|
protected boolean |
isAutoCloseEnabled()
Mainly for subclass usage.
|
protected boolean |
isAutoFlushEnabled()
Mainly for subclass usage.
|
protected static Map |
sessionMap() |
static Session |
unbind(SessionFactory factory)
Disassociates a previously bound session from the current thread of execution.
|
protected Session |
wrap(Session session) |
baseSessionBuilder, factory, validateExistingSession
public ThreadLocalSessionContext(SessionFactoryImplementor factory)
public final Session currentSession() throws HibernateException
CurrentSessionContext
HibernateException
- Typically indicates an issue
locating or creating the current session.protected SessionFactoryImplementor getFactory()
protected Session buildOrObtainSession()
protected ThreadLocalSessionContext.CleanupSynch buildCleanupSynch()
protected boolean isAutoCloseEnabled()
protected boolean isAutoFlushEnabled()
protected ConnectionReleaseMode getConnectionReleaseMode()
public static void bind(Session session)
session
- The session to bind.public static Session unbind(SessionFactory factory)
factory
- The factory for which the session should be unbound.protected static Map sessionMap()
Copyright © 2012 JBoss by Red Hat. All Rights Reserved.