4

除了 @Transactional 注释之外,spring 中回滚事务的替代方法是什么。我已经使用了这个注释,但我想要在 catch 块中回滚事务的方式。有什么办法吗?

提前谢谢。

4

1 回答 1

3

这是一个草稿:

public class SomeService implements SomeInterface {

   private SomeDao thisDaoWrapsJdbcTemplate;
   private PlatformTransactionManager transactionManager;

   public void setTransactionManager( PlatformTransactionManager transactionManager ) {
      this.transactionManager = transactionManager;
   }

   public void doBusiness( Business: business ) {

      TransactionDefinition def = new DefaultTransactionDefinition();
      TransactionStatus status = transactionManager.getTransaction( def );

      try {

         // do business here
         Money money = Money.LOTS_OF
         ...
         // wire the money in..
         thisDaoWrapsJdbcTemplate.depositLotsOfMoney( money )

         transactionManager.commit( status );

      } catch ( DataAccessException dae ) {

         transactionManager.rollback( status );
         throw dae;
      }
      return;
   }
于 2014-09-11T09:29:34.723 回答