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(){};
}