use of org.springframework.jdbc.datasource.ConnectionProxy in project spring-framework by spring-projects.
the class JdbcTransactionManagerTests method testTransactionAwareDataSourceProxyWithSuspensionAndReobtaining.
@Test
public void testTransactionAwareDataSourceProxyWithSuspensionAndReobtaining() throws Exception {
given(con.getAutoCommit()).willReturn(true);
final TransactionTemplate tt = new TransactionTemplate(tm);
tt.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRES_NEW);
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);
final TransactionAwareDataSourceProxy dsProxy = new TransactionAwareDataSourceProxy(ds);
dsProxy.setReobtainTransactionalConnections(true);
try {
assertThat(((ConnectionProxy) dsProxy.getConnection()).getTargetConnection()).isEqualTo(con);
// should be ignored
dsProxy.getConnection().close();
} catch (SQLException ex) {
throw new UncategorizedSQLException("", "", ex);
}
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
// something transactional
assertThat(DataSourceUtils.getConnection(ds)).isEqualTo(con);
try {
assertThat(((ConnectionProxy) dsProxy.getConnection()).getTargetConnection()).isEqualTo(con);
// should be ignored
dsProxy.getConnection().close();
} catch (SQLException ex) {
throw new UncategorizedSQLException("", "", ex);
}
}
});
try {
assertThat(((ConnectionProxy) dsProxy.getConnection()).getTargetConnection()).isEqualTo(con);
// should be ignored
dsProxy.getConnection().close();
} 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, times(2)).close();
}
use of org.springframework.jdbc.datasource.ConnectionProxy in project spring-framework by spring-projects.
the class JdbcTemplateTests method testConnectionCallback.
@Test
public void testConnectionCallback() throws Exception {
String result = this.template.execute((ConnectionCallback<String>) con -> {
assertThat(con instanceof ConnectionProxy).isTrue();
assertThat(((ConnectionProxy) con).getTargetConnection()).isSameAs(JdbcTemplateTests.this.connection);
return "test";
});
assertThat(result).isEqualTo("test");
}
use of org.springframework.jdbc.datasource.ConnectionProxy in project spring-framework by spring-projects.
the class JdbcTransactionManagerTests method testTransactionAwareDataSourceProxyWithSuspension.
@Test
public void testTransactionAwareDataSourceProxyWithSuspension() throws Exception {
given(con.getAutoCommit()).willReturn(true);
final TransactionTemplate tt = new TransactionTemplate(tm);
tt.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRES_NEW);
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);
final TransactionAwareDataSourceProxy dsProxy = new TransactionAwareDataSourceProxy(ds);
try {
assertThat(((ConnectionProxy) dsProxy.getConnection()).getTargetConnection()).isEqualTo(con);
// should be ignored
dsProxy.getConnection().close();
} catch (SQLException ex) {
throw new UncategorizedSQLException("", "", ex);
}
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
// something transactional
assertThat(DataSourceUtils.getConnection(ds)).isEqualTo(con);
try {
assertThat(((ConnectionProxy) dsProxy.getConnection()).getTargetConnection()).isEqualTo(con);
// should be ignored
dsProxy.getConnection().close();
} catch (SQLException ex) {
throw new UncategorizedSQLException("", "", ex);
}
}
});
try {
assertThat(((ConnectionProxy) dsProxy.getConnection()).getTargetConnection()).isEqualTo(con);
// should be ignored
dsProxy.getConnection().close();
} 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, times(2)).close();
}
Aggregations