(转+作者个人理解)

最近在配置 Structs, Spring 和Hibernate整合的问题:
开启OpenSessionInViewFilter来阻止延迟加载的错误的时候抛出了这个异常:
    org.springframework.dao.InvalidDataAccessApiUsageException错误
但是在我们开启OpenSessionInViewFilter这个过滤器的时候FlushMode就已经被默认设置为了MANUAL!
如果FlushMode是MANUAL或NEVEL,在操作过程中 hibernate会将事务设置为readonly,所以在增加、删除或修改操作过程中会出现如下错误:
org.springframework.dao.InvalidDataAccessApiUsageException:
Write operations are not allowed in read-only mode (FlushMode.NEVER) turn your Session into FlushMode.AUTO or remove 'readOnly' marker from transaction definition; 解决办法1:
    直接修改OpenSessionInViewFilter过滤器的配置,配置过滤器的时候配置就是在一般的配置里面加上下面蓝色部分就可以了,直接指定flushMode的配置就OK了:
下面是配置文件:(web.xml)
[html] view plaincopyprint?
<filter> <filter-name>OpenSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> <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> 解决方法2:
    就是配置事务的边界,在你方法的执行时配置事务边界!
下面是sessionFactor.xml配置:
[html] view plaincopyprint?
<!-- 事务的配置 --> <!-- sessionFactory 为自己配置 sessionFactory 的 bean--> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <aop:config> <!-- execution(public * *.*.*..*.*(..)) 为自己项目中操作数据库中的方法 --> <aop:pointcut id="**" expression="execution(public * *.*.*..*.*(..))" /> <aop:advisor pointcut-ref="**" advice-ref="txAdvice" /> </aop:config> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <!-- name 为 方法名 --> <tx:method name="**" read-only="true" /> <tx:method name="**" propagation="REQUIRED"/> </tx:attributes> </tx:advice> 下面是总结:
原理:因为配置openSessionInView时,启动后他默认是给没有配置 事务的加强处理(包括注解型事务) 的方法都默认为只读的,所以在插入数据时就会报上面的错

Write operations are not allowed in read-only mode错误的更多相关文章

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

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

  2. 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 ...

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

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

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

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

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

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

  6. 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 ...

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

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

  8. 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 ...

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

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

随机推荐

  1. Shell实现跳板机,为什么用跳板机

    整理自:http://blog.chinaunix.net/uid-22101889-id-3167454.html 注意:请谨慎使用,到现在为止,使用了,我还没找到改回去的方法. 1.     问题 ...

  2. 【BZOJ1878】[SDOI2009]HH的项链 离线BIT

    1878: [SDOI2009]HH的项链 Description HH有一串由各种漂亮的贝壳组成的项链.HH相信不同的贝壳会带来好运,所以每次散步 完后,他都会随意取出一段贝壳,思考它们所表达的含义 ...

  3. spring_150804_controller

    实体类: package com.spring.model; public class DogPet { private int id; private String name; private in ...

  4. JQuery的第一天实战学习

    1.按照下面的工程来建: 2.新建UserVerify.html文件: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitiona ...

  5. linq lambda 分组后排序

    1.lamdba分组排序foodBusinessDistrict.                        GroupBy(x => new                        ...

  6. YARN集群维护部分问题汇总

    云梯开发人员在云梯Yarn集群的搭建和维护过程中做了许多工作,本文选择这期间部分较为典型的问题,通过对这些问题的分析和解决方案,为大家分享分布式系统问题调查的经验. 调查的问题 1. 2013年初引入 ...

  7. java反射机制(基础版)

    package com.reflect; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import ja ...

  8. SVN与CVS的区别大全(转载)

    本节讲解SVN与CVS的区别,主要包括是否更好的冲突标识与处理,是否有更多的本地/离线操作以及元数据管理问题. 更好的冲突标识与处理     通过是否进行更好的冲突标识与处理看SVN与CVS的区别:C ...

  9. ios开发理解nil,Nil, NULL

    nil是一个对象指针为空,Nil是一个类指针为空,NULL是基本数据类型为空.这些可以理解为nil,Nil, NULL的区别吧. iOS剪切板 UIPasteboard *pasteboard = [ ...

  10. Data Flow ->> DQS Cleansing

    Data Quality Services(DQS)是SQL Server 2012引入的一大特性.这个服务的任务是为了实现客户端数据标准化和清理错误数据的.比如客户端数据容易因为用户输出诸如像城市名 ...