org.springframework.dao.InvalidDataAccessApiUsag eException:Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL):Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker fromtransaction
definition.
问题 : 只读模式下(FlushMode.NEVER/MANUAL) 写操作不被允许:把你的Session改成FlushMode.COMMIT/AUTO或者清除事务定义中的readOnly标记。
错误原因 :
OpenSessionInViewFilter在 getSession 的时候 , 会把获取回来的 session 的 flush
mode 设为 FlushMode.NEVER 。然后把该 sessionFactory 绑定到 TransactionSynchronizati onManager ,使 request 的整个过程都使用同一个 session ,在请求过后再接除该 sessionFactory 的绑定,最后 closeSessionIfNecessary 根据该 session 是否已和 transaction 绑定来决定是否关闭 session 。在这个过程中,若 HibernateTemplate 发现自当前 session 有不是 readOnly 的 transaction ,就会获取到 FlushMode.AUTOSession ,使方法拥有写权限。 也即是,如果有不是readOnly 的 transaction 就可以由 Flush.NEVER 转为 Flush.AUTO, 拥有 insert,update,delete 操作权限,如果没有 transaction ,并且没有另外人为地设 flush
model 的话,则 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.HibernateTransactionMana ger">
<property name="sessionFactory"ref="sessionFactory" />
</bean>
<aop:config>
<aop:pointcut id="bussinessService"
expression="execution(*com.fan.service.base.*.*(..))" />
<aop:advisor pointcut-ref="bussinessService"
advice-ref="txAdvice" />
</aop:config>
<tx:adviceid="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*"read-only="false" propagation="NOT_SUPPORTED"/>
<tx:method name="find*"read-only="false" propagation="NOT_SUPPORTED"/>
<tx:method name="save*"propagation="REQUIRED"/> // 如果不把 saveupdate delete 都配置上,
<tx:method name="update*"propagation="REQUIRED"/> //这些操作会无效
<tx:method name="delete*"propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>