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

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

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

org.springframework.orm.hibernate3.HibernateTemplate.checkWriteOper

ationAllowed(HibernateTemplate.java:1095)

问题只读模式下(FlushMode.NEVER/MANUAL)写操作不被允许:把你的Session改成FlushMode.COMMIT/AUTO或者清除事务定义中的readOnly标记。

 

错误原因:
         OpenSessionInViewFilter在getSession的时候,会把获取回来的session的flushmode 设为FlushMode.NEVER。然后把该sessionFactory绑定到TransactionSynchronizationManager,使request的整个过程都使用同一个session,在请求过后再接除该sessionFactory的绑定,最后closeSessionIfNecessary根据该session是否已和transaction绑定来决定是否关闭session。在这个过程中,若HibernateTemplate发现自当前session有不是readOnly的transaction,就会获取到FlushMode.AUTOSession,使方法拥有写权限。也即是,如果有不是readOnly的transaction就可以由Flush.NEVER转为Flush.AUTO,拥有insert,update,delete操作权限,如果没有transaction,并且没有另外人为地设flushmodel的话,则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里的配置

 <beanid="txManager"
  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <propertyname="sessionFactory" ref="sessionFactory" />
 </bean>

 <aop:config>
  <aop:pointcutid="bussinessService"
   expression="execution(*com.fan.service.base.*.*(..))" />
  <aop:advisorpointcut-ref="bussinessService"
   advice-ref="txAdvice"/>
 </aop:config>

 <tx:adviceid="txAdvice" transaction-manager="txManager">
  <tx:attributes>
   <tx:methodname="get*" read-only="false"propagation="NOT_SUPPORTED"/>
   <tx:methodname="find*" read-only="false"propagation="NOT_SUPPORTED"/>
   <tx:methodname="save*" propagation="REQUIRED"/> // 如果不把saveupdate delete都配置上,
   <tx:methodname="update*"propagation="REQUIRED"/> //这些操作会无效
   <tx:methodname="delete*" propagation="REQUIRED"/>
  </tx:attributes>
 </tx:advice>



--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

这个异常产生的主要原因是DAO采用了Spring容器的事务管理策略,如果操作方法的名称和事务策略中指定的被管理的名称不能够匹配上,spring 就会采取默认的事务管理策略(PROPAGATION_REQUIRED,read only).如果是插入和修改操作,就不被允许的,所以包这个异常




一般这个错误是事务引起的,如果确定事务没有问题,还是有这个错,可以重写OpenSessionInViewFilter的2个方法

在myfaces的wiki里提供了OpenSessionInViewFilter的一个子类如下:
public class OpenSessionInViewFilter extends org.springframework.orm.hibernate3.support.OpenSessionInViewFilter {
      
        /**
         * we do a different flushmode than in the codebase
         * here
         */
        protected Session getSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException {
                Session session = SessionFactoryUtils.getSession(sessionFactory, true);
                session.setFlushMode(FlushMode.COMMIT);
                return session;
        }
        /**
         * we do an explicit flush here just in case
         * we do not have an automated flush
         */
        protected void closeSession(Session session, SessionFactory factory) {
                session.flush();
                super.closeSession(session, factory);
        }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值