Introduce lazyTransactionalConnections flag on TransactionAwareDataSourceProxy

Includes revision of JDBC transaction tests.

Closes gh-29423
This commit is contained in:
Juergen Hoeller
2023-12-04 18:24:30 +01:00
parent 8a82da43c9
commit 47fe61ef79
4 changed files with 224 additions and 1673 deletions

View File

@@ -76,6 +76,8 @@ import org.springframework.transaction.support.TransactionSynchronizationManager
*/
public class TransactionAwareDataSourceProxy extends DelegatingDataSource {
private boolean lazyTransactionalConnections = true;
private boolean reobtainTransactionalConnections = false;
@@ -94,6 +96,18 @@ public class TransactionAwareDataSourceProxy extends DelegatingDataSource {
super(targetDataSource);
}
/**
* Specify whether to obtain the transactional target Connection lazily on
* actual data access.
* <p>The default is "true". Specify "false" to immediately obtain a target
* Connection when a transaction-aware Connection handle is retrieved.
* @since 6.1.2
*/
public void setLazyTransactionalConnections(boolean lazyTransactionalConnections) {
this.lazyTransactionalConnections = lazyTransactionalConnections;
}
/**
* Specify whether to reobtain the target Connection for each operation
* performed within a transaction.
@@ -119,7 +133,12 @@ public class TransactionAwareDataSourceProxy extends DelegatingDataSource {
*/
@Override
public Connection getConnection() throws SQLException {
return getTransactionAwareConnectionProxy(obtainTargetDataSource());
DataSource ds = obtainTargetDataSource();
Connection con = getTransactionAwareConnectionProxy(ds);
if (!this.lazyTransactionalConnections && shouldObtainFixedConnection(ds)) {
((ConnectionProxy) con).getTargetConnection();
}
return con;
}
/**