类 ThreadLocalSessionContext
- java.lang.Object
-
- org.hibernate.context.spi.AbstractCurrentSessionContext
-
- org.hibernate.context.internal.ThreadLocalSessionContext
-
- 所有已实现的接口:
Serializable,CurrentSessionContext
public class ThreadLocalSessionContext extends AbstractCurrentSessionContext
ACurrentSessionContextimpl 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 theTransactionassociated with that session is committed/rolled-back. In order for ensuring that happens, the sessions generated here are unusable until afterSharedSessionContract.beginTransaction()has been called. If close() is called on a session managed by this class, it will be automatically unbound. Additionally, the staticbind(org.hibernate.Session)andunbind(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. ThebuildOrObtainSession(),isAutoCloseEnabled(),isAutoFlushEnabled(),getConnectionReleaseMode(), andbuildCleanupSynch()methods are all provided to allow easy subclassing (for long-running session scenarios, for example).- 作者:
- Steve Ebersole, Sanne Grinovero
- 另请参阅:
- 序列化表格
-
-
嵌套类概要
嵌套类 修饰符和类型 类 说明 protected static classThreadLocalSessionContext.CleanupSyncTransaction sync used for cleanup of the internal session map.
-
构造器概要
构造器 构造器 说明 ThreadLocalSessionContext(SessionFactoryImplementor factory)Constructs a ThreadLocal
-
方法概要
所有方法 静态方法 实例方法 具体方法 修饰符和类型 方法 说明 static voidbind(Session session)Associates the given session with the current thread of execution.protected ThreadLocalSessionContext.CleanupSyncbuildCleanupSynch()protected SessionbuildOrObtainSession()Strictly provided for sub-classing purposes; specifically to allow long-session support.SessioncurrentSession()Retrieve the current session according to the scoping defined by this implementation.protected ConnectionReleaseModegetConnectionReleaseMode()Mainly for subclass usage.protected SessionFactoryImplementorgetFactory()Getter for property 'factory'.protected booleanisAutoCloseEnabled()Mainly for subclass usage.protected booleanisAutoFlushEnabled()Mainly for subclass usage.protected static Map<SessionFactory,Session>sessionMap()static Sessionunbind(SessionFactory factory)Disassociates a previously bound session from the current thread of execution.protected Sessionwrap(Session session)-
从类继承的方法 org.hibernate.context.spi.AbstractCurrentSessionContext
baseSessionBuilder, factory, validateExistingSession
-
-
-
-
构造器详细资料
-
ThreadLocalSessionContext
public ThreadLocalSessionContext(SessionFactoryImplementor factory)
Constructs a ThreadLocal- 参数:
factory- The factory this context will service
-
-
方法详细资料
-
currentSession
public final Session currentSession() throws HibernateException
从接口复制的说明:CurrentSessionContextRetrieve the current session according to the scoping defined by this implementation.- 返回:
- The current session.
- 抛出:
HibernateException- Typically indicates an issue locating or creating the current session.
-
getFactory
protected SessionFactoryImplementor getFactory()
Getter for property 'factory'.- 返回:
- Value for property 'factory'.
-
buildOrObtainSession
protected Session buildOrObtainSession()
Strictly provided for sub-classing purposes; specifically to allow long-session support. This implementation always just opens a new session.- 返回:
- the built or (re)obtained session.
-
buildCleanupSynch
protected ThreadLocalSessionContext.CleanupSync buildCleanupSynch()
-
isAutoCloseEnabled
protected boolean isAutoCloseEnabled()
Mainly for subclass usage. This impl always returns true.- 返回:
- 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.- 返回:
- Whether or not the the session should be flushed prior to transaction completion.
-
getConnectionReleaseMode
protected ConnectionReleaseMode getConnectionReleaseMode()
Mainly for subclass usage. This impl always returns after_transaction.- 返回:
- The connection release mode for any built sessions.
-
bind
public static void bind(Session session)
Associates the given session with the current thread of execution.- 参数:
session- The session to bind.
-
unbind
public static Session unbind(SessionFactory factory)
Disassociates a previously bound session from the current thread of execution.- 参数:
factory- The factory for which the session should be unbound.- 返回:
- The session which was unbound.
-
sessionMap
protected static Map<SessionFactory,Session> sessionMap()
-
-