spring 声明性事务配置中的一个小疑点
spring 声明性事务配置中的一个小问题
在使用ss2时遇见的一个问题,先帖上出错的代码
UserManager.java
public class UserManager extends HibernateEntityExtendDao<User> { public User checkLogin(String username, String password) { //省略 } }
UserManager 所在的包是com.xyz.demo.service
HibernateEntityExtendDao 使用的是springside2的 org.springside.core.dao.extend.HibernateEntityExtendDao
声明性事务相关的部分配置内容如下:
<!-- 定义事务通知--> <tx:advice transaction-manager="hibernateTransactionManager" id="txAdvice"> <tx:attributes> <tx:method name="*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <!--定义一个切面及切面和事务关联--> <aop:config proxy-target-class="true"> <aop:advisor pointcut="execution(* com.xyz.demo.service.*Manager.*(..))" advice-ref="txAdvice"/> </aop:config>
以上配置在没使用osiv前一切正常,使用osiv后,就出现
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.
看到各位高手都说这个是由于没有正确配置事务造成的,找了N久,终于找到了问题:
没有对UserManager的父类配置事务 ,一直以为配置了子类的事务,父类就不用配置了,现在想想真是大囧
将事务的配置改成下面这样就OK了
<!-- 定义事务通知--> <tx:advice transaction-manager="hibernateTransactionManager" id="txAdvice"> <tx:attributes> <tx:method name="*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <!--定义一个切面及切面和事务关联--> <aop:config proxy-target-class="true"> <aop:advisor pointcut="execution(* com.xyz.demo.service.*Manager.*(..))" advice-ref="txAdvice"/><aop:advisor pointcut="execution(* org.springside.core.dao.*Dao.*(..))" advice-ref="txAdvice"/> </aop:config>