Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
今天在做事务测试的时候,控制台输出了以上的错误信息,在网上找了一下,原来是我在web.xml
文件中设置了OpenSessionInView . 将设置去掉就可以了.
<!-- OpenSessionInView -->
<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>*.action</url-pattern>
</filter-mapping>
或者加上事务设置
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
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"
>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="create*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="find*" propagation="REQUIRED" read-only="true"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="daoOperation" expression="execution(* dgut.ke.dao.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="daoOperation"/>
</aop:config>
</beans>
网上又说
尽管Open Session In View看起来还不错,其实副作用不少。看回上面OpenSessionInViewFilter的doFilterInternal方法代码,这个方法实际上是被父类的doFilter调用的,因此,我们可以大约了解的OpenSessionInViewFilter调用流程: request(请求)->open session并开始transaction->controller->View(Jsp)->结束transaction并close session.
一切看起来很正确,尤其是在本地开发测试的时候没出现问题,但试想下如果流程中的某一步被阻塞的话,那在这期间connection就一直被占用而不释放。最有可能被阻塞的就是在写Jsp这步,一方面可能是页面内容大,response.write的时间长,另一方面可能是网速慢,服务器与用户间传输时间久。当大量这样的情况出现时,就有连接池连接不足,造成页面假死现象。
Open Session In View是个双刃剑,放在公网上内容多流量大的网站请慎用。
分享到:
相关推荐
Spring提供的CharacterEncoding和OpenSessionInView功能
介绍Spring+hibernate opensessioninview问题
为了练手培训,给大家准备的 Open Session In View 的简单例子,纯代码,大家可以参考,其中主要说了六部分内容: ...5.通过 spring aop(aspectJ) 声明事务 6.通过formular 映射参数表,指定两个死的变量
OpenSessionInView项目整合jar包
NULL 博文链接:https://8366.iteye.com/blog/472141
在没有使用Spring提供的Open Session In View情况下,因需要在service(or Dao)层里把session关闭,所以lazy loading 为true的话,要在应用层内把关系集合都初始化,如 company.getEmployees(),否则Hibernate抛...
SSH项目整合示例【OpenSessionInView】所用到的jar包 包含Struts + Hibernate + Spring所有jar及其依赖的jar
3. 在使用中如果遇到OpenSessionInView的问题,可以添加OpenSessionInViewFilter或OpenSessionInViewInterceptor。 五、Spring的事务管理 Spring框架提供了事务管理机制,提供了几个关于事务处理的类: 1. ...
NULL 博文链接:https://likenice.iteye.com/blog/290162
spring2.5学习PPT 传智博客 01_全面阐释Spring及其各项功能 ... 30.Spring提供的CharacterEncoding和OpenSessionInView功能 31.使用Spring集成JPA 32.Struts+Spring+JPA集成 33.使用Spring集成Struts2 34.所有源码
和Spring中OpenSessionInView由于org.springframework.web.struts.ContextLoaderPlugIn中保存同一个对象的名不同导致openSessionInView失效 稍微修改后在struts-config.xml中使用MyContextLoaderPlugIn.jar包中...
NULL 博文链接:https://yanzhenwei.iteye.com/blog/1701164
2,配置声明式事务(使用基于注解的方式) 1,配置 <!-- 配置事务管理器 --> <property name="sessionFactory" ref="sessionFactory"></property> <!-- 配置基于注解的事务支持--> 2,...
SSH项目整合示例【OpenSessionInView】源码
最新版SSH项目整合 实现了OpenSessionInView。 hibernate5.2.3+struts2.5.2+spring4.3.3
Spring的Ioc Spring的AOP , AspectJ Spring的事务管理 , 三大框架的整合 目录 1.1 Spring 框架学习路线:..........................................................................................................
使用 AOC 拦截器而不是过滤器进行 OpenSessionInView 处理 列出一个国家的城市和添加城市的可能性(bean 验证和自动检索 lat 和 lng 坐标) 安全 安全的 REST Web 服务 使用 CSRF 令牌 角度父状态的问题 有用的链接 ...