0

I am trying to override the transactional behaviour for a service method(someService.updateSomething() in the example) annotated with @Transactional annotation in Spring. To do so, from other class, I am using programmatic transactional code like the next:

@Service
public class MyServiceClass {


   private TransactionTemplate transactionTemplate; 


   public MyClass (PlatformTransactionManager transactionManager) {
       transactionTemplate = new TransactionTemplate(transactionManager);
   }



 @Transactional
 public void someMethod(){                  
    transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);     
    transactionTemplate.execute(new TransactionCallbackWithoutResult()  {    
        protected void doInTransactionWithoutResult(TransactionStatus status){  
            try{
                someService.updateSomething();    
            }catch(Exception e){
                LOGGER.error("Error has ocurred");                  
            }
        } 
    });
  }
}

My problem is that someService.updateSomething() does not run in a new Transaction and I dont understand why. So:

  • If I call a proxied service method with transactional behaviour like someService.updateSomething() but in the call I create a new transaction like in the example, when the code hits to the proxied method, it will take the new transaction created and not the transaction already running for the someMethod() method, right?

Thanks!

General Grievance
  • 4,555
  • 31
  • 31
  • 45
fernando1979
  • 1,727
  • 2
  • 20
  • 26
  • To understand the issue I tried a simple test and it does start a new txn . Could you please share why you think a new txn is not started ? – R.G Jan 25 '20 at 12:13
  • Because when someService.updateSomething() throws an Exception, I get an UnexpectedRollbackException when someMethod tries to commit its transaction – fernando1979 Jan 26 '20 at 22:12
  • Could you please share the complete code and the exception trace . Also following [post](https://stackoverflow.com/questions/2007097/unexpectedrollbackexception-a-full-scenario-analysis) will be helpful in your analysis . – R.G Jan 26 '20 at 23:55
  • 1
    I understood whats happening. When you wrap programmatically a method with a new transaction, if the method throws an exception (this case), there is a LOGGER.error code but when it finishes, the parent method(someMethod) finishes and tries to commit the new transaction but the Transactional annotation in the someService.updateSomething() method in its class has marked the transaction to rollback. So, I do have a new transactional behaviour – fernando1979 Jan 27 '20 at 15:18

0 Answers0