报错: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的更多相关文章

  1. javaEE-----org.springframework.dao.InvalidDataAccessApiUsageException: Write operation

    org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read ...

  2. org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode

    [spring]:org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowe ...

  3. org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [41] did not match expected type [java.lang.Integer (n/a)];

    题记:以前记录过一些自己遇到的BUG,这个行为,让我一看报错的提示信息就能定位到问题的所在,后来记得比较多了,好多是重复性的再加上比较忙就没有详细的记录了,今天的工作量比较小,就顺便记录一下,以便以后 ...

  4. org.springframework.dao.InvalidDataAccessApiUsageException:The given object has a null identifi的解决方案

    异常信息: org.springframework.dao.InvalidDataAccessApiUsageException: The given object has a null identi ...

  5. 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 ...

  6. 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 ...

  7. hibernate框架学习错误集锦-org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.MANUAL)

    最近学习ssh框架,总是出现这问题,后查证是没有开启事务. 如果采用注解方式,直接在业务层加@Transactional 并引入import org.springframework.transacti ...

  8. org.springframework.dao.InvalidDataAccessApiUsageException报错

    2018-01-09 18:12:29,980 [qtp1501019626-21] ERROR - 外部接口调用方法[TestController$$EnhancerBySpringCGLIB$$8 ...

  9. org.springframework.dao.InvalidDataAccessApiUsageException

    org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read ...

随机推荐

  1. android122 zhihuibeijing 新闻中心NewsCenterPager加载网络数据实现

    新闻中心NewsCenterPager.java package com.itheima.zhbj52.base.impl; import java.util.ArrayList; import an ...

  2. sscanf、strsep

    #include <stdio.h> #include <string.h> int main() { char token[] ="abdzxbcdefgh&quo ...

  3. Azure Powershell 创建 Internal Load Balancer

    Select-AzureSubscription -SubscriptionName "订阅名称" $serviceName="云服务名称" $ilbName= ...

  4. Java8的新特性

        Java 8主要新特性包括如下几点: 一.接口的默认方法和静态方法 Java 8版之前,接口只有抽象方法,而在Java 8,为接口新增了两种类型的方法. 第一种是默认方法:在Java 8中,可 ...

  5. 如何在Xilinx ISE中使用TCL提高工作效率

    http://wenku.baidu.com/link?url=jxtsPLGUlWwYuD8TtfWYYU_NhY5Qty3rx8ZDLCkINLe39JRGb90V5HoJhnkn9r_PQ6vZ ...

  6. SecureCRT 7.3.4 安装以及破解

    1-9为 SecureCRT 7.3.4 安装图解:10-13是 SecureCRT 7.3.4 破解图解,心急的朋友可以直接向下拉. 以下是百度百科对 SecureCRT 的介绍: SecureCR ...

  7. [转] sql数据类型 varchar与nvarchar的区别

    SQL Server提供两种数据类型来存储字符信息.在如何在SQL Server或应用程序中使用方面,这两种数据类型大致是一样的.差别在于nvarchar是用于存储处理数据库图表中多语言数据的Unic ...

  8. ubuntu修改grub背景

    grub背景由/etc/grub.d/05_debian_theme定义,修改图片只需要将图片文件放到/boot/grub,d/下即可, 修改颜色只需编辑/boot/grub.d/grub.cfg

  9. 阅读 Linux 内核源码

    阅读Linux Kernel Source Code 假如你在Linux系统下面阅读Linux内核源代码,那么需要准备一些工具. ①Linux的内核源码 内核源码的下载地址:Index of /pub ...

  10. XML, XPath, Xslt及解析/Parse

    XML及解析/Parse "Programming with libxml2 is like the thrilling embrace of an exotic stranger.&quo ...