站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > Hibernate 3.2.4 正式版 API 英文文档

ThreadLocalSessionContext (Hibernate API Documentation) - Hibernate 3.2.4 正式版 API 英文文档


org.hibernate.context
Class ThreadLocalSessionContext

java.lang.Object
  extended byorg.hibernate.context.ThreadLocalSessionContext
All Implemented Interfaces:
CurrentSessionContext, Serializable

public class ThreadLocalSessionContext
extends Object
implements 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 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(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).

Author:
Steve Ebersole
See Also:
Serialized Form

Nested Class Summary
protected static class ThreadLocalSessionContext.CleanupSynch
          JTA transaction synch used for cleanup of the internal session map.
 
Field Summary
protected  SessionFactoryImplementor factory
           
 
Constructor Summary
ThreadLocalSessionContext(SessionFactoryImplementor factory)
           
 
Method Summary
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()
           
protected  boolean isAutoCloseEnabled()
          Mainly for subclass usage.
protected  boolean isAutoFlushEnabled()
          Mainly for subclass usage.
protected static Map sessionMap()
           
static Session unbind(SessionFactory factory)
          Unassociate a previously bound session from the current thread of execution.
protected  Session wrap(Session session)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

factory

protected final SessionFactoryImplementor factory
Constructor Detail

ThreadLocalSessionContext

public ThreadLocalSessionContext(SessionFactoryImplementor factory)
Method Detail

currentSession

public final Session currentSession()
                             throws HibernateException
Description copied from interface: CurrentSessionContext
Retrieve the current session according to the scoping defined by this implementation.

Specified by:
currentSession in interface CurrentSessionContext
Returns:
The current session.
Throws:
HibernateException - Typically indicates an issue locating or creating the current session.

getFactory

protected SessionFactoryImplementor getFactory()

buildOrObtainSession

protected Session buildOrObtainSession()
Strictly provided for subclassing purposes; specifically to allow long-session support.

This implementation always just opens a new session.

Returns:
the built or (re)obtained session.

buildCleanupSynch

protected ThreadLocalSessionContext.CleanupSynch buildCleanupSynch()

isAutoCloseEnabled

protected boolean isAutoCloseEnabled()
Mainly for subclass usage. This impl always returns true.

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

isAutoFlushEnabled

protected boolean isAutoFlushEnabled()
Mainly for subclass usage. This impl always returns true.

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

getConnectionReleaseMode

protected ConnectionReleaseMode getConnectionReleaseMode()
Mainly for subclass usage. This impl always returns after_transaction.

Returns:
The connection release mode for any built sessions.

wrap

protected Session wrap(Session session)

bind

public static void bind(Session session)
Associates the given session with the current thread of execution.

Parameters:
session - The session to bind.

unbind

public static Session unbind(SessionFactory factory)
Unassociate a previously bound session from the current thread of execution.

Returns:
The session which was unbound.

sessionMap

protected static Map sessionMap()