0

I'm trying to make an independent call inside a method with transactional hook with following details :


    @Transactional(transactionManager = "tenantTransactionManager", isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED) 
    void method1(){
    //business logic 1
    metohd2();
    //business logic 2
    }

    method2(){
    TransactionSynchronizationManager.registerSynchronization( new TransactionSynchronization() {
                @Override
                public void afterCommit( boolean readOnly ) {
                      //businessLogic to be done after commit
                }
       });
    }

**EXPECTED BEHAVIOUR:**
  1. Transactions in method 1 should rollback is any exception occurs
  2. Transactions in method 2 should commit in any situation
user2971907
  • 21
  • 2
  • 6

1 Answers1

0

To run method2 in a separate transaction you have to do 2 things:

  1. Move method2 to another class (EJB)
  2. Annotate method2 with @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)

Moving it another class (EJB) because of: See: EJB Transactions in local method-calls

Your app container (like Payara) will automatically create transactions for all methods in your @Stateless class / EJB, as if they are all annotated with @TransactionAttribute(TransactionAttributeType.REQUIRED).

Also see: https://javaee.github.io/tutorial/transactions004.html

Max
  • 1,107
  • 12
  • 24