Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 701
Kedar Parikh Avatar asked Nov 02 '25 06:11

Kedar Parikh


1 Answers

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;
   }
like image 113
Xstian Avatar answered Nov 04 '25 20:11

Xstian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!