use of org.springframework.transaction.TransactionStatus in project spring-framework by spring-projects.
the class CciLocalTransactionTests method testLocalTransactionRollback.
/**
* Test if a transaction ( begin / rollback ) is executed on the
* LocalTransaction when CciLocalTransactionManager is specified as
* transaction manager and a non-checked exception is thrown.
*/
@Test
public void testLocalTransactionRollback() throws ResourceException {
final ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
Connection connection = mock(Connection.class);
Interaction interaction = mock(Interaction.class);
LocalTransaction localTransaction = mock(LocalTransaction.class);
final Record record = mock(Record.class);
final InteractionSpec interactionSpec = mock(InteractionSpec.class);
given(connectionFactory.getConnection()).willReturn(connection);
given(connection.getLocalTransaction()).willReturn(localTransaction);
given(connection.createInteraction()).willReturn(interaction);
given(interaction.execute(interactionSpec, record, record)).willReturn(true);
given(connection.getLocalTransaction()).willReturn(localTransaction);
CciLocalTransactionManager tm = new CciLocalTransactionManager();
tm.setConnectionFactory(connectionFactory);
TransactionTemplate tt = new TransactionTemplate(tm);
try {
tt.execute(new TransactionCallback<Void>() {
@Override
public Void doInTransaction(TransactionStatus status) {
assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(connectionFactory));
CciTemplate ct = new CciTemplate(connectionFactory);
ct.execute(interactionSpec, record, record);
throw new DataRetrievalFailureException("error");
}
});
} catch (Exception ex) {
}
verify(localTransaction).begin();
verify(interaction).close();
verify(localTransaction).rollback();
verify(connection).close();
}
use of org.springframework.transaction.TransactionStatus in project spring-framework by spring-projects.
the class WebSphereUowTransactionManagerTests method newTransactionWithRollbackOnly.
@Test
public void newTransactionWithRollbackOnly() {
MockUOWManager manager = new MockUOWManager();
WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager(manager);
DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
assertEquals("result", ptm.execute(definition, new TransactionCallback<String>() {
@Override
public String doInTransaction(TransactionStatus status) {
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
status.setRollbackOnly();
return "result";
}
}));
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
assertEquals(0, manager.getUOWTimeout());
assertEquals(UOWManager.UOW_TYPE_GLOBAL_TRANSACTION, manager.getUOWType());
assertFalse(manager.getJoined());
assertTrue(manager.getRollbackOnly());
}
use of org.springframework.transaction.TransactionStatus in project spring-framework by spring-projects.
the class WebSphereUowTransactionManagerTests method propagationNeverFailsInCaseOfExistingTransaction.
@Test
public void propagationNeverFailsInCaseOfExistingTransaction() {
MockUOWManager manager = new MockUOWManager();
manager.setUOWStatus(UOWManager.UOW_STATUS_ACTIVE);
WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager(manager);
DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_NEVER);
try {
ptm.execute(definition, new TransactionCallback<String>() {
@Override
public String doInTransaction(TransactionStatus status) {
return "result";
}
});
fail("Should have thrown IllegalTransactionStateException");
} catch (IllegalTransactionStateException ex) {
// expected
}
}
use of org.springframework.transaction.TransactionStatus in project spring-framework by spring-projects.
the class WebSphereUowTransactionManagerTests method doTestExistingTransactionWithParticipation.
private void doTestExistingTransactionWithParticipation(int propagationBehavior) {
MockUOWManager manager = new MockUOWManager();
final WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager(manager);
DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
final DefaultTransactionDefinition definition2 = new DefaultTransactionDefinition();
definition2.setPropagationBehavior(propagationBehavior);
definition2.setReadOnly(true);
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
assertEquals("result", ptm.execute(definition, new TransactionCallback<String>() {
@Override
public String doInTransaction(TransactionStatus status) {
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
assertEquals("result2", ptm.execute(definition2, new TransactionCallback<String>() {
@Override
public String doInTransaction(TransactionStatus status) {
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
return "result2";
}
}));
return "result";
}
}));
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
assertEquals(0, manager.getUOWTimeout());
assertEquals(UOWManager.UOW_TYPE_GLOBAL_TRANSACTION, manager.getUOWType());
assertTrue(manager.getJoined());
assertFalse(manager.getRollbackOnly());
}
use of org.springframework.transaction.TransactionStatus in project spring-framework by spring-projects.
the class WebSphereUowTransactionManagerTests method newTransactionWithRollback.
@Test
public void newTransactionWithRollback() {
MockUOWManager manager = new MockUOWManager();
WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager(manager);
DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
try {
ptm.execute(definition, new TransactionCallback<String>() {
@Override
public String doInTransaction(TransactionStatus status) {
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
throw new OptimisticLockingFailureException("");
}
});
fail("Should have thrown OptimisticLockingFailureException");
} catch (OptimisticLockingFailureException ex) {
// expected
}
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
assertEquals(0, manager.getUOWTimeout());
assertEquals(UOWManager.UOW_TYPE_GLOBAL_TRANSACTION, manager.getUOWType());
assertFalse(manager.getJoined());
assertTrue(manager.getRollbackOnly());
}
Aggregations