spring中配置了事务,数据业务层捕获异常,事务配置不成功?
2014-09-12 11:12
330 查看
这个问题我是解决了。原来我对spring的事务切如原理不是特别了解。
解决方案如下:
原理:spring aop 异常捕获原理:被拦截的方法需显式抛出异常,并不能经任何处理,这样aop代理才能捕获到方法的异常,才能进行回滚,默认情况下aop只捕获runtimeexception的异常,但可以通过
<tx:method name="upd*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
配置来捕获特定的异常并回滚
换句话说在service的方法中不使用try catch 或者在catch中最后加上throw new runtimeexcetpion(),这样程序异常时才能被aop捕获进而回滚
解决方案:
方案1.例如service层处理事务,那么service中的方法中不做异常捕获,或者在catch语句中最后增加throw new RuntimeException()语句,以便让aop捕获异常再去回滚,并且在service上层(webservice客户端,view层action)要继续捕获这个异常并处理
方案2.在service层方法的catch语句中增加:TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();语句,手动回滚,这样上层就无需去处理异常(现在项目的做法)
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="upd*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="*" propagation="SUPPORTS" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="canyin" expression="execution(* com.laphone.base.baseservice.*.*(..)) ||execution(* com.laphone.canyin.*.service.*.*(..)) || execution(* com.laphone.canyin.*.*.service.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="canyin" />
</aop:config>
解决方案如下:
原理:spring aop 异常捕获原理:被拦截的方法需显式抛出异常,并不能经任何处理,这样aop代理才能捕获到方法的异常,才能进行回滚,默认情况下aop只捕获runtimeexception的异常,但可以通过
<tx:method name="upd*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
配置来捕获特定的异常并回滚
换句话说在service的方法中不使用try catch 或者在catch中最后加上throw new runtimeexcetpion(),这样程序异常时才能被aop捕获进而回滚
解决方案:
方案1.例如service层处理事务,那么service中的方法中不做异常捕获,或者在catch语句中最后增加throw new RuntimeException()语句,以便让aop捕获异常再去回滚,并且在service上层(webservice客户端,view层action)要继续捕获这个异常并处理
方案2.在service层方法的catch语句中增加:TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();语句,手动回滚,这样上层就无需去处理异常(现在项目的做法)
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="upd*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="*" propagation="SUPPORTS" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="canyin" expression="execution(* com.laphone.base.baseservice.*.*(..)) ||execution(* com.laphone.canyin.*.service.*.*(..)) || execution(* com.laphone.canyin.*.*.service.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="canyin" />
</aop:config>
相关文章推荐
- spring中配置了事务,数据业务层捕获异常,事务配置不成功?
- Spring AOP配置声明式事务,数据业务层捕获异常,事务控制处理
- Spring配置事务 service 异常捕获回滚问题
- 【spring源码学习】spring配置的事务方式是REQUIRED,但业务层抛出TransactionRequiredException异常问题
- Spring配置事务 service 异常捕获回滚问题
- Spring异常捕获而且回滚事务的方法
- spring事务异常捕获问题
- MySQL innodb引擎,事务提交 commit 异常,但数据提交成功
- 解决Spring AOP 事务 配置 失效原因--业务类里抛出的异常不满足事务拦截器里定义的异常
- Spring事务异常回滚,捕获异常不抛出就不会回滚
- Spring事务异常回滚,捕获异常不抛出就不会回滚
- Spring事务异常回滚,捕获异常不抛出就不会回滚
- Spring事务异常回滚,捕获异常不抛出就不会回滚
- 异常捕获不抛出,Spring事务无法回滚
- Spring事务异常回滚,捕获异常不抛出就不会回滚
- 异常捕获不抛出,Spring事务无法回滚
- spring配置好后,写入数据时空指针异常,objectFactory
- 异常捕获不抛出,Spring事务无法回滚
- spring 管理事务配置时,结果 报错: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here这个异常
- Spring事务异常回滚,捕获异常不抛出就不会回滚