zoukankan      html  css  js  c++  java
  • spring中配置了事务,数据业务层捕获异常,事务配置不成功?

    这个问题我是解决了。原来我对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>

  • 相关阅读:
    web自动化中的三种切换---alert弹框切换
    web自动化中的三种切换--窗口切换
    web自动化中的三种切换---iframe
    web元素定位中的三种等待方法
    web自动化浏览器chrome和驱动chromedriver
    selenium安装
    pytest用例标记规则
    键盘事件
    鼠标事件
    控制浏览器
  • 原文地址:https://www.cnblogs.com/huzi007/p/3967982.html
Copyright © 2011-2022 走看看