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.

最近在配置 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)

<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配置:

<!-- 事务的配置 -->

    <!-- 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时,启动后他默认是给没有配置事务边界的方法都默认为只读的,所以在插入数据时就会报上面的错

如果出现该错误,检查事务边界配置是否正确

原文地址:http://blog.csdn.net/enterys/article/details/7927840

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.

时间: 2024-12-17 16:50:09

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.的相关文章

spring整合之后运行报什么只读错误。Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove &#39;readOnly&#39; marker from transaction definition.

解决办法, 再大dao的实现类上添加注解: @Transactional(readOnly = false ) 不让它只读就行了 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 defi

Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove &#39;readOnl

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. 错误解决办法: <fi

Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushM

错误提示: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. 我在删除一条数据时出现了以上错误,网上查询的答案基本是说这个错误一般由事务引起的,有可能是项目事务配置文件不正确,但是我删除其它数据并没有出现这

(详解)Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session

昨天发现以前正常的功能报错了,错误日志如下: 报错日志: Hibernate: select taxtypecon0_.ID as ID62_, taxtypecon0_.TAX_TYPE_NAME as TAX2_62_, taxtypecon0_.EXPRESSIONS as EXPRESSI3_62_, taxtypecon0_.CREATE_DATE as CREATE4_62_, taxtypecon0_.CREATED_BY as CREATED5_62_, taxtypecon0_

保存数据报错Write operations are not allowed in read-only mode (FlushMode.MANUAL)

1. 报错信息 org.springframework.dao.InvalidDataAccessApiUsageException: 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. at org.sp

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

最近学习ssh框架,总是出现这问题,后查证是没有开启事务. 如果采用注解方式,直接在业务层加@Transactional 并引入import org.springframework.transaction.annotation.Transactional; 详细信息,可以参考下面博客: 参考资料:http://blog.csdn.net/longxia1987/article/details/7819242 org.springframework.dao.InvalidDataAccessApi

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

今天在做ssh项目开发的时候遇到一个问题,保存数据的时候报错: Write operations are not allowed in read-only mode (FlushMode.NEVER/ 后参考这篇文章才知道出了什么问题:http://www.aichengxu.com/view/37412 原来在Spring事务管理中save类的方法都没有read-only=true,但是我的方法是add*,所以就出现开头讲的问题,把add*方法改成save*就可以了. <!-- 配置事务策略

OpenSessionInViewFilter与org.springframework.dao.InvalidDataAccessApiUsageException

本文转载:Iuranus 报错: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 definit

ssh整合bug

异常: Org.hibernate.lazyinitializationException:could not initialize proxy –no session 这是延迟加载的一个问题,主要原因是service层在开启事务后经过Dao的查询的对象是代理的对象且没有实际参数,只有在使用时才会帮我初始化对象.此时事务提交后session被销毁,而web端得到的是该代理对象,如果此时去初始化对象时会抛出以上异常,因为该对象此时处于游离态. 解决: 不使用延迟加载 手动将延迟加载初始化 Hibe