org.springframework.dao.InvalidDataAccessApiUsageException: Writejava
operations are not allowed in read-only modeweb
(FlushMode.NEVER/MANUAL): Turn your Session intospring
FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transactionsession
definition.ui
athibernate
org.springframework.orm.hibernate3.HibernateTemplate.checkWriteOperorm
ationAllowed(HibernateTemplate.java:1095)xml
這個異常產生的主要緣由是DAO採用了Spring容器的事務管理策略,若是操做方法的名稱和事務策略中指定的被管理的名稱不可以匹配上,spring 就會採起默認的事務管理策略(PROPAGATION_REQUIRED,read only).若是是插入和修改操做,就不被容許的,因此報這個異常htm
查看spring中事務管理配置:
<bean id="txProxyTemplate" abstract="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="find*">PROPAGATION_REQUIRED</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="create*">PROPAGATION_REQUIRED</prop>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="del*">PROPAGATION_REQUIRED</prop>
<prop key="clear*">PROPAGATION_REQUIRED</prop>
<prop key="build*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
看了以後才知道,原來的事務策略的<prop key="*">PROPAGATION_REQUIRED</prop>被刪除後,bumenAuth()方法後忘了修改,因此致使報上述的錯誤
修改方法一:
將此方法修改成update或者build,add....等上述策略名稱開頭的方法:如:updateBumenAuth()
修改方法二:
增長<prop key="*">PROPAGATION_REQUIRED</prop>便可
修改方法三:
將web.xml下的
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>
中 的singleSession值修改成false,即不限制整個過程用同一個session,但缺點是Hibernate Session的Instance可能會大增,使用的JDBC Connection量也會大增,若是Connection Pool的maxPoolSize設得過小,很容易就出問題
參考:關於OpenSessionView(http://liuwei1578.blog.163.com/blog/static/4958036420092104215514/)
Spring事務配置TransactionProxyFactoryBean(http://liuwei1578.blog.163.com/blog/static/49580364200921041136625/)