use of org.springframework.transaction.support.TransactionTemplate 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.TransactionTemplate 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();
}
}
use of org.springframework.transaction.support.TransactionTemplate in project spring-framework by spring-projects.
the class DataSourceJtaTransactionTests method testJtaTransactionWithIsolationLevelDataSourceAdapter.
@Test
public void testJtaTransactionWithIsolationLevelDataSourceAdapter() throws Exception {
given(userTransaction.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE, Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
final IsolationLevelDataSourceAdapter dsToUse = new IsolationLevelDataSourceAdapter();
dsToUse.setTargetDataSource(dataSource);
dsToUse.afterPropertiesSet();
JtaTransactionManager ptm = new JtaTransactionManager(userTransaction);
ptm.setAllowCustomIsolationLevels(true);
TransactionTemplate tt = new TransactionTemplate(ptm);
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
Connection c = DataSourceUtils.getConnection(dsToUse);
assertThat(TransactionSynchronizationManager.hasResource(dsToUse)).as("Has thread connection").isTrue();
assertThat(c).isSameAs(connection);
DataSourceUtils.releaseConnection(c, dsToUse);
}
});
tt.setIsolationLevel(TransactionDefinition.ISOLATION_REPEATABLE_READ);
tt.setReadOnly(true);
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
Connection c = DataSourceUtils.getConnection(dsToUse);
assertThat(TransactionSynchronizationManager.hasResource(dsToUse)).as("Has thread connection").isTrue();
assertThat(c).isSameAs(connection);
DataSourceUtils.releaseConnection(c, dsToUse);
}
});
verify(userTransaction, times(2)).begin();
verify(userTransaction, times(2)).commit();
verify(connection).setReadOnly(true);
verify(connection).setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
verify(connection, times(2)).close();
}
use of org.springframework.transaction.support.TransactionTemplate in project spring-framework by spring-projects.
the class DataSourceTransactionManagerTests method testParticipatingTransactionWithIncompatibleIsolationLevel.
@Test
public void testParticipatingTransactionWithIncompatibleIsolationLevel() throws Exception {
tm.setValidateExistingTransaction(true);
boolean condition2 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition2).as("Hasn't thread connection").isTrue();
boolean condition1 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition1).as("Synchronization not active").isTrue();
assertThatExceptionOfType(IllegalTransactionStateException.class).isThrownBy(() -> {
final TransactionTemplate tt = new TransactionTemplate(tm);
final TransactionTemplate tt2 = new TransactionTemplate(tm);
tt2.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE);
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
assertThat(status.isRollbackOnly()).as("Is not rollback-only").isFalse();
tt2.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
status.setRollbackOnly();
}
});
assertThat(status.isRollbackOnly()).as("Is rollback-only").isTrue();
}
});
});
boolean condition = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition).as("Hasn't thread connection").isTrue();
verify(con).rollback();
verify(con).close();
}
use of org.springframework.transaction.support.TransactionTemplate in project spring-framework by spring-projects.
the class DataSourceTransactionManagerTests method testPropagationRequiresNewWithExistingTransactionAndUnrelatedFailingDataSource.
@Test
public void testPropagationRequiresNewWithExistingTransactionAndUnrelatedFailingDataSource() throws Exception {
final DataSource ds2 = mock(DataSource.class);
SQLException failure = new SQLException();
given(ds2.getConnection()).willThrow(failure);
final TransactionTemplate tt = new TransactionTemplate(tm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
DataSourceTransactionManager tm2 = new DataSourceTransactionManager(ds2);
tm2.setTransactionSynchronization(DataSourceTransactionManager.SYNCHRONIZATION_NEVER);
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();
assertThatExceptionOfType(CannotCreateTransactionException.class).isThrownBy(() -> 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 {
status.setRollbackOnly();
}
});
}
})).withCause(failure);
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).rollback();
verify(con).close();
}
Aggregations