use of org.springframework.transaction.support.TransactionCallbackWithoutResult in project spring-framework by spring-projects.
the class DataSourceTransactionManagerTests 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.transaction.support.TransactionCallbackWithoutResult in project spring-framework by spring-projects.
the class DataSourceTransactionManagerTests method testPropagationRequiresNewWithExistingTransactionAndUnrelatedDataSource.
@Test
public void testPropagationRequiresNewWithExistingTransactionAndUnrelatedDataSource() throws Exception {
Connection con2 = mock(Connection.class);
final DataSource ds2 = mock(DataSource.class);
given(ds2.getConnection()).willReturn(con2);
final TransactionTemplate tt = new TransactionTemplate(tm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
PlatformTransactionManager tm2 = new DataSourceTransactionManager(ds2);
final TransactionTemplate tt2 = new TransactionTemplate(tm2);
tt2.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
boolean condition4 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition4).as("Hasn't thread connection").isTrue();
boolean condition3 = !TransactionSynchronizationManager.hasResource(ds2);
assertThat(condition3).as("Hasn't thread connection").isTrue();
boolean condition2 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition2).as("Synchronization not active").isTrue();
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
assertThat(status.isNewTransaction()).as("Is new transaction").isTrue();
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization active").isTrue();
assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).isFalse();
assertThat(TransactionSynchronizationManager.isActualTransactionActive()).isTrue();
tt2.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
assertThat(TransactionSynchronizationManager.hasResource(ds)).as("Has thread connection").isTrue();
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization active").isTrue();
assertThat(status.isNewTransaction()).as("Is new transaction").isTrue();
assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).isFalse();
assertThat(TransactionSynchronizationManager.isActualTransactionActive()).isTrue();
status.setRollbackOnly();
}
});
assertThat(status.isNewTransaction()).as("Is new transaction").isTrue();
assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).isFalse();
assertThat(TransactionSynchronizationManager.isActualTransactionActive()).isTrue();
}
});
boolean condition1 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition1).as("Hasn't thread connection").isTrue();
boolean condition = !TransactionSynchronizationManager.hasResource(ds2);
assertThat(condition).as("Hasn't thread connection").isTrue();
verify(con).commit();
verify(con).close();
verify(con2).rollback();
verify(con2).close();
}
use of org.springframework.transaction.support.TransactionCallbackWithoutResult in project spring-framework by spring-projects.
the class DataSourceJtaTransactionTests method testJtaTransactionWithConnectionHolderStillBound.
@Test
public void testJtaTransactionWithConnectionHolderStillBound() throws Exception {
@SuppressWarnings("serial") JtaTransactionManager ptm = new JtaTransactionManager(userTransaction) {
@Override
protected void doRegisterAfterCompletionWithJtaTransaction(JtaTransactionObject txObject, final List<TransactionSynchronization> synchronizations) throws RollbackException, SystemException {
Thread async = new Thread() {
@Override
public void run() {
invokeAfterCompletion(synchronizations, TransactionSynchronization.STATUS_COMMITTED);
}
};
async.start();
try {
async.join();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
};
TransactionTemplate tt = new TransactionTemplate(ptm);
boolean condition2 = !TransactionSynchronizationManager.hasResource(dataSource);
assertThat(condition2).as("Hasn't thread connection").isTrue();
boolean condition1 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition1).as("JTA synchronizations not active").isTrue();
given(userTransaction.getStatus()).willReturn(Status.STATUS_ACTIVE);
for (int i = 0; i < 3; i++) {
final boolean releaseCon = (i != 1);
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).as("JTA synchronizations active").isTrue();
boolean condition = !status.isNewTransaction();
assertThat(condition).as("Is existing transaction").isTrue();
Connection c = DataSourceUtils.getConnection(dataSource);
assertThat(TransactionSynchronizationManager.hasResource(dataSource)).as("Has thread connection").isTrue();
DataSourceUtils.releaseConnection(c, dataSource);
c = DataSourceUtils.getConnection(dataSource);
assertThat(TransactionSynchronizationManager.hasResource(dataSource)).as("Has thread connection").isTrue();
if (releaseCon) {
DataSourceUtils.releaseConnection(c, dataSource);
}
}
});
if (!releaseCon) {
assertThat(TransactionSynchronizationManager.hasResource(dataSource)).as("Still has connection holder").isTrue();
} else {
boolean condition = !TransactionSynchronizationManager.hasResource(dataSource);
assertThat(condition).as("Hasn't thread connection").isTrue();
}
boolean condition = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition).as("JTA synchronizations not active").isTrue();
}
verify(connection, times(3)).close();
}
use of org.springframework.transaction.support.TransactionCallbackWithoutResult in project spring-framework by spring-projects.
the class DataSourceJtaTransactionTests method doTestJtaTransactionCommitWithNewTransactionWithinEmptyTransaction.
private void doTestJtaTransactionCommitWithNewTransactionWithinEmptyTransaction(final boolean requiresNew, boolean notSupported) throws Exception {
if (notSupported) {
given(userTransaction.getStatus()).willReturn(Status.STATUS_ACTIVE, Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
given(transactionManager.suspend()).willReturn(transaction);
} else {
given(userTransaction.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
}
final DataSource dataSource = mock(DataSource.class);
final Connection connection1 = mock(Connection.class);
final Connection connection2 = mock(Connection.class);
given(dataSource.getConnection()).willReturn(connection1, connection2);
final JtaTransactionManager ptm = new JtaTransactionManager(userTransaction, transactionManager);
TransactionTemplate tt = new TransactionTemplate(ptm);
tt.setPropagationBehavior(notSupported ? TransactionDefinition.PROPAGATION_NOT_SUPPORTED : TransactionDefinition.PROPAGATION_SUPPORTS);
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).isFalse();
assertThat(TransactionSynchronizationManager.isActualTransactionActive()).isFalse();
assertThat(DataSourceUtils.getConnection(dataSource)).isSameAs(connection1);
assertThat(DataSourceUtils.getConnection(dataSource)).isSameAs(connection1);
TransactionTemplate tt2 = new TransactionTemplate(ptm);
tt2.setPropagationBehavior(requiresNew ? TransactionDefinition.PROPAGATION_REQUIRES_NEW : TransactionDefinition.PROPAGATION_REQUIRED);
tt2.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).isFalse();
assertThat(TransactionSynchronizationManager.isActualTransactionActive()).isTrue();
assertThat(DataSourceUtils.getConnection(dataSource)).isSameAs(connection2);
assertThat(DataSourceUtils.getConnection(dataSource)).isSameAs(connection2);
}
});
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).isFalse();
assertThat(TransactionSynchronizationManager.isActualTransactionActive()).isFalse();
assertThat(DataSourceUtils.getConnection(dataSource)).isSameAs(connection1);
}
});
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
verify(userTransaction).begin();
verify(userTransaction).commit();
if (notSupported) {
verify(transactionManager).resume(transaction);
}
verify(connection2).close();
verify(connection1).close();
}
use of org.springframework.transaction.support.TransactionCallbackWithoutResult in project spring-framework by spring-projects.
the class DataSourceJtaTransactionTests method doTestJtaTransactionWithPropagationRequiresNewAndBeginException.
private void doTestJtaTransactionWithPropagationRequiresNewAndBeginException(boolean suspendException, final boolean openOuterConnection, final boolean useTransactionAwareDataSource) throws Exception {
given(userTransaction.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
if (suspendException) {
given(transactionManager.suspend()).willThrow(new SystemException());
} else {
given(transactionManager.suspend()).willReturn(transaction);
willThrow(new SystemException()).given(userTransaction).begin();
}
given(connection.isReadOnly()).willReturn(true);
final DataSource dsToUse = useTransactionAwareDataSource ? new TransactionAwareDataSourceProxy(dataSource) : dataSource;
if (dsToUse instanceof TransactionAwareDataSourceProxy) {
((TransactionAwareDataSourceProxy) dsToUse).setReobtainTransactionalConnections(true);
}
JtaTransactionManager ptm = new JtaTransactionManager(userTransaction, transactionManager);
final TransactionTemplate tt = new TransactionTemplate(ptm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
boolean condition3 = !TransactionSynchronizationManager.hasResource(dsToUse);
assertThat(condition3).as("Hasn't thread connection").isTrue();
boolean condition2 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition2).as("JTA synchronizations not active").isTrue();
assertThatExceptionOfType(TransactionException.class).isThrownBy(() -> tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
boolean condition = !TransactionSynchronizationManager.hasResource(dsToUse);
assertThat(condition).as("Hasn't thread connection").isTrue();
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).as("JTA synchronizations active").isTrue();
assertThat(status.isNewTransaction()).as("Is new transaction").isTrue();
Connection c = DataSourceUtils.getConnection(dsToUse);
try {
assertThat(TransactionSynchronizationManager.hasResource(dsToUse)).as("Has thread connection").isTrue();
c.isReadOnly();
DataSourceUtils.releaseConnection(c, dsToUse);
c = DataSourceUtils.getConnection(dsToUse);
assertThat(TransactionSynchronizationManager.hasResource(dsToUse)).as("Has thread connection").isTrue();
if (!openOuterConnection) {
DataSourceUtils.releaseConnection(c, dsToUse);
}
} catch (SQLException ex) {
}
try {
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
boolean condition = !TransactionSynchronizationManager.hasResource(dsToUse);
assertThat(condition).as("Hasn't thread connection").isTrue();
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).as("JTA synchronizations active").isTrue();
assertThat(status.isNewTransaction()).as("Is new transaction").isTrue();
Connection c = DataSourceUtils.getConnection(dsToUse);
assertThat(TransactionSynchronizationManager.hasResource(dsToUse)).as("Has thread connection").isTrue();
DataSourceUtils.releaseConnection(c, dsToUse);
c = DataSourceUtils.getConnection(dsToUse);
assertThat(TransactionSynchronizationManager.hasResource(dsToUse)).as("Has thread connection").isTrue();
DataSourceUtils.releaseConnection(c, dsToUse);
}
});
} finally {
if (openOuterConnection) {
try {
c.isReadOnly();
DataSourceUtils.releaseConnection(c, dsToUse);
} catch (SQLException ex) {
}
}
}
}
}));
boolean condition1 = !TransactionSynchronizationManager.hasResource(dsToUse);
assertThat(condition1).as("Hasn't thread connection").isTrue();
boolean condition = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition).as("JTA synchronizations not active").isTrue();
verify(userTransaction).begin();
if (suspendException) {
verify(userTransaction).rollback();
}
if (suspendException) {
verify(connection, atLeastOnce()).close();
} else {
verify(connection, never()).close();
}
}
Aggregations