Fix leaked db connections in SpringTransactionManager #167
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #134
Thanks to @GrannyOlli and @kzkvv for pointing it out.
To sum up: the method
inTransactionReturnsThrows
ofSpringTransactionManager
is marked as@Transactional
but this annotation takes effect only if this method is called outside its class (e.g. from here).Currently, there are 4 default methods of
TransactionManager
which both call it too, and, in fact, from the same class (SpringTransactionManager
inherits this interface), so@Transactional
doesn't work in this case because of Spring AOP restrictions. In some cases, this can cause errors (e.g. a connection can be left open after an exception occurring inside the transaction).In this PR, I overrode those methods with the same implementations but also added
@Transactional
for them. So now, a new transaction will be created while calling them too. On the other hand,inTransactionReturnsThrows
can be safely called inside those methods because, in this case, its@Transactional
is ignored and there won't be extra transactions spawned (as it could be if we call interface default methods with thesuper
keyword).Another solution would be to move
inTransactionReturnsThrows
to a different class, but since it's a Spring-only related issue, I think it's better to leave it as is.