上下文相关可以理解为范围。一个会话必须发生在一定的环境下,有一定的存活范围,而这个环境和范围就称为上下文。
假设整个应用只用一个Session,,那么这个Session的上下文就是这个应用的范围。相反,假设每次增删改查都使用全新的Session,使用一次之后又销毁掉,那么每次操作的范围都是这些Session的上下文。
一种常见的情况则是为每个线程绑定一个session,这就是基于ThreadLocal的上下文会话。例如,在Web应用中,每个request都必须交给一个线程来处理,如果这个线程使用session来进行数据库交互。这时候,我们首先必须拥有一个session(新建或者使用已有的), 然后再将这个session与这个线程绑定在一起。当完成绑定之后该线程生命范围内的获取当前session的操作都会返回已绑定的那个session。
为了管理session的上下文,hibernate提供了org.hibernate.context.CurrentSessionContext接口。这个接口只提供了一个方法:
/** * Retrieve the current session according to the scoping defined * by this implementation. * * @return The current session. * @throws HibernateException Typically indicates an issue * locating or creating the current session. */ public org.hibernate.classic.Session currentSession() throws HibernateException;
实际上,这表示了如何获取当前session是session上下文管理中首要解决的,session上下文管理只需要管理在某种环境下该返回哪个session,而可以不必关注session本身(例如session的open,close等操作)。下面列出了hibernate内置的CurrentSessionContext接口的三种实现,你将会发现,这三种实现除了对上下文的定义不同之外,在对待session的管理上也有不同,例如是否对session进行清理,如何结合自身的特点进行清理。
Hibernate内置的CurrentSessionContext接口的三种实现:
1). org.hibernate.context.JTASessionContext:当前会话根据 JTA 来跟踪和界定。这和以前的仅支持 JTA 的方法是完全一样的。详情请参阅 Javadoc。
2). org.hibernate.context.ThreadLocalSessionContext:当前会话通过当前执行的线程来跟踪和界定。详情也请参阅 Javadoc。
3). org.hibernate.context.ManagedSessionContext:当前会话通过当前执行的线程来跟踪和界定。但是,你需要负责使用这个类的静态方法将 Session 实例绑定、或者取消绑定,它并不会打开(open)、flush 或者关闭(close)任何 Session。
/** * An implementation of {@link CurrentSessionContext} which scopes the notion * of a current session to a JTA transaction. Because JTA gives us a nice * tie-in to clean up after ourselves, this implementation will generate * Sessions as needed provided a JTA transaction is in effect. If a session * is not already associated with the current JTA transaction at the time * {@link #currentSession()} is called, a new session will be opened and it * will be associated with that JTA transaction. * <p/> * Note that the sessions returned from this method are automatically configured with * both the {@link org.hibernate.cfg.Environment#FLUSH_BEFORE_COMPLETION auto-flush} and * {@link org.hibernate.cfg.Environment#AUTO_CLOSE_SESSION auto-close} attributes set to * true, meaning that the Session will be automatically flushed and closed * as part of the lifecycle for the JTA transaction to which it is associated. * Additionally, it will also be configured to aggressively release JDBC * connections after each statement is executed. These settings are governed * by the {@link #isAutoFlushEnabled()}, {@link #isAutoCloseEnabled()}, and * {@link #getConnectionReleaseMode()} methods; these are provided (along with * the {@link #buildOrObtainSession()} method) for easier subclassing for custom * JTA-based session tracking logic (like maybe long-session semantics). * * @author Steve Ebersole */ public class JTASessionContext implements CurrentSessionContext
JTASessionContext是一种用JTA事务来划分当前session概念的实现。由于JTA提供了一个良好的关联(tie-in)可用于清理,因此针对每个有效的JTA事务,这种实现会根据需要产生一些session。如果在调用currentSession()方法时,当前的JTA事务还没有与任何session关联,那么一个新的session将会被打开并与这个JTA事务关联起来。
需注意的是,这些由currentSession()方法返回的session都已经自动将org.hibernate.cfg.Environment.FLUSH_BEFORE_COMPLETION auto-flush和org.hibernate.cfg.Environment.AUTO_CLOSE_SESSION auto-close这两个属性配置为true了,这就表示这个session将会作为它所关联的JTA事务的一部分,被自动地flush和close。
另外,这个session还被配置成当每一个statement被执行后就主动地释放JDBC连接。这些配置是通过isAutoFlushEnabled(),isAutoCloseEnabled()和getConnectionReleaseMode()方法来控制的;这些(还有buildOrObtainSession()方法)是为了能更简单地定制基于JTA的session跟踪逻辑(像long-session这种)的子类化。
/** * A {@link 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 {@link 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 {@link Session#beginTransaction()} * has been called. If <tt>close()</tt> is called on a session managed by * this class, it will be automatically unbound. * <p/> * Additionally, the static {@link #bind} and {@link #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. * <p/> * The {@link #buildOrObtainSession}, {@link #isAutoCloseEnabled}, * {@link #isAutoFlushEnabled}, {@link #getConnectionReleaseMode}, and * {@link #buildCleanupSynch} methods are all provided to allow easy * subclassing (for long-running session scenarios, for example). * * @author Steve Ebersole */ public class ThreadLocalSessionContext implements CurrentSessionContext
顾名思义,ThreadLocalSessionContext是一种根据当前正在执行的线程来定义当前session的概念的。不同于JTA的是,线程并不能提供一个良好的hook(钩子)给我们来执行任何形式的清理,这也使得要用这种实现来真正地产生Session实例变得可疑。从可用性的角度出发,我们决定让默认实现在第一次请求的时候产生一个session,然后在与这个session相关的事务(org.hibernate.Transaction)提交或回滚后就清理它(清理后session处于closed状态,并与当前线程解除绑定)。为了保证这个过程能够顺利进行,新产生的session们必须等到session.beginTransaction()方法被调用后才能够可用。如果这个类所管理的sesion的close()方法被调用了,那么这个session将自动地解除与这个类的绑定。
另外,为了能够明确地控制session们的开闭,ThreadLocalSessionContext提供了bind和unbind这些静态方法。这种带有些许Interception方式的实现是一种更好的方法。它允许了简单框架的集成,也为实现长session(long-session)提供了一种可能。
buildOrObtainSession(), isAutoCloseEnabled(), isAutoFlushEnabled(), getConnectionReleaseMode(), 和 buildCleanupSynch()这些方法是提供给子类用于定制相关行为时使用的(例如用于long-running session的场合)。
根据上述的内容,如果不想让session在每次事务提交或回滚后就关闭(同时解除绑定),则可以通过继承ThreadLocalSessionContext并重写isAutoClosedEnabled()方法使其返回false。这样,在每次事务完成后将不再自动地关闭session,你可以通过显式的调用session.close()方法来关闭它。而且你可能还需要调用unbind()方法来显式地将session与当前线程解除绑定。 |
/** * Represents a {@link CurrentSessionContext} the notion of a contextual session * is managed by some external entity (generally some form of interceptor, etc). * This external manager is responsible for scoping these contextual sessions * appropriately binding/unbinding them here for exposure to the application * through {@link SessionFactory#getCurrentSession} calls. * <p/> * Basically exposes two interfaces. <ul> * <li>First is the implementation of CurrentSessionContext which is then used * by the {@link SessionFactory#getCurrentSession()} calls. This * portion is instance-based specific to the session factory owning the given * instance of this impl (there will be one instance of this per each session * factory using this strategy). * <li>Second is the externally facing methods {@link #hasBind}, {@link #bind}, * and {@link #unbind} used by the external thing to manage exposure of the * current session it is scoping. This portion is static to allow easy * reference from that external thing. * </ul> * The underlying storage of the current sessions here is a static * {@link ThreadLocal}-based map where the sessions are keyed by the * the owning session factory. * * @author Steve Ebersole */ public class ManagedSessionContext implements CurrentSessionContext
ManagedSessionContext是上下文会话由外部实体(一般指interceptor等)管理的一种实现。
外部管理器负责通过绑定和解除绑定(binding/unbinding)来合适地确定这些会话的范围,并提供SessionFactory的getCurrentSession方法给应用来调用。
主要提供了以下两个接口:
在这种实现方式下,session的底层存储方法是基于静态(ThreadLocal)map的,session们是value,而拥有这些session的session factory则是key。
使用ManagedSessionContext我们可定制的内容将更多。例如在session的关闭上,ThreadLocalSessionContext默认在事务结束之后就关闭当前session并解除绑定关系,而ManagedSessionContext默认不关闭session,你只能通过session的close(()方法和ManagedSessionContext.unbind()方法来关闭和解除绑定。 |