当使用spring+hibernate+struts2的时候,会遇到此问题。
大概异常描述 :org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.NEVER) - turn your Session into FlushMode.AUTO or remove 'readOnly' marker from transaction definition
org.springframework.orm.hibernate3.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:910)
org.springframework.orm.hibernate3.HibernateTemplate$18.doInHibernate(HibernateTemplate.java:614)
org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:312)
org.springframework.orm.hibernate3.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:612)
hibernate.dao.HibernateDAO.saveOrUpdate(HibernateDAO.java:44)。。。。。。。。。。。。。
解决办法:
1:在web.xml里加上OpenSessionInViewFilter的filter。
<!-- OpenSessionInViewFilter-->
<filter>
<filter-name> OpenSessionInViewFilter </filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name> OpenSessionInViewFilter </filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2.配置事务
事务方式1:
<bean name="transactionAttributesSource" class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">
<property name="properties">
<props>
<prop key="get*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="create*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="submit*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="transactionAttributeSource">
<ref bean="transactionAttributesSource"/>
</property>
</bean>
<bean id="autoTxProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="interceptorNames">
<list>
<idref local="transactionInterceptor"/>
</list>
</property>
<property name="beanNames">
<value>*Dao</value>
</property>
</bean>
事务方式2:
<aop:config proxy-target-class="true">
<aop:pointcut id="contractService"
expression="execution(* com.richserc.contract.service..*ContractTypeService.*(..))"/>
<aop:pointcut id="contractWebService"
expression="execution(* com.richserc.contract.web..*ContractTypeAction.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="contractService" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="contractWebService" />
</aop:config>
<!-- 基本事务定义,使用transactionManager作事务管理
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*"/>
<tx:method name="update*"/>
<tx:method name="remove*"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>