Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow TransactionAwareDataSourceProxy to eagerly fetch the connection #29423

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
* properly. Use {@link Connection#unwrap} to retrieve the native JDBC Connection.
*
* @author Juergen Hoeller
* @author Réda Housni Alaoui
* @since 1.1
* @see javax.sql.DataSource#getConnection()
* @see java.sql.Connection#close()
Expand All @@ -77,6 +78,7 @@
public class TransactionAwareDataSourceProxy extends DelegatingDataSource {

private boolean reobtainTransactionalConnections = false;
private boolean obtainTransactionalConnectionEagerly = false;


/**
Expand Down Expand Up @@ -107,6 +109,15 @@ public void setReobtainTransactionalConnections(boolean reobtainTransactionalCon
this.reobtainTransactionalConnections = reobtainTransactionalConnections;
}

/**
* Specify whether to obtain the transactional connection as soon as possible.
* <p>The default is "false".
* <p>Turning this to "true" allows to obtain the transactional connection during {@link #getConnection()}
* <p>This setting has no effect if {@link #reobtainTransactionalConnections} is set to "true".
*/
public void setObtainTransactionalConnectionEagerly(boolean obtainTransactionalConnectionEagerly) {
this.obtainTransactionalConnectionEagerly = obtainTransactionalConnectionEagerly;
}

/**
* Delegates to DataSourceUtils for automatically participating in Spring-managed
Expand All @@ -130,7 +141,7 @@ public Connection getConnection() throws SQLException {
* @see java.sql.Connection#close()
* @see DataSourceUtils#doReleaseConnection
*/
protected Connection getTransactionAwareConnectionProxy(DataSource targetDataSource) {
protected Connection getTransactionAwareConnectionProxy(DataSource targetDataSource) throws SQLException {
return (Connection) Proxy.newProxyInstance(
ConnectionProxy.class.getClassLoader(),
new Class<?>[] {ConnectionProxy.class},
Expand Down Expand Up @@ -166,8 +177,11 @@ private class TransactionAwareInvocationHandler implements InvocationHandler {

private boolean closed = false;

public TransactionAwareInvocationHandler(DataSource targetDataSource) {
public TransactionAwareInvocationHandler(DataSource targetDataSource) throws SQLException {
this.targetDataSource = targetDataSource;
if (obtainTransactionalConnectionEagerly && shouldObtainFixedConnection(this.targetDataSource)) {
this.target = DataSourceUtils.doGetConnection(this.targetDataSource);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.fail;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.willThrow;
Expand All @@ -64,6 +65,7 @@

/**
* @author Juergen Hoeller
* @author Réda Housni Alaoui
* @since 04.07.2003
* @see org.springframework.jdbc.support.JdbcTransactionManagerTests
*/
Expand Down Expand Up @@ -1184,6 +1186,40 @@ protected void doInTransactionWithoutResult(TransactionStatus status) {
verify(con, times(2)).close();
}

@Test
public void testTransactionAwareDataSourceProxyWithObtainTransactionalConnectionEagerly() throws SQLException {
given(con.getAutoCommit()).willReturn(true);
given(con.getWarnings()).willThrow(new SQLException());

TransactionTemplate tt = new TransactionTemplate(tm);
boolean condition1 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition1).as("Hasn't thread connection").isTrue();
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
// something transactional
assertThat(DataSourceUtils.getConnection(ds)).isEqualTo(con);
TransactionAwareDataSourceProxy dsProxy = new TransactionAwareDataSourceProxy(ds);
dsProxy.setObtainTransactionalConnectionEagerly(true);
try {
Connection connection = dsProxy.getConnection();
assertThatThrownBy(connection::getWarnings).isInstanceOf(SQLException.class);
}
catch (SQLException ex) {
throw new UncategorizedSQLException("", "", ex);
}
}
});

boolean condition = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition).as("Hasn't thread connection").isTrue();
InOrder ordered = inOrder(con);
ordered.verify(con).setAutoCommit(false);
ordered.verify(con).commit();
ordered.verify(con).setAutoCommit(true);
verify(con).close();
}

/**
* Test behavior if the first operation on a connection (getAutoCommit) throws SQLException.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.fail;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.willThrow;
Expand All @@ -70,6 +71,7 @@

/**
* @author Juergen Hoeller
* @author Réda Housni Alaoui
* @since 5.3
* @see org.springframework.jdbc.datasource.DataSourceTransactionManagerTests
*/
Expand Down Expand Up @@ -1112,6 +1114,40 @@ protected void doInTransactionWithoutResult(TransactionStatus status) {
verify(con, times(2)).close();
}

@Test
public void testTransactionAwareDataSourceProxyWithObtainTransactionalConnectionEagerly() throws SQLException {
given(con.getAutoCommit()).willReturn(true);
given(con.getWarnings()).willThrow(new SQLException());

TransactionTemplate tt = new TransactionTemplate(tm);
boolean condition1 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition1).as("Hasn't thread connection").isTrue();
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
// something transactional
assertThat(DataSourceUtils.getConnection(ds)).isEqualTo(con);
TransactionAwareDataSourceProxy dsProxy = new TransactionAwareDataSourceProxy(ds);
dsProxy.setObtainTransactionalConnectionEagerly(true);
try {
Connection connection = dsProxy.getConnection();
assertThatThrownBy(connection::getWarnings).isInstanceOf(SQLException.class);
}
catch (SQLException ex) {
throw new UncategorizedSQLException("", "", ex);
}
}
});

boolean condition = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition).as("Hasn't thread connection").isTrue();
InOrder ordered = inOrder(con);
ordered.verify(con).setAutoCommit(false);
ordered.verify(con).commit();
ordered.verify(con).setAutoCommit(true);
verify(con).close();
}

/**
* Test behavior if the first operation on a connection (getAutoCommit) throws SQLException.
*/
Expand Down