Call a transactional function of Required propagation twice results in only 1 transaction ( but 2 transactions when calling from another class)

Viewed 343

I have a service class with method doSomething() annotated with @transactional and of propagation type “required”. Another non-transactional class calling doSomething() twice.

I assume that there will be 2 transactions, created for each of the call, but it turned out to be only using the same transaction for both calls. To confuse me further, I called doSomething() twice using another @RestController non-transactional class. But this time it turned out to be using different transactions for each call.

I have 2 questions.

What is the expected behaviour (1 or 2 transactions)

Why is the difference here?

NormalClass{
   @Autowired
   Service service;

   service.doSomething();
   // same transaction used
   service.doSomething(); 
}

RestController{
   @Autowired
   Service service;

   service.doSomething();
   // new transaction used
   service.doSomething();
}

Service{
   @Transactional
   doSomething(){};
}

1 Answers

Thanks @JB Nizet you're right that there's an abstract class extended by NormalClass that already obtains transaction. I wasn't expecting an abstract class to do that.

Related