OpenSessionInViewFilter与org.springframework.dao.InvalidDataAccessApiUsageException
报错:org.springframework.dao.InvalidDataAccessApiUsageException: 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.
搞开发的时候碰到这个问题,在网上搜了一下原因,归纳成以下几个知识点,部分摘抄网络原文:
1、延迟加载:
Hibernate允许关联对象进行延迟加载,前提是必须保证延迟加载操作是在同一个Session范围之内进行。如果Service层返回了 一个已启用延迟加载的领域对象给View层,当View层访问那些需要延迟加载的数据时,由于加载领域对象的Session已经关闭,将导致延迟加载数据 的访问异常(org.hibernate.LazyInitializationException)。
2、OpenSessionInViewFilter:
总所周知,Java类或者方法命名以长著称,OpenSessionInViewFilter也是,顾名思义,它能够让我们在View层保持 Session继续Open。Spring提供的OpenSessionInViewFilter用来把一个Hibernate Session和一次完整的请求过程对应的线程相绑定(整个request过程都是用同一个Session,在请求结束后再解除绑定),允许在事务提交之 后延迟加载View层需要的对象。在绑定过程中,它将自动被Spring的事务管理器探测到,所以,OpenSessionInViewFilter 适用于Service层使用HibernateTransactionManager或JtaTransactionManager进行事务管理的环境, 也可以用于非事务只读的数据操作中。
“OpenSessionInViewFilter在getSession的时 候,会把获取回来的session的flush mode设为FlushMode.NEVER。然后把该sessionFactory绑定到 TransactionSynchronizationManager,使request的整个过程都使用同一个session,在请求过后再解除该 sessionFactory的绑定,最后closeSessionIfNecessary根据该 session是否已和transaction绑定来决定是否关闭session。在这个过程中,若HibernateTemplate 发现自当前session有不是readOnly的transaction,就会获取到FlushMode.AUTO Session,使方法拥有写权限。”
3、解决办法:
web.xml中配置OpenSessionInViewFilter初始参数:singleSession:true、flushMode:AUTO
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>flushMode</param-name>
<param-value>AUTO</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
或者在Spring配置文件中,将方法的read-only设置为false。我用的是注解,为图方便,将方法上的@Transactional(readOnly = true)去掉即可。
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean> <aop:config>
<aop:pointcut id="bussinessService" expression="execution(* com.fan.service.base.*.*(..))" />
<aop:advisor pointcut-ref="bussinessService" advice-ref="txAdvice" />
</aop:config> <tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="false" propagation="NOT_SUPPORTED"/>
<tx:method name="find*" read-only="false" propagation="NOT_SUPPORTED"/>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
4、备注:
“尽 管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是个双刃剑,放在公网上内容多流量大的网站请慎用。”
OpenSessionInViewFilter与org.springframework.dao.InvalidDataAccessApiUsageException的更多相关文章
- javaEE-----org.springframework.dao.InvalidDataAccessApiUsageException: Write operation
org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read ...
- org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode
[spring]:org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowe ...
- org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [41] did not match expected type [java.lang.Integer (n/a)];
题记:以前记录过一些自己遇到的BUG,这个行为,让我一看报错的提示信息就能定位到问题的所在,后来记得比较多了,好多是重复性的再加上比较忙就没有详细的记录了,今天的工作量比较小,就顺便记录一下,以便以后 ...
- org.springframework.dao.InvalidDataAccessApiUsageException:The given object has a null identifi的解决方案
异常信息: org.springframework.dao.InvalidDataAccessApiUsageException: The given object has a null identi ...
- org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist: sys.entity.Role; nested exception is org.hibernate.PersistentObjectException: 的解决方案
1.错误信息 org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist ...
- Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: Multiple representations of the same entity解决方法
1.错误信息 Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUs ...
- hibernate框架学习错误集锦-org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.MANUAL)
最近学习ssh框架,总是出现这问题,后查证是没有开启事务. 如果采用注解方式,直接在业务层加@Transactional 并引入import org.springframework.transacti ...
- org.springframework.dao.InvalidDataAccessApiUsageException报错
2018-01-09 18:12:29,980 [qtp1501019626-21] ERROR - 外部接口调用方法[TestController$$EnhancerBySpringCGLIB$$8 ...
- org.springframework.dao.InvalidDataAccessApiUsageException
org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read ...
随机推荐
- iOS开发——适配篇&iOS9适配
iOS9适配 1. Demo1_iOS9网络适配_ATS:改用更安全的HTTPS [摘要]iOS9把所有的http请求都改为https了:iOS9系统发送的网络请求将统一使用TLS 1.2 SSL.采 ...
- CSDN上下载的一些关于Android程序调用Webservice执行不成功的问题
今天从书上和CSDN上找了几个关于android调用webservice的样例,这些样例从代码来看.没不论什么错误,可是就是执行不成功.分析了android调用web接口的写法,发现这些样例在调用的时 ...
- windows平台下php版本问题–VC6/VC9和TS/NTS
php下载页面中提供了4个下载版本,是vc6/vc9 与 TS/NTS的组合 VC6:legacy Visual Studio 6 compiler,就是使用这个编译器编译的. VC9: ...
- 未打开Ad Hoc Distributed Queries
SSAS访问ORACLE数据仓库读取数据创建CUBE的时候报如下错误: SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT 'Open ...
- js jquery 等的地址
jquery在线地址(jquery地址):http://code.jquery.com/jquery-latest.js js人脉图(关系图)插件: http://js.cytoscape.org/
- 使用AndroidScreenSlidePager开源库
一.下载地址 https://github.com/LyndonChin/AndroidScreenSlidePager 点击右侧的Download ZIp按钮进行下载.然后解压缩到本地. 二.使用方 ...
- 轻松学习Ionic (一) 搭建开发环境,并创建工程
1.准备工作 下载 Node.js(下载包),WebStorm(IDE,编写代码,浏览器调试),JDK(webstorm 运行环境),Android SDK (Android编译) 不 ...
- Map集合的两种遍历方式
Map集合:即 接口Map<K,V> map集合的两种取出方式: 1.Set<k> keyset: 将map中所有的键存入到set集合(即将所有的key值存入到set中) ...
- 有关line-height的见解
line-height:简单的说就是行高,是两行文字之间基线的距离.基线是指在英语的书写的4线3格中,从上往下数的第三条线 1.line-height与行内框盒子模型 所有内联元素的样式表现都与行内框 ...
- 关于数据导出到Excel科学计数法的处理
SELECT '=T("'+字段+'")' from table 在这里在显示的字段内容前加了 '=T("',在后面也加了'")'.在这这里T()是Exc ...