marshalljian
marshalljian
2018-06-24 12:08
采纳率: 100%
浏览 731
已采纳

在Spring集成Hibernate后,进行save操作报错

在进行save操作时抛出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.异常

在网上找了许久,全是千篇一律。基本上都是说在web.xml的过滤器加FlushMode,但是我这个只有Spring+Hibernate,哪来的web.xml。也有说加事务处理的,但是我加了事务,测试了无数遍,还是不行,请大神们帮忙下,谢谢,下面是我的xml配置

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<!-- Stock business object -->
<bean id="stockBo" class="com.yiibai.stock.bo.impl.StockBoImpl" >
    <property name="stockDao" ref="stockDao" />
</bean>

<bean id="stockDao" class="com.yiibai.stock.dao.impl.StockDaoImpl" >
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
    <property name="transactionManager" ref="transactionManager" />
    <property name="transactionAttributes">
        <props>
            <prop key="save">PROPAGATION_SUPPORTS</prop>  
        </props>
    </property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="dataSource" ref="dataSource" />
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="productBoProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="target" ref="stockDao" />
    <property name="interceptorNames">
        <list>
            <value>transactionInterceptor</value>
        </list>
    </property>
</bean>

下面是具体错误信息
Exception in thread "main" 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.springframework.orm.hibernate5.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1093)
at org.springframework.orm.hibernate5.HibernateTemplate.lambda$update$13(HibernateTemplate.java:658)
at org.springframework.orm.hibernate5.HibernateTemplate.doExecute(HibernateTemplate.java:383)
at org.springframework.orm.hibernate5.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:349)
at org.springframework.orm.hibernate5.HibernateTemplate.update(HibernateTemplate.java:657)
at org.springframework.orm.hibernate5.HibernateTemplate.update(HibernateTemplate.java:652)
at com.yiibai.stock.dao.impl.StockDaoImpl.update(StockDaoImpl.java:17)
at com.yiibai.stock.bo.impl.StockBoImpl.update(StockBoImpl.java:20)
at com.yiibai.common.App.main(App.java:28)

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • uniqueweimeijun
    uniquewdl 2018-06-24 13:38
    已采纳

    dao上加上@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW )

    点赞 评论
  • marshalljian
    marshalljian 2018-06-24 14:12

    谢谢uniqueweimeijun,除了加注解外,还需要在xml配置文件加配置,完整xml

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <!-- Stock business object -->
    <bean id="stockBo" class="com.yiibai.stock.bo.impl.StockBoImpl" >
        <property name="stockDao" ref="stockDao" />
    </bean>
    
    <bean id="stockDao" class="com.yiibai.stock.dao.impl.StockDaoImpl" >
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="dataSource" ref="dataSource" />
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    
    <tx:annotation-driven transaction-manager="transactionManager" />
    

    点赞 评论
  • marshalljian
    marshalljian 2018-06-24 14:23

    另外,其实我一开始的配置是可以的,只不过getBean的时候要使用代理bean,或者使用自动代理配置事务管理

    点赞 评论
  • a394149398
    叮当你去哪了 2018-06-25 08:54

    建议:我今天也遇到了这个问题,不建议将readOnly设为false,这样就相当于把事务去掉了,当然就不报错了
    原因:真正的原因在于你用没有事务的方法调用了有事务的方法,此时readOnly如果为true的话就会报这个错
    解决:把没有事务的方法加到事务里

    点赞 评论

相关推荐