报错: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. 打开已存在 Android项目及常见的问题

    Eclipse 打开已存在 Android项目及常见的问题   1.  点击菜单“File”-- "Import",会弹出 Import 对话框:   2,  选择“General ...

  2. Android屏幕分辨率详解(VGA、HVGA、QVGA、WVGA、WQVGA)

    这些术语都是指屏幕的分辨率. VGA:Video Graphics Array,即:显示绘图矩阵,相当于640×480 像素: HVGA:Half-size VGA:即:VGA的一半,分辨率为480× ...

  3. 简洁判断一个byte中有多少位为1的bit?

    以下是Brian W. Kernighan公开的一个方法 unsigned bit_count(unsigned v) { unsigned int c; //置位总数累计 ; v; c++) { v ...

  4. Android_CntextMenu_example_textSize

    menu.xml <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item ...

  5. RESTful HTTP实践

    http://www.infoq.com/cn/articles/designing-restful-http-apps-roth 摘要: 本文对RESTful HTTP的基础原理做了一个概览,探讨了 ...

  6. 对于 NSLayoutConstraint 不执行动画的处理:

    在开发中  我们有时候需要改变某个空间的约束条件 也就是更改NSLayoutConstraint的值  (比如说我想在键盘顶部增加一个工具栏 让工具栏随着键盘的位置变化而变化  有一个动画效果)但是发 ...

  7. opencv里面CV_32FC1家族

    因为总是接触过这样一些#define里面的东西但是总也不知道是干什么用的.而且每看一次梦比一次. 对于这些东西到底是什么的简写根本就不能理解. 原意是跑一下这个例程的: cvRectangle( my ...

  8. C语言创建并使用dll

    利用C语言创建 利用 C++使用: 参见前面  利用C语言创建并使用lib 如法炮制创建 showDll Dll代码 __declspec(dllexport) double myDivision(i ...

  9. [转]win7 64位下android开发环境的搭建

    本文转自:http://www.cfanz.cn/index.php?c=article&a=read&id=65289 最近换了新电脑,装了win7 64位系统,安装了各种开发环境, ...

  10. C语言局部变量和全局变量问题汇总

    1.局部变量能否和全局变量重名? 答:能,局部会屏蔽全局.要用全局变量,需要使用"::" 局部变量可以与全局变量同名,在函数内引用这个变量时,会用到同名的局部变量,而不会用到全局变 ...