`

关于spring延长hibernate中session的生命周期:OpenSessionInViewFilter

阅读更多

在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就可以了
具体如下:
<filter>
    <filter-name>openSessionInViewFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>openSessionInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

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

分享到:
评论

相关推荐

    struts+spring+hibernate整合

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

    OpenSessionInViewFilter

    OpenSessionInViewFilter个人学习总结

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

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

    关于OpenSessionInViewFilter的学习

    NULL 博文链接:https://yanzhenwei.iteye.com/blog/1701164

    spring_note.rar_inversion_spring concept

    课程内容 面向接口(抽象)编程的概念与好处 IOC/DI的概念与好处 ...Struts2.1.6 + Spring2.5.6 + Hibernate3.3.2整合(重要) opensessionInviewfilter(记住,解决什么问题,怎么解决) Spring JDBC

    spring_demo:Spring MVC示范项目

    Spring MVC Hibernate Demo Hibernate 配置 数据库实体必须设置以下注解 @Entity ... &lt;filter&gt;org.springframework.orm.hibernate4.support.OpenSessionInViewFilter &lt;param&gt;flushMode&lt;/param-nam

    SPRING API 2.0.CHM

    OpenSessionInViewFilter OpenSessionInViewFilter OpenSessionInViewInterceptor OpenSessionInViewInterceptor OptimisticLockingFailureException OptionsTag OptionTag OptionWriter OracleLobHandler ...

    OA项目SSH整合框架

    &lt;property name="configLocation" value="classpath:hibernate.cfg.xml"&gt; ---------------------- jdbc.properties ------------------------ jdbcUrl = jdbc:mysql:///itcastoa driverClass = ...

    SSH项目整合示例【OpenSessionInView】所用到的jar包

    SSH项目整合示例【OpenSessionInView】所用到的jar包 包含Struts + Hibernate + Spring所有jar及其依赖的jar

    Sping 事务管理.doc

    OpenSessionInViewFilter解决Web应用程序的问题

    Mac Mysql数据库中文乱码问题解决

    如:在使用Java中得SSH框架时,我们需要在web.xml文件中配置编码的filter,具体代码是: &lt;span xss=removed&gt;&lt;!-- 表单处理乱码,必须在OpenSessionInViewFilter的filter之前 --&gt; &lt;filter&gt;CharacterFilter ...

    S2SH集成 案例

    该案例实现了一个简单的登录功能,但里面将S2SH集成的所有配置信息都添加进去了。 如,OpenSessionInViewFilter、声明式事务、三层等等

Global site tag (gtag.js) - Google Analytics