use of cn.taketoday.jdbc.datasource.ConnectionProxy in project today-infrastructure by TAKETODAY.
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();
}
use of cn.taketoday.jdbc.datasource.ConnectionProxy in project today-infrastructure by TAKETODAY.
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 cn.taketoday.jdbc.datasource.ConnectionProxy in project today-framework by TAKETODAY.
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 cn.taketoday.jdbc.datasource.ConnectionProxy in project today-framework by TAKETODAY.
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();
}
use of cn.taketoday.jdbc.datasource.ConnectionProxy in project today-framework by TAKETODAY.
the class JdbcTemplateTests method testConnectionCallback.
@Test
public void testConnectionCallback() throws Exception {
String result = this.template.execute(new ConnectionCallback<String>() {
@Override
public String doInConnection(Connection con) {
assertThat(con instanceof ConnectionProxy).isTrue();
assertThat(((ConnectionProxy) con).getTargetConnection()).isSameAs(JdbcTemplateTests.this.connection);
return "test";
}
});
assertThat(result).isEqualTo("test");
}
Aggregations