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.问题:只读模式下(FlushMode.NEVER/MANUAL)写操作不被允许:把你的Session改成FlushMode.COMMIT/AUTO或者清除事务定义中的readOnly标记。

错误原因:

OpenSessionInViewFilter在getSession的时候,会把获取回来的session的flush
mode 设为FlushMode.NEVER。然后把该sessionFactory绑定到TransactionSynchronizationManager,使request的整个过程都使用同一个session,在请求过后再接除该sessionFactory的绑定,最后closeSessionIfNecessary根据该session是否已和transaction绑定来决定是否关闭session。在这个过程中,若HibernateTemplate
发现自当前session有不是readOnly的transaction,就会获取到FlushMode.AUTO
Session,使方法拥有写权限。也即是,如果有不是readOnly的transaction就可以由Flush.NEVER转为Flush.AUTO,拥有insert,update,delete操作权限,如果没有transaction,并且没有另外人为地设flush
model的话,则doFilter的整个过程都是Flush.NEVER。所以受transaction(声明式的事务)保护的方法有写权限,没受保护的则没有。

解决方法:

web.xml配置里添加
<filter>

<filter-name>OpenSessionInViewFilter</filter-name>

<filter-class>
   
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter

</filter-class>
  
<init-param>
   
<param-name>sessionFactoryBeanName</param-name>

<param-value>sessionFactory</param-value>

</init-param>
  
<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>/*</url-pattern>

</filter-mapping>

如果在交给spring 管理的情况下,在beans.xml
里的配置

<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"/> // 如果不把save
update delete都配置上,
   <tx:method
name="update*"
propagation="REQUIRED"/> //这些操作会无效

<tx:method
name="delete*" propagation="REQUIRED"/>
  </tx:attributes>

</tx:advice>

Write operations are not allowed in read-only mode 只读模式下(FlushMode.NEVER/MANUAL)写操作不允的更多相关文章

  1. Write operations are not allowed in read-only mode 只读模式下(FlushMode.NEVER/MANUAL)写操作不

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

  2. Write operations are not allowed in read-only mode (FlushMode.NEVER/

    今天在做ssh项目开发的时候遇到一个问题,保存数据的时候报错: Write operations are not allowed in read-only mode (FlushMode.NEVER/ ...

  3. Write operations are not allowed in read-only mode

    使用Spring提供的Open Session In View而引起Write operations are not allowed in read-only mode (FlushMode.NEVE ...

  4. 解决Hibernate Write operations are not allowed in read-only mode的方法

    错误信息: org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed i ...

  5. Write operations are not allowed in read-only mode错误

    (转+作者个人理解) 最近在配置 Structs, Spring 和Hibernate整合的问题: 开启OpenSessionInViewFilter来阻止延迟加载的错误的时候抛出了这个异常: org ...

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

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

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

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

  8. Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.

    Struts Problem Report Struts has detected an unhandled exception: Messages: Write operations are not ...

  9. spring整合问题分析之-Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.

    1.异常分析 Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into ...

随机推荐

  1. java程序设计

    IP地址计数器 原理:获取用户的IP地址,然后存入数据库,当再次访问时查询数据库是否存在该条数据,即可完成此程序 设计过程 创建一个连接数据库类:DB.java package com.count.O ...

  2. cas单点登录-CAS5.1.3 overlay服务器搭建(二)

    前言    本节主要讲解怎么搭建cas服务端,并且在浏览器中使用https访问cas服务端 1.通过cas代码生成工具(https://casinitializr.herokuapp.com/),生成 ...

  3. PHP只显示姓名首尾字符,隐藏中间字符并用*替换

    //测试时文件的编码方式要是UTF8 $str='中文a字1符'; echo strlen($str).'<br>';//14 echo mb_strlen($str,'utf8').'& ...

  4. CSS标签类型和样式表继承与优先级

    标签类型 块级标签 什么是块级标签:在html中<div>. <p>.h1~h6.<form>.<ul> 和 <li>就是块级元素 块级标签 ...

  5. C语言 · 字串逆序

    算法训练 字串逆序   时间限制:1.0s   内存限制:512.0MB      问题描述 给定一个字符串,将这个串的所有字母逆序后输出. 输入格式 输入包含一个字符串,长度不超过100,字符串中不 ...

  6. [动态库]动态库生成和使用以及Makefile编写

    转自:https://www.cnblogs.com/ljtknowns/p/5647793.html 文件目录结构如下 1 dynamiclibapp.c 2 Makefile 3 comm/inc ...

  7. sed——linux下对文本当控制操作(替换,追加)

    sed——linux下对文本当控制操作(替换,追加) 2011-12-12 19:27:17 分类: LINUX Linux下sed命令 1. Sed简介 sed 是一种在线编辑器,它一次处理一行内容 ...

  8. VMware下的Centos7联网并设置固定IP

    安装CentOS7之后总得联网呀,而且不能一直连服务器,我们需要一个其他工具连它,所以一个固定IP也很重要了. 工具/原料   CentOS7 VMware 方法/步骤     首先设置vmware能 ...

  9. react学习资源

    http://www.ruanyifeng.com/blog/2015/03/react.html http://www.ruanyifeng.com/blog/2015/02/future-of-d ...

  10. Linux用户及用户组管理命令

    一,组操作 1.创建组 groupadd  test 增加一个test组 2.修改组 groupmod -n test2  test 将test组的名子改成test2  3.删除组 groupdel ...