报错: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. 文件I/O(不带缓冲)之creat函数

    本篇博文内容摘自<UNIX环境高级编程>(第二版),仅作个人学习记录所用.关于本书可参考:http://www.apuebook.com/. 也可调用creat函数创建一个新文件. #in ...

  2. 在javascript中关于submit和button提交表单区别

    submit是button的一个特例,也是button的一种,它把提交这个动作自动集成了,submit和button,二者都以按钮的形式展现,看起来都是按钮,所不同的是type属性和处发响应的事件上. ...

  3. JavaScript中的各种宽高以及位置总结

    JavaScript中的各种宽高以及位置总结 在javascript中操作dom节点让其运动的时候,常常会涉及到各种宽高以及位置坐标等概念,如果不能很好地理解这些属性所代表的意义,就不能理解js的运动 ...

  4. android开发之路04(初级android工程师必会,你懂得!)

    Android初级Android工程师重点掌握内容如下: 1.Android开发基础: ①UI界面设计: ②SQLite数据库: ③android四大组件: ④android网络编程: ⑤androi ...

  5. URL格式编码与解码

    char* urlencode(const void* buf, size_t size) { _assert_(buf && size <= MEMMAXSIZ); const ...

  6. Centos7 安装mongodb3.2.9 过程

    1:wget --no-check-certificate  https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-amazon-3.2.9.tg ...

  7. Bootloader简介

    来介绍一下Bootloader,在专用的嵌入式开发板上运行GNU/Linux 系统已经变得越来越流行.一个嵌入式Linux 系统从软件的角度看通常可以分为四个层次: 1.引导加载程序.包括固化在固件( ...

  8. .NET学习笔记(2) — IIS服务器环境搭建

    目录 一:开启Windows系统自带的IIS服务器方法 二:备注 三:常见问题     一:开启Windows系统自带的IIS服务器方法   第一步:安装IIS,控制面板->程序和功能-> ...

  9. C#JSON格式数据的转换

    json格式字符串转化为json对象:JObject calculate = (JObject)JsonConvert.DeserializeObject(Rep.Request["data ...

  10. mac 查看系统时区

    sudo systemsetup -gettimezone https://developer.apple.com/library/mac/documentation/Darwin/Reference ...