异常:Write operations are not allowed in read-only mode (FlushMode.NEVER)
我用Spring的声明事务,增加一个业务功能<关闭问题>时,我出现了异常:
Write operations are not allowed in read-only mode (FlushMode.NEVER) - turn your Session into FlushMode.AUTO or remove 'readOnly' marker from transaction definition
查了错误原理:在业务层dao.update(quesModel)中getSession的时候,会把获取回来的session的flush mode 设为FlushMode.NEVER。然后把该sessionFactory绑定到 TransactionSynchronizationManager,使request的整个过程都使用同一个session,在请求过后再接除该 sessionFactory的绑定,最后closeSessionIfNecessary 根据该session是否已和transaction绑定来决定是否关闭session。在这个过程中,若HibernateTemplate 发现自当前session有不是readOnly的transaction,就会获取到FlushMode.AUTO Session,使方法拥有写权限。也即是,如果有不是readOnly的transaction就可以由Flush.NEVER转为Flush.AUTO,拥有insert,update ,delete操作权限,如果没有transaction,并且没有另外人为地设flush model的话,则doFilter的整个过程都是Flush.NEVER。所以受transaction保护的方法有写权限,没受保护的则没有。
我的解决方式是:
1.将singleSession设为false,缺点是Hibernate Session的Instance可能会大增,使用的JDBC Connection量也会大增,如果Connection Pool的maxPoolSize设得太小,很容易就出问题。此办法放弃。
2.我的resovedQuesWithNoOkAnswer方法没有写权限,这时方法内有update操作,则需要手动设置flush model为Flush.AUTO,如
session.setFlushMode(FlushMode.AUTO); session.save(user); session.flush();
<property name="transactionAttributes"><props><prop key="save*">PROPAGATION_REQUIRED</prop><prop key="update*">PROPAGATION_REQUIRED</prop><prop key="delete*">PROPAGATION_REQUIRED</prop><prop key="add*">PROPAGATION_REQUIRED</prop><prop key="insert*">PROPAGATION_REQUIRED</prop><prop key="remove*">PROPAGATION_REQUIRED</prop><prop key="set*">PROPAGATION_REQUIRED</prop><prop key="login*">PROPAGATION_REQUIRED</prop><prop key="*">PROPAGATION_REQUIRED, readOnly</prop></props></property>
<prop key="resoved*">PROPAGATION_REQUIRED</prop>