Kedar Parikh
Kedar Parikh

Reputation: 877

Alternative of @Transactional annotation

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.

Upvotes: 4

Views: 3830

Answers (1)

Xstian
Xstian

Reputation: 8272

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;
   }

Upvotes: 3

Related Questions