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. Zabiix 监控图形乱码问题

    Zabiix切换为中文 配置中文乱码问题 在C:\Windows\Fonts中复制想要的字体,后缀为ttf,若本身问大写,请改成小写的文件后缀ttf,并上传至zabbix服务器的/usr/local/ ...

  2. 20145109 《Java程序设计》第四周学习总结

    20145109 <Java程序设计>第四周学习总结 教材学习内容总结 Chapter 6 Inheritance & Polymorphism What is Inheritan ...

  3. Zookeeper初步了解

    Zookeeper初步了解: Zookeeper实现了许多复杂的事情,例如实现了Zookeeper Atomic Broadcasting Protocal来广播状态信息的变化,Fast Paxas ...

  4. C3p0的参数

    C3p0的参数设置:ComboPooledDataSource和BasicDataSource一样提供了一个用于关闭数据源的close()方法,这样我们就可以保证Spring容器关闭时数据源能够成功释 ...

  5. quartz(8)--其他

    JOB并发执行 Quartz定时任务默认都是并发执行的,不会等待上一次任务执行完毕,只要间隔时间到就会执行, 如果定时任执行太长,会长时间占用资源,导致其它任务堵塞. 设置为非并发 1)Job类加上注 ...

  6. mysql入门语句

    连接 1.mysql -h localhost -u root -p ******(回车) 2.mysql -h localhost -u root -p(回车) ****** 3.mysql -u ...

  7. PAT1076. Forwards on Weibo (30)

    使用DFS出现超时,改成bfs DFS #include <iostream> #include <vector> #include <set> using nam ...

  8. 读取和修改xml

    如有一个xml文件DownData.xml,内容如下 <?xml version="1.0" standalone="yes"?> <Root ...

  9. Pandas日期功能

    日期功能扩展了时间序列,在财务数据分析中起主要作用.在处理日期数据的同时,我们经常会遇到以下情况 - 生成日期序列 将日期序列转换为不同的频率 创建一个日期范围 通过指定周期和频率,使用date.ra ...

  10. python脚本3_输入若干个整数打印出最大值

    #输入若干个整数,打印出最大值 # m = int(input('Input first number >>>')) while True: c = input('Input a n ...