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. spring 强制采用cglib进行代理

    spring对AOP的支持 *如果目标对象实现了接口,默认情况下会采用JDK的动态代理实现AOP* 如果目标对象实现了接口,可以强制使用CGLIB实现AOP*如果目标对象没有实现了接口,必须采用CGL ...

  2. Android创建并响应选项菜单

    创建options menu 之前提到,Android的activity已经为我们提前创建好了android.view.Menu对象,并提供了回调方法onCreateOptionsMenu(Menu  ...

  3. C盘空间不够,清除VS下的 Font Cache

    C盘空间老是不够用.清除Font Cache 1.在 C:\Users\Jimmy\AppData\Local\Microsoft\Visual Studio 下的  Font Cache 目录可以干 ...

  4. python 字符串格式化转换类型

  5. c字符输出

    #include<stdio.h> int main() { char c1,c2; c1 = ; c2 = ; printf("c1 = %c,c2 = %c\n", ...

  6. jQuery实现radio第一次点击选中第二次点击取消功能(转)

    转载自:http://www.jb51.net/article/113730.htm 由于项目的需求,要求radio点击两次后为取消状态,不方便修改为checkbox,可以用正面的方法实现. // j ...

  7. maven jar包上传到服务器

    maven jar包上传到服务器时出现pom文件没有上传上去,致使该jar包再被使用的时候没有依赖,jar包调用出错 解决办法,将pom文件一起deploy上去 mvn deploy:deploy-f ...

  8. STM32F10x_ADC三通道DMA连续转换(3通道、软件单次触发)

    Ⅰ.概述 上一篇文章讲述的内容是:三通道逐次转换(单次.单通道软件触发),也就是说3条通道要三次软件触发才能完成转换,而且是通过软件读取转换数值. 本文讲述三通道DMA连续转换(3通道.软件单次触发) ...

  9. sql逻辑查询 理论知识

    参考并转载http://www.cnblogs.com/bhtfg538/archive/2008/11/25/1341016.html First: (8) SELECT (9) DISTINCT  ...

  10. Repeater 嵌套,子级Repeater获取 父级Repeater 中的值

    第一种方法,子级Repeater中绑定父级的某个字段: <%# DataBinder.Eval((Container.NamingContainer.NamingContainer as Rep ...