Description
DataSourceUtils resolution getTargetConnection
gets stuck in an endless loop under certain conditions in spring-jdbc 6.2.2/6.2.3.
I am creating a DataSource
, wrapping it into the TransactionAwareDataSourceProxy
and then creating an EntityManagerFactory
. Then the datasource and the emf both get posted to a JpaTransactionManager
.
Upon trying to commit a transaction, the application goes into an endless loop in DataSourceUtils.getTargetConnection()
.
As far as I could understand it by debugging, the following happens.
TransactionAwareDataSourceProxy
gets asked for a connection and creates a lazy initialization proxy. As this proxy is lazy, it does not initialize an inner. The proxy handler is the TransactionAwareInvocationHandler
.
Next step is, that this proxy gets bound to the current transaction-context in the TransactionSynchronizationManager
. This happens during JpaTransactionManager.doBegin(Object, TransactionDefinition)
.
Now, during release of the connection while committing, the connection proxy gets into DataSourceUtils.getTargetConnection()
and there it is determined, it is an instanceof ConnectionProxy
and therefore gets resolved within TransactionAwareDataSourceProxy
, which delegates to DataSourceUtils.doGetConnection
, which delegates to TransactionSynchronizationManager.getResource
which returns the uninitialized proxy.
I am not quite sure, why most invocations do not trigger this issue and some do. My current assumption is, that there is no issue, as long as the databaseconnection is actually used within the transaction. We have, however, cases, where a spring bean caches data, requests a transaction but does not actually trigger database queries upon cache-hits.
I am not quite sure, how to solve this correctly. Probably a non-initialized proxy for a connection should not be registered at all within the TransactionAwareDataSourceProxy
?