4

What is the alternative of rollback transaction in spring except @Transactional annotation. I have used this annotation but i want the way by which i can rollback transaction in catch block. Is there any way?

Thanx in advance.

Xstian
  • 8,184
  • 10
  • 42
  • 72
Kedar Parikh
  • 807
  • 1
  • 14
  • 19

1 Answers1

3

Here is a draft:

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;
   }
Xstian
  • 8,184
  • 10
  • 42
  • 72
  • i have used TransactionAspectSupport.currentTransactionStatus().setRollbackOnly() in catch block, which rollbacks transaction done in try block. But, what if i do any transaction after this line in catch block? will be be rolled back too? – Kedar Parikh Sep 11 '14 at 09:32
  • In above draft will be rollbacked only the transactionstatus (current)... if you setRollbackOnly, by javadoc "This instructs the transaction manager that the only possible outcome of the transaction may be a rollback" – Xstian Sep 11 '14 at 09:41