---
title: "关于spring延长hibernate中session的生命周期：OpenSessionInViewFilter  "
author: "brilliance2011"
date: "2013-10-23T17:22:09Z"
tags: []
categories: ["Web前端"]
slug: "spring-hibernate-session-opensessioninviewfilter"
draft: false
lastmod: "2013-10-23T17:22:09Z"
id: 1963306
url: "https://brilliance2011.iteye.com/blog/1963306"
---

在HIBERNATE中,如果我们要访问到某个类的集合属性,那我们一般在类的的映射文件里SET节点启用LAZE=FALSE;  
那么在S2SH中怎么解决呢?  
1.OpenSessionInViewFilter是Spring提供的一个针对Hibernate的一个支持类，其主要意思是在发起一个页面请求时打开Hibernate的Session，  
一直保持这个Session，直到这个请求结束，具体是通过一个Filter来实现的。  
2.由于Hibernate引入了Lazy Load特性，使得脱离Hibernate的Session周期的对象如果再想通过getter方法取到其关联对象的值，  
Hibernate会抛出一个LazyLoad的Exception。所以为了解决这个问题，Spring引入了这个Filter，使得Hibernate的Session的生命周期变长。  
解决方法:  
修改WEB.XML !!!  
在WEB.XML中加入OpenSessionInView 的FILETER就可以了  
具体如下:  
&lt;filter&gt;  
    &lt;filter-name&gt;openSessionInViewFilter&lt;/filter-name&gt;  
    &lt;filter-class&gt;org.springframework.orm.hibernate3.support.OpenSessionInViewFilter&lt;/filter-class&gt;  
&lt;/filter&gt;  
&lt;filter-mapping&gt;  
    &lt;filter-name&gt;openSessionInViewFilter&lt;/filter-name&gt;  
    &lt;url-pattern&gt;/\*&lt;/url-pattern&gt;  
&lt;/filter-mapping&gt;

PS:老郭给你们的忠告!  
1.在加入这个FILETER的时候,一定一定,千万千万记得放在STRUTS2的核心控制器的位置的上面!切记!  
2.如果你的SPRING的配置文件中SESSIONFACTORY的BEAN ID默认不是sessionFactory的话,你的openSessionInView要改成这样.  
&lt;filter&gt;  
    &lt;filter-name&gt;openSessionInViewFilter&lt;/filter-name&gt;  
    &lt;filter-class&gt;org.springframework.orm.hibernate3.support.OpenSessionInViewFilter&lt;/filter-class&gt;  
    &lt;init-param&gt;  
        &lt;param-name&gt;sessionFactoryBeanName&lt;/param-name&gt;  
        &lt;param-value&gt;xxxx&lt;/param-value&gt; //也就是你的SPRING配置文件中SESSIONFACTORY的名字  
    &lt;/init-param&gt;  
&lt;/filter&gt;  
&lt;filter-mapping&gt;  
    &lt;filter-name&gt;openSessionInViewFilter&lt;/filter-name&gt;  
    &lt;url-pattern&gt;/\*&lt;/url-pattern&gt;  
&lt;/filter-mapping&gt;  
3.当你把OPENSESSIONINVIEW配置上后,你就可以让SESSION晚点关闭的.但是又一个新的问题产生了.什么问题呢?自己可以试试,如果这个时候你的SPRING  
没有配置事务的话,那么你所有的更新操作都是不允许的.  
所以要在SPRING里把需要更新的SERVICE操作都配置事务.  
配置如下:  
首先在SPRING配置文件中加入XML命名空间:  
xmlns:tx="[http://www.springframework.org/schema/tx](http://www.springframework.org/schema/tx)"  
xmlns:aop="[http://www.springframework.org/schema/aop](http://www.springframework.org/schema/aop)"  
方案地址:  
xsi:schemaLocation里加上  
[http://www.springframework.org/schema/tx](http://www.springframework.org/schema/tx) [http://www.springframework.org/schema/tx/spring-tx-2.0.xsd](http://www.springframework.org/schema/tx/spring-tx-2.0.xsd)  
[http://www.springframework.org/schema/aop](http://www.springframework.org/schema/aop) [http://www.springframework.org/schema/aop/spring-aop-2.0.xsd](http://www.springframework.org/schema/aop/spring-aop-2.0.xsd)"  
前提是你配置了AOP的命名空间和方案地址  
&lt;!-- 事务配置 --&gt;  
&lt;!--Step1.事务管理器配置--&gt;  
&lt;bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;  
    &lt;property name="sessionFactory" ref="sessionFactory" /&gt;  
    &lt;/bean&gt;  
    &lt;!--Step2.交给SPRING事务管理的一些方法.--&gt;  
&lt;aop:config&gt;  
&lt;aop:pointcut id="productServiceMethods"  
            &lt;!-- 指定切面是哪个范围类,比如你项目的SERVICE的所有方法--&gt;  
expression="execution(* com.公司名.项目名.service..\*.\*(..))" /&gt;  
&lt;!-- 一个通知的集合,这个集合都用上的POINTCUT--&gt;  
&lt;aop:advisor advice-ref="txAdvice"  
pointcut-ref="productServiceMethods" /&gt;  
&lt;/aop:config&gt;  
&lt;!--定义通知集合,以TX开头,是有事务的 --&gt;  
&lt;tx:advice id="txAdvice" transaction-manager="txManager"&gt;  
&lt;tx:attributes&gt;  
&lt;!--所有的以ADD开头的方法名的方法都用事务  REQUIRED 表示,比如方法A有更新操作,A中调用了B方法,那么B到底是重新起一个事务还是用A方法的事务,  
REQUIRED标识不起就用当前事务,如果有就用A的,如果没有就起一个--&gt;  
&lt;tx:method name="add\*" propagation="REQUIRED" /&gt;  
&lt;tx:method name="delete\*" propagation="REQUIRED" /&gt;  
&lt;tx:method name="update\*" propagation="REQUIRED" /&gt;  
&lt;!--加入只读,说明以GET或者LOAD开头的方法名的方法都是只读事务,说明这个方法内没有UPDATE操作,这样声明下可以提高该查询方法的效率 --&gt;  
&lt;tx:method name="get\*" propagation="REQUIRED"  
read-only="true" /&gt;  
&lt;tx:method name="load\*" propagation="REQUIRED"  
read-only="true" /&gt;  
&lt;/tx:attributes&gt;  
&lt;/tx:advice&gt;