spring2.0事务的嵌套

来源:互联网 发布:手机淘宝体检中心 编辑:程序博客网 时间:2024/06/08 09:50

spring2.0事务的嵌套:

一个ServiceA:

public class ServiceA{

public int transactionDetailService(PaymentDetail detail)
   throws ServiceException {
  try {
     return dao.deletePaymentDetail(detail);
  } catch (DAOException ex) {
   logger .error("错误发生在:PaymentDetailServiceImpl.deletePaymentdetailService()方法中!");
   throw new ServiceException();
  }
 }

}

另一个ServiceB:

public class ServiceB{

public int deletePaymentInfoService(int payId) throws ServiceException {
  try {
         PaymentDetail detail = new PaymentDetail();
         detail.setPayId(payId);
          paymentDetailService.transactionDetailService(detail);
         dao.deletePaymentInfo(payId);     //如果这里执行删除操作失败的话,那么ServiceB的事务回滚,同时,内嵌事务ServiceA的事务也回滚;即transactionDetailService()中的删除操作回滚。
        return 1;
      } catch (DAOException ex) {
   logger.error("错误发生在:PaymentInfoServiceImpl.deletePaymentInfoService(int payId)方法中!");
   throw new ServiceException();
    }
 }

}

Spring2.0 配置文件:

 <aop:aspectj-autoproxy />

 <!-- default define, uses transactionManager,default *  is readonly -->
 <tx:advice id="txAdvice" transaction-manager="transactionManager">
  <tx:attributes>
   <tx:method name="insert*" />
   <tx:method name="update*" />
   <tx:method name="*" read-only="true" />
   <tx:method name="transaction*" propagation="NESTED" />
  </tx:attributes>
 </tx:advice>
 <aop:config proxy-target-class="true">
  <aop:advisor
   pointcut="execution(* com.ving.xzfw.service.impl.*Service*.*(..))"
   advice-ref="txAdvice" />
 </aop:config>