`

spring 的OpenSessionInViewFilter简介

阅读更多

[转]假设在你的应用中Hibernate是通过spring 来管理它的session.如果在你的应用中没有使用OpenSessionInViewFilter或者OpenSessionInViewInterceptor。session会在transaction结束后关闭。
如果你采用了spring的声明式事务模式,它会对你的被代理对象的每一个方法进行事务包装(AOP的方式)。如下:

 <bean id="txProxyTemplate" abstract="true"
        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="transactionManager" ref="transactionManager"/>
        <property name="transactionAttributes">
            <props>
                <prop key="save*">PROPAGATION_REQUIRED</prop>
                <prop key="remove*">PROPAGATION_REQUIRED</prop>
                <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
            </props>
        </property>
    </bean>

    <bean id="manager" parent="txProxyTemplate">
        <property name="target">
            <bean class="org.appfuse.service.impl.BaseManager">
                <property name="dao" ref="dao"/>
            </bean>
        </property>
    </bean>
目标类org.appfuse.service.impl.BaseManager 的  save *方法的事务类型PROPAGATION_REQUIRED  ,remove* 方法的事务类型PROPAGATION_REQUIRED
其他的方法的事务类型是PROPAGATION_REQUIRED,readOnly。
所以给你的感觉是调用这个名为“manager”的bean的方法之后session就关掉了。
如果应用中使用了OpenSessionInViewFilter或者OpenSessionInViewInterceptor,所有打开的session会被保存在一个线程变量里。在线程退出前通过
OpenSessionInViewFilter或者OpenSessionInViewInterceptor断开这些session。 为什么这么做?这主要是为了实现Hibernate的延迟加载功能。基于一个请求
一个hibernate session的原则。

spring中对OpenSessionInViewFilter的描述如下:
它是一个Servlet2.3过滤器,用来把一个Hibernate Session和一次完整的请求过程对应的线程相绑定。目的是为了实现"Open Session in View"的模式。
例如: 它允许在事务提交之后延迟加载显示所需要的对象。

这个过滤器和 HibernateInterceptor 有点类似:它是通过线程实现的。无论是没有事务的应用,还是有业务层事务的应用(通过HibernateTransactionManager 或
JtaTransactionManager的方式实现)它都适用。在后一种情况下,事务会自动采用由这个filter绑定的Session来进行相关的操作以及根据实际情况完成提交操作。

警告: 如果在你的应用中,一次请求的过程中使用了单一的一个HIbernate Session,在这种情况下,采用这个filter会产生一些以前没遇到的问题。特别需要注意的是通过
Hibernate Session重新组织持久化对象之间关系的相关操作需要在请求的最开始进行。以免与已经加载的相同对象发生冲突。

或者,我们可以通过指定"singleSession"="false"的方式把这个过滤器调到延期关闭模式。这样在一次请求的过程中不会使用一个单一的Session.每一次数据访问或事务相关
操作都使用属于它自己的session(有点像不使用Open Session in View).这些session都被注册成延迟关闭模式,即使是在这一次的请求中它相关操作已经完成。

"一次请求一个session" 对于一级缓存而言很有效,但是这样可以带来副作用。例如在saveOrUpdate的时候或事物回滚之后,虽然它和“no Open Session in View”同样安全。
但是它却允许延迟加载。

它会在spring的web应用的上下文根中查找Session工厂。它也支持通过在web.xml中定义的“SessionFactoryBeanName”的init-param元素 指定的Session工厂对应的bean的
名字来查找session工厂。默认的bean的名字是"sessionFactory".他通过每一次请求查找一次SessionFactory的方式来避免由初始化顺序引起的问题(当使用ContextLoaderServlet
来集成spring的时候 ,spring 的应用上下文是在这个filter 之后才被初始化的)。

默认的情况下,这个filter 不会同步Hibernate Session.这是因为它认为这项工作是通过业务层的事务来完成的。而且HibernateAccessors 的FlushMode为FLUSH_EAGER.如果你
想让这个filter在请求完成以后同步session.你需要覆盖它的closeSession方法,在这个方法中在调用父类的关闭session操作之前同步session.此外你需要覆盖它的getSession()
方法。返回一个session它的FlushMode 不是默认的FlushMode.NEVER。需要注意的是getSession()和closeSession()方法只有在single session的模式中才被调用。

在myfaces的wiki里提供了OpenSessionInViewFilter的一个子类如下:
public class OpenSessionInViewFilter extends org.springframework.orm.hibernate3.support.OpenSessionInViewFilter {
      
        /**
         * we do a different flushmode than in the codebase
         * here
         */
        protected Session getSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException {
                Session session = SessionFactoryUtils.getSession(sessionFactory, true);
                session.setFlushMode(FlushMode.COMMIT);
                return session;
        }
        /**
         * we do an explicit flush here just in case
         * we do not have an automated flush
         */
        protected void closeSession(Session session, SessionFactory factory) {
                session.flush();
                super.closeSession(session, factory);
        }
}

OpenSessionInView 

另外,你不愿意你的DAO测试代码每次都打开关系Session,因此,我们一般会采用OpenSessionInView模式。

OpenSessionInViewFilter解决Web应用程序的问题

如果程序是在正常的Web程序中运行,那么Spring的OpenSessionInViewFilter能够解决问题,它:

 

protected void doFilterInternal(HttpServletRequest request, 
             HttpServletResponse response,
	     FilterChain filterChain) throws ServletException, IOException {
	SessionFactory sessionFactory = lookupSessionFactory();
	logger.debug("Opening Hibernate Session in OpenSessionInViewFilter");
	Session session = getSession(sessionFactory);
	TransactionSynchronizationManager.bindResource(sessionFactory, 
             new SessionHolder(session));
	try {
		filterChain.doFilter(request, response);
	}
	finally {
		TransactionSynchronizationManager.unbindResource(sessionFactory);
		logger.debug("Closing Hibernate Session in OpenSessionInViewFilter");
		closeSession(session, sessionFactory);
	}
}
可以看到,这个Filter在request开始之前,把sessionFactory绑定到TransactionSynchronizationManager,和这个SessionHolder相关。这个意味着所有request执行过程中将使用这个session。而在请求结束后,将和这个sessionFactory对应的session解绑,并且关闭Session。

为什么绑定以后,就可以防止每次不会新开一个Session呢?看看HibernateDaoSupport的情况:

public final void setSessionFactory(SessionFactory sessionFactory) {
    this.hibernateTemplate = new HibernateTemplate(sessionFactory);
  }
 protected final HibernateTemplate getHibernateTemplate() {
  return hibernateTemplate;
 }

我们的DAO将使用这个template进行操作:

public abstract class BaseHibernateObjectDao
	extends HibernateDaoSupport
	implements BaseObjectDao {

protected BaseEntityObject getByClassId(final long id) { BaseEntityObject obj = (BaseEntityObject) getHibernateTemplate() .execute(new HibernateCallback() {

public Object doInHibernate(Session session) throws HibernateException { return session.get(getPersistentClass(), new Long(id)); }

}); return obj; }

 

public void save(BaseEntityObject entity) { getHibernateTemplate().saveOrUpdate(entity); }

public void remove(BaseEntityObject entity) { try {

getHibernateTemplate().delete(entity); } catch (Exception e) { throw new FlexEnterpriseDataAccessException(e); } }

public void refresh(final BaseEntityObject entity) { getHibernateTemplate().execute(new HibernateCallback() {

public Object doInHibernate(Session session) throws HibernateException { session.refresh(entity); return null; }

}); }

public void replicate(final Object entity) { getHibernateTemplate().execute(new HibernateCallback() {

public Object doInHibernate(Session session) throws HibernateException { session.replicate(entity, ReplicationMode.OVERWRITE); return null; }

}); }

而HibernateTemplate试图每次在execute之前去获得Session,执行完就力争关闭Session
public Object execute(HibernateCallback action) throws DataAccessException {
	Session session = (!this.allowCreate ?
		SessionFactoryUtils.getSession(getSessionFactory(), 
                  false) :
		SessionFactoryUtils.getSession(getSessionFactory(),
                  getEntityInterceptor(),
                  getJdbcExceptionTranslator()));
	boolean existingTransaction =  
          TransactionSynchronizationManager.hasResource(getSessionFactory());
	if (!existingTransaction && getFlushMode() == FLUSH_NEVER) {
		session.setFlushMode(FlushMode.NEVER);
	}
	try {
		Object result = action.doInHibernate(session);
		flushIfNecessary(session, existingTransaction);
		return result;
	}
	catch (HibernateException ex) {
		throw convertHibernateAccessException(ex);
	}
	catch (SQLException ex) {
		throw convertJdbcAccessException(ex);
	}
	catch (RuntimeException ex) {
		// callback code threw application exception
		throw ex;
	}
	finally {
		SessionFactoryUtils.closeSessionIfNecessary(
                    session, getSessionFactory());
	}
}
而这个SessionFactoryUtils能否得到当前的session以及closeSessionIfNecessary是否真正关闭session,端取决于这个session是否用sessionHolder和这个sessionFactory在我们最开始提到的TransactionSynchronizationManager绑定。
public static void closeSessionIfNecessary(Session session, 
    SessionFactory sessionFactory)   
    throws CleanupFailureDataAccessException {
	if (session == null || 
	   TransactionSynchronizationManager.hasResource(sessionFactory)) {
		return;
	}
	logger.debug("Closing Hibernate session");
	try {
		session.close();
	}
	catch (JDBCException ex) {
		// SQLException underneath
		throw new CleanupFailureDataAccessException(
		"Cannot close Hibernate session", ex.getSQLException());
	}
	catch (HibernateException ex) {
		throw new CleanupFailureDataAccessException(
		"Cannot close Hibernate session", ex);
	}
}

HibernateInterceptor和OpenSessionInViewInterceptor的问题

使用同样的方法,这两个Interceptor可以用来解决问题。但是关键的不同之处在于,它们的力度只能定义在DAO或业务方法上,而不是在我们的Test方法上,除非我们把它们应用到TestCase的方法上,但你不大可能为TestCase去定义一个接口,然后把Interceptor应用到这个接口的某些方法上。直接使用HibernateTransactionManager也是一样的。因此,如果我们有这样的测试:

Category parentCategory  = new Category ();
	parentCategory.setName("parent");
	dao.save(parentCategory);

Category childCategory = new Category(); childCategory.setName("child");

parentCategory.addChild(childCategory); dao.save(childCategory);

Category savedParent = dao.getCategory("parent"); Category savedChild = (Category ) savedParent.getChildren().get(0); assertEquals(savedChild, childCategory);

将意味着两件事情:
  • 每次DAO执行都会启动一个session和关闭一个session
  • 如果我们定义了一个lazy的关系,那么最后的Category savedChild = (Category ) savedParent.getChildren().get(0);将会让hibernate报错。

解决方案

一种方法是对TestCase应用Interceptor或者TransactionManager,但这个恐怕会造成很多麻烦。除非是使用增强方式的AOP.我前期采用这种方法(Aspectwerkz),在Eclipse里面也跑得含好。

另一种方法是在TestCase的setup和teardown里面实现和Filter完全一样的处理,其他的TestCase都从这个TestCase继承,这种方法是我目前所使用的。

Jolestar补充:openSessionInView的配置方法:

   <filter>
        <filter-name>opensession</filter-name>
        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
        <init-param>
            <param-name>singleSession</param-name>
            <param-value>false</param-value>
        </init-param>
    </filter>

评论

相关推荐

    OpenSessionInViewFilter

    OpenSessionInViewFilter个人学习总结

    关于OpenSessionInViewFilter的学习

    OpenSessionInViewFilter是Spring框架中一个非常重要的组件,主要用于解决数据访问层(DAO)与视图层(View)之间的事务管理问题。在Web应用中,由于HTTP请求的无状态性,一次请求通常由多个Servlet过滤器、控制器和...

    马士兵Spring课堂笔记(超级详细版).pdf

    我们可以通过配置XML文件和注解来实现整合,并使用opensessionInviewfilter来解决会话问题。 Spring JDBC面向接口编程 本节讲解了如何使用Spring JDBC来实现面向接口编程。我们可以通过配置XML文件和注解来实现DAO...

    Spring面试题大全

    3. 在使用中如果遇到OpenSessionInView的问题,可以添加OpenSessionInViewFilter或OpenSessionInViewInterceptor。 五、Spring的事务管理 Spring框架提供了事务管理机制,提供了几个关于事务处理的类: 1. ...

    spring面试题大全.doc

    - **OpenSessionInView**:解决长时间 HTTP 请求导致的 Hibernate 会话问题,可以使用 OpenSessionInViewFilter 或 OpenSessionInViewInterceptor。 **5. Spring 事务管理** Spring 提供了两种事务管理方式: - *...

    spring_note.rar_inversion_spring concept

    Spring简介 Spring应用IOC/DI(重要) xml annotation Spring应用AOP(重要) xml annotation Struts2.1.6 + Spring2.5.6 + Hibernate3.3.2整合(重要) opensessionInviewfilter(记住,解决什么问题,怎么解决) ...

    spring面试题集

    当遇到OpenSessionInView问题时,可以使用OpenSessionInViewFilter或OpenSessionInViewInterceptor来解决。 Spring的事务管理提供了强大且灵活的机制,主要包括编程式和声明式两种方式: 1. 编程式事务管理:通过...

    struts+spring+hibernate整合

    Spring4.0、Struts2.3.15、Hibernate4.2.4、jQuery1.9.1涉及到了诸多开发时的细节:ModelDriven、Preparable 拦截器、编写自定义的类型转换器、Struts2 处理 Ajax、OpenSessionInViewFilter、迫切左外连接、Spring ...

    Struts2+Spring+hibernate中对action的单元测试环境搭建[总结].pdf

    解决这个问题的方法有两种:一种是在 web.xml 中使用 Spring 提供的 OpenSessionInViewFilter,另一种是在 application.xml 中配置 OpenSessionInViewFilter。前者通用,后者只能用于 SpringMVC 结构中。 最后,...

    Struts+Hibernate+Spring的基本流程

    在Web应用中,为了确保每次请求都能获得新的Session,通常会配置openSessionInViewFilter,保证数据操作的正确性。 整合SSH的步骤如下: 1. 引入SSH相关的jar包,确保版本兼容。Struts和Hibernate的jar包可以从IDE...

    Struts,Spring与hibernate集成

    10. **处理Hibernate延迟加载问题**:为了解决由于Session生命周期和HTTP请求生命周期不匹配导致的延迟加载问题,可以使用OpenSessionInViewFilter。这个过滤器确保Session在请求结束时才关闭,从而能正确处理延迟...

    Spring提供的CharacterEncoding和OpenSessionInView功能

    在处理Web应用时,Spring提供了一些关键特性,如`CharacterEncodingFilter`和`OpenSessionInViewFilter`,它们对于解决特定问题至关重要。 首先,让我们深入了解一下`CharacterEncodingFilter`。在Web应用中,字符...

    Struts+Hibernate+Spring的整合方法

    Struts、Hibernate和Spring是Java开发中常用的三个框架,它们分别负责表现层、持久层和业务层的管理。将这三个框架整合在一起可以实现一个高效、松耦合的企业级应用程序。 1. **Struts** 是一个MVC(Model-View-...

    java-spring面试题大全

    - 解决OpenSessionInView问题,可以使用OpenSessionInViewFilter或OpenSessionInViewInterceptor,确保在HTTP请求的整个生命周期中都保持数据库会话。 5. **Spring的事务管理**: - Spring提供了...

    Struts+hibernate+Spring的整合

    10. **延迟加载问题**:为了处理Hibernate的延迟加载问题,可以使用Spring的`OpenSessionInViewFilter`。这个过滤器确保在一次HTTP请求的整个生命周期内,Hibernate的Session保持打开状态,允许延迟加载在请求结束时...

    jar包(struts2.0.8+spring2.0+hibernate3.2)

    Struts2、Spring和Hibernate是Java开发中三个非常重要的框架,它们共同构成了经典的MVC(Model-View-Controller)架构的实现。这个压缩包“struts2.0.8+spring2.0+hibernate3.2”包含了这三个框架的特定版本,这将...

    spring3和hibernate4整合的lib库

    或者可以直接使用SessionFactory的代理,例如`OpenSessionInViewFilter`,在每个HTTP请求的生命周期内打开和关闭Session。 6. **实体管理**:将实体类作为Spring Bean管理,使用`@Entity`注解标记,配合`@Table`等...

    spring面试大全

    对于OpenSessionInView模式的问题,Spring 提供了OpenSessionInViewFilter或OpenSessionInViewInterceptor来解决。 7. **事务管理**:Spring 提供了两种事务管理方式:编程式和声明式。编程式事务管理使用...

Global site tag (gtag.js) - Google Analytics