I have a scheduler in spring boot application, if I use @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class) on scheduler method then it works well with only one database session but if I use @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class) at DAO level or Service class level, four DAO's/Services based on data it will go to one of the DAO's, it reaching maxConnection limit 16 as we see in database sessions and its stucking/hanging without further process in java. what would be the problem.?
2 Answers
It really depends on the way you are calling the methods.
One test that I would ask you to do is this:
Add in the schedule method this:
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
And then, at Service method, this:
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
This way, when the service method ends, it commits the transaction, but there is another transaction scope running in a higher level.
-
Thank you Sir, As per your suggestion it has only two sessions and its not increasing any more. Its working for me.– RameshApr 15, 2021 at 21:04
Basically all the processing inside the scheduler method should be managed inside one transaction so the annotation should be on it (the first approach).
In the case you add this annotation in service/DAO methods, so for each method is called inside you caode a new transaction is created, which may explain the problem you are facing.
Finally no need to add "rollbackFor = Exception.class" because this is the default behavior.