(转+作者个人理解)

最近在配置 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. Visual Studio 自定义控件不显示在工具箱

    工具=>选项=>Windows窗体设计器=>常规=>AutoToolBoxPopulate=>True

  2. MyEclipse — Maven+Spring+Struts+Hibernate 整合 [学习笔记-4]

    引入Hibernate 在pom.xml中加入jar包引用 <!-- hibernate4 --> <dependency> <groupId>org.hibern ...

  3. http://blog.csdn.net/wxwzy738/article/details/16968767

    http://blog.csdn.net/wxwzy738/article/details/16968767

  4. lintcode :同构字符串

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  5. Dreamweaver修改页面编码

    想修改当前页面编码,可以选择菜单 :修改->页面属性->标题/编码 想设置新建页面默认编码,可以选择菜单: 编辑->首选参数->新建文档 的默认编码里面修改

  6. photoshop菜单显示不全的解决方法

    photoshop菜单显示不全? 解决方法,选择菜单 编辑->菜单,下拉菜单选择photoshop默认值

  7. iOS 消息转发

    消息转发   delegate和protocol   类别      消息转发   当向someObject发送某消息,但runtime system在当前类和父类中都找不到对应方法的实现时,runt ...

  8. Jenkins 构建JavaHelloWorld

    原文:http://www.cnblogs.com/itech/archive/2011/11/03/2234662.html 注意:我们知道Jenkins通过master/slave来支持分布式的j ...

  9. Spring AOP 创建增强类

    AOP联盟为增强定义了org.aopalliance.aop.Advice接口,Spring支持5种类型的增强:     1)前置增强:org.springframework.aop.BeforeAd ...

  10. php post和get

    作为一个计算机系统,输入输出设备作为非核心设备却是不可或缺的,硬件如此,软件亦是如此.试想一台功能强劲的计算机,如果没有输入输出设备,它与一块只能耗电并且发出嗡嗡噪音的废铁有何不同.应用程序的道理也是 ...