use of org.springframework.dao.OptimisticLockingFailureException 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());
}
use of org.springframework.dao.OptimisticLockingFailureException in project spring-framework by spring-projects.
the class LocalContainerEntityManagerFactoryBeanTests method testApplicationManagedEntityManagerWithTransactionAndCommitException.
@Test
public void testApplicationManagedEntityManagerWithTransactionAndCommitException() throws Exception {
Object testEntity = new Object();
EntityTransaction mockTx = mock(EntityTransaction.class);
willThrow(new OptimisticLockException()).given(mockTx).commit();
// This one's for the tx (shared)
EntityManager sharedEm = mock(EntityManager.class);
given(sharedEm.getTransaction()).willReturn(new NoOpEntityTransaction());
// This is the application-specific one
EntityManager mockEm = mock(EntityManager.class);
given(mockEm.getTransaction()).willReturn(mockTx);
given(mockEmf.createEntityManager()).willReturn(sharedEm, mockEm);
LocalContainerEntityManagerFactoryBean cefb = parseValidPersistenceUnit();
JpaTransactionManager jpatm = new JpaTransactionManager();
jpatm.setEntityManagerFactory(cefb.getObject());
TransactionStatus txStatus = jpatm.getTransaction(new DefaultTransactionAttribute());
EntityManagerFactory emf = cefb.getObject();
assertSame("EntityManagerFactory reference must be cached after init", emf, cefb.getObject());
assertNotSame("EMF must be proxied", mockEmf, emf);
EntityManager em = emf.createEntityManager();
em.joinTransaction();
assertFalse(em.contains(testEntity));
try {
jpatm.commit(txStatus);
fail("Should have thrown OptimisticLockingFailureException");
} catch (OptimisticLockingFailureException ex) {
// expected
}
cefb.destroy();
verify(mockTx).begin();
verify(mockEm).contains(testEntity);
verify(mockEmf).close();
}
use of org.springframework.dao.OptimisticLockingFailureException in project spring-framework by spring-projects.
the class ChainedPersistenceExceptionTranslatorTests method exceptionTranslationWithTranslation.
@Test
public void exceptionTranslationWithTranslation() {
MapPersistenceExceptionTranslator mpet1 = new MapPersistenceExceptionTranslator();
RuntimeException in1 = new RuntimeException("in");
InvalidDataAccessApiUsageException out1 = new InvalidDataAccessApiUsageException("out");
InvalidDataAccessApiUsageException out2 = new InvalidDataAccessApiUsageException("out");
mpet1.addTranslation(in1, out1);
ChainedPersistenceExceptionTranslator chainedPet1 = new ChainedPersistenceExceptionTranslator();
assertSame("Should not translate yet", in1, DataAccessUtils.translateIfNecessary(in1, chainedPet1));
chainedPet1.addDelegate(mpet1);
assertSame("Should now translate", out1, DataAccessUtils.translateIfNecessary(in1, chainedPet1));
// Now add a new translator and verify it wins
MapPersistenceExceptionTranslator mpet2 = new MapPersistenceExceptionTranslator();
mpet2.addTranslation(in1, out2);
chainedPet1.addDelegate(mpet2);
assertSame("Should still translate the same due to ordering", out1, DataAccessUtils.translateIfNecessary(in1, chainedPet1));
ChainedPersistenceExceptionTranslator chainedPet2 = new ChainedPersistenceExceptionTranslator();
chainedPet2.addDelegate(mpet2);
chainedPet2.addDelegate(mpet1);
assertSame("Should translate differently due to ordering", out2, DataAccessUtils.translateIfNecessary(in1, chainedPet2));
RuntimeException in2 = new RuntimeException("in2");
OptimisticLockingFailureException out3 = new OptimisticLockingFailureException("out2");
assertNull(chainedPet2.translateExceptionIfPossible(in2));
MapPersistenceExceptionTranslator mpet3 = new MapPersistenceExceptionTranslator();
mpet3.addTranslation(in2, out3);
chainedPet2.addDelegate(mpet3);
assertSame(out3, chainedPet2.translateExceptionIfPossible(in2));
}
use of org.springframework.dao.OptimisticLockingFailureException in project spring-framework by spring-projects.
the class JtaTransactionManagerTests method jtaTransactionManagerWithExistingTransactionAndCommitException.
@Test
public void jtaTransactionManagerWithExistingTransactionAndCommitException() throws Exception {
UserTransaction ut = mock(UserTransaction.class);
given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
final TransactionSynchronization synch = mock(TransactionSynchronization.class);
willThrow(new OptimisticLockingFailureException("")).given(synch).beforeCommit(false);
JtaTransactionManager ptm = newJtaTransactionManager(ut);
TransactionTemplate tt = new TransactionTemplate(ptm);
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
try {
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
TransactionSynchronizationManager.registerSynchronization(synch);
}
});
fail("Should have thrown OptimisticLockingFailureException");
} catch (OptimisticLockingFailureException ex) {
// expected
}
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
verify(ut).setRollbackOnly();
verify(synch).beforeCompletion();
verify(synch).afterCompletion(TransactionSynchronization.STATUS_UNKNOWN);
}
use of org.springframework.dao.OptimisticLockingFailureException in project spring-framework by spring-projects.
the class AbstractTransactionAspectTests method transactionExceptionPropagatedWithCallbackPreference.
@Test
public void transactionExceptionPropagatedWithCallbackPreference() throws Throwable {
TransactionAttribute txatt = new DefaultTransactionAttribute();
MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
tas.register(exceptionalMethod, txatt);
MockCallbackPreferringTransactionManager ptm = new MockCallbackPreferringTransactionManager();
TestBean tb = new TestBean();
ITestBean itb = (ITestBean) advised(tb, ptm, tas);
checkTransactionStatus(false);
try {
itb.exceptional(new OptimisticLockingFailureException(""));
fail("Should have thrown OptimisticLockingFailureException");
} catch (OptimisticLockingFailureException ex) {
// expected
}
checkTransactionStatus(false);
assertSame(txatt, ptm.getDefinition());
assertFalse(ptm.getStatus().isRollbackOnly());
}
Aggregations