理解Session的上下文

来源:互联网 发布:uusee网络电视手机版 编辑:程序博客网 时间:2024/05/18 18:55

一、什么是上下文相关的Session(Contextual Session)?

上下文相关可以理解为范围。一个会话必须发生在一定的环境下,有一定的存活范围,而这个环境和范围就称为上下文。

假设整个应用只用一个Session,,那么这个Session的上下文就是这个应用的范围。相反,假设每次增删改查都使用全新的Session,使用一次之后又销毁掉,那么每次操作的范围都是这些Session的上下文。

 

一种常见的情况则是为每个线程绑定一个session,这就是基于ThreadLocal的上下文会话。例如,在Web应用中,每个request都必须交给一个线程来处理,如果这个线程使用session来进行数据库交互。这时候,我们首先必须拥有一个session(新建或者使用已有的), 然后再将这个session与这个线程绑定在一起。当完成绑定之后该线程生命范围内的获取当前session的操作都会返回已绑定的那个session。 

 

二、如何管理当前的session

1. 一个接口

为了管理session的上下文,hibernate提供了org.hibernate.context.CurrentSessionContext接口。这个接口只提供了一个方法:

 

Java代码  收藏代码
  1. /** 
  2.  * Retrieve the current session according to the scoping defined 
  3.  * by this implementation. 
  4.  * 
  5.  * @return The current session. 
  6.  * @throws HibernateException Typically indicates an issue 
  7.  * locating or creating the current session. 
  8.  */  
  9. 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。


2. 三种默认实现

1). JTASessionContext

 

Java代码  收藏代码
  1. /** 
  2.  * An implementation of {@link CurrentSessionContext} which scopes the notion 
  3.  * of a current session to a JTA transaction.  Because JTA gives us a nice 
  4.  * tie-in to clean up after ourselves, this implementation will generate 
  5.  * Sessions as needed provided a JTA transaction is in effect.  If a session 
  6.  * is not already associated with the current JTA transaction at the time 
  7.  * {@link #currentSession()} is called, a new session will be opened and it 
  8.  * will be associated with that JTA transaction. 
  9.  * <p/> 
  10.  * Note that the sessions returned from this method are automatically configured with 
  11.  * both the {@link org.hibernate.cfg.Environment#FLUSH_BEFORE_COMPLETION auto-flush} and 
  12.  * {@link org.hibernate.cfg.Environment#AUTO_CLOSE_SESSION auto-close} attributes set to 
  13.  * true, meaning that the Session will be automatically flushed and closed 
  14.  * as part of the lifecycle for the JTA transaction to which it is associated. 
  15.  * Additionally, it will also be configured to aggressively release JDBC 
  16.  * connections after each statement is executed.  These settings are governed 
  17.  * by the {@link #isAutoFlushEnabled()}, {@link #isAutoCloseEnabled()}, and 
  18.  * {@link #getConnectionReleaseMode()} methods; these are provided (along with 
  19.  * the {@link #buildOrObtainSession()} method) for easier subclassing for custom 
  20.  * JTA-based session tracking logic (like maybe long-session semantics). 
  21.  * 
  22.  * @author Steve Ebersole 
  23.  */  
  24. 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这种)的子类化。

 

2). ThreadLocalSessionContext

 

Java代码  收藏代码
  1. /** 
  2.  * A {@link CurrentSessionContext} impl which scopes the notion of current 
  3.  * session by the current thread of execution.  Unlike the JTA counterpart, 
  4.  * threads do not give us a nice hook to perform any type of cleanup making 
  5.  * it questionable for this impl to actually generate Session instances.  In 
  6.  * the interest of usability, it was decided to have this default impl 
  7.  * actually generate a session upon first request and then clean it up 
  8.  * after the {@link org.hibernate.Transaction} associated with that session 
  9.  * is committed/rolled-back.  In order for ensuring that happens, the sessions 
  10.  * generated here are unusable until after {@link Session#beginTransaction()} 
  11.  * has been called. If <tt>close()</tt> is called on a session managed by 
  12.  * this class, it will be automatically unbound. 
  13.  * <p/> 
  14.  * Additionally, the static {@link #bind} and {@link #unbind} methods are 
  15.  * provided to allow application code to explicitly control opening and 
  16.  * closing of these sessions.  This, with some from of interception, 
  17.  * is the preferred approach.  It also allows easy framework integration 
  18.  * and one possible approach for implementing long-sessions. 
  19.  * <p/> 
  20.  * The {@link #buildOrObtainSession}, {@link #isAutoCloseEnabled}, 
  21.  * {@link #isAutoFlushEnabled}, {@link #getConnectionReleaseMode}, and 
  22.  * {@link #buildCleanupSynch} methods are all provided to allow easy 
  23.  * subclassing (for long-running session scenarios, for example). 
  24.  * 
  25.  * @author Steve Ebersole 
  26.  */  
  27. 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与当前线程解除绑定。

 

3). ManagedSessionContext

 

Java代码  收藏代码
  1. /** 
  2.  * Represents a {@link CurrentSessionContext} the notion of a contextual session 
  3.  * is managed by some external entity (generally some form of interceptor, etc). 
  4.  * This external manager is responsible for scoping these contextual sessions 
  5.  * appropriately binding/unbinding them here for exposure to the application 
  6.  * through {@link SessionFactory#getCurrentSession} calls. 
  7.  * <p/> 
  8.  *  Basically exposes two interfaces.  <ul> 
  9.  * <li>First is the implementation of CurrentSessionContext which is then used 
  10.  * by the {@link SessionFactory#getCurrentSession()} calls.  This 
  11.  * portion is instance-based specific to the session factory owning the given 
  12.  * instance of this impl (there will be one instance of this per each session 
  13.  * factory using this strategy). 
  14.  * <li>Second is the externally facing methods {@link #hasBind}, {@link #bind}, 
  15.  * and {@link #unbind} used by the external thing to manage exposure of the 
  16.  * current session it is scoping.  This portion is static to allow easy 
  17.  * reference from that external thing. 
  18.  * </ul> 
  19.  * The underlying storage of the current sessions here is a static 
  20.  * {@link ThreadLocal}-based map where the sessions are keyed by the 
  21.  * the owning session factory. 
  22.  * 
  23.  * @author Steve Ebersole 
  24.  */  
  25. public class ManagedSessionContext implements CurrentSessionContext   

 

 

ManagedSessionContext是上下文会话由外部实体(一般指interceptor等)管理的一种实现。
外部管理器负责通过绑定和解除绑定(binding/unbinding)来合适地确定这些会话的范围,并提供SessionFactory的getCurrentSession方法给应用来调用。

 

主要提供了以下两个接口:

  • 第一是提供CurrentSessionContext的实现,这是在调用SessionFactory的getCurrentSession()方法时用到的[只有实现了会话的上下文,SessionFactory在调用getCurrentSession()时才会知道哪个session是当前的,才能返回它]。 这部分的实现是基于实例的,针对每个SessionFactory都会有一个指定的实现了CurrentSessionContext的实例(每个使用这种策略的session factory都会有一个CurrentSessionContext的实例)。
  • 第二是外部方法,包括hasBind,bind,unbind等。外部管理器用这些方法来管理它所拥有的session的暴露程度的。这一部分旨在允许通过对外部管理器提供方法的简单引用来管理session。

在这种实现方式下,session的底层存储方法是基于静态(ThreadLocal)map的,session们是value,而拥有这些session的session factory则是key。

 

 

 

使用ManagedSessionContext我们可定制的内容将更多。例如在session的关闭上,ThreadLocalSessionContext默认在事务结束之后就关闭当前session并解除绑定关系,而ManagedSessionContext默认不关闭session,你只能通过session的close(()方法和ManagedSessionContext.unbind()方法来关闭和解除绑定。