According this article read only operation with JPA much more efficient in case of no transaction opened (in case of no outer transaction). So to improve performance it is needed to declare @Transactional(readOnly = true, propagation=Propagation.SUPPORTS) in all read only operation with JPA, e.g.
I gave this a spin - see remarks in the PR - looks like a useful change.
This change uses a different code path in org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(TransactionDefinition). than before.
We now hit the case:
"// Create "empty" transaction"
which avoids the creation of a new tx.
I am strongly opposing this one for a few reasons:
It's by no means clear that the change would actually improve performance. There's multiple aspects in this. First of them is that the linked article is dated and incredibly flawed as it aggressively simplifies things. I can go into details if you want but I'll leave it at that for now. There are a lot of things playing into execution performance here. Without a transaction in progress neither the readOnly flag is propagated to the JDBC driver (which would cause optimizations for a lot of databases not being applied) nor would you apply the optimizations in Spring's JPA resource management code like explicitly turning off flushing, which - if applied - can dramatically improve performance in case you read a lot of data.
There are means to tweak the default behavior already. The default transaction behavior can be tweaked on the repository interface declarations. Using @Transactional on the repository you can configure the transaction settings that are used during the execution of methods of the entire type hierarchy.
That said, we usually see people to define their own transaction boundaries on the service layer. Service methods usually provide a more adequate level of transaction granularity which leaves you yet another knob to tweak the behavior according to your needs
Concerning #2 I fully agree: user always can change transaction behavior at service layer.
Mark Repository layer by @Transactional is rather feature of Spring Data project to simplify developer life. Please correct me, if I mistaken.
So, I suppose ticket could be closed.
Thanks and best regards,