Search in sources :

Example 1 with OptimisticLockingFailureException

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());
}
Also used : OptimisticLockingFailureException(org.springframework.dao.OptimisticLockingFailureException) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) TransactionStatus(org.springframework.transaction.TransactionStatus) Test(org.junit.Test)

Example 2 with OptimisticLockingFailureException

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();
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) OptimisticLockingFailureException(org.springframework.dao.OptimisticLockingFailureException) DefaultTransactionAttribute(org.springframework.transaction.interceptor.DefaultTransactionAttribute) EntityManagerFactory(javax.persistence.EntityManagerFactory) OptimisticLockException(javax.persistence.OptimisticLockException) TransactionStatus(org.springframework.transaction.TransactionStatus) Test(org.junit.Test)

Example 3 with OptimisticLockingFailureException

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));
}
Also used : OptimisticLockingFailureException(org.springframework.dao.OptimisticLockingFailureException) MapPersistenceExceptionTranslator(org.springframework.dao.support.DataAccessUtilsTests.MapPersistenceExceptionTranslator) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException) Test(org.junit.Test)

Example 4 with OptimisticLockingFailureException

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);
}
Also used : UserTransaction(javax.transaction.UserTransaction) OptimisticLockingFailureException(org.springframework.dao.OptimisticLockingFailureException) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) TransactionSynchronization(org.springframework.transaction.support.TransactionSynchronization) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.Test)

Example 5 with OptimisticLockingFailureException

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());
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) OptimisticLockingFailureException(org.springframework.dao.OptimisticLockingFailureException) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) MockCallbackPreferringTransactionManager(org.springframework.transaction.MockCallbackPreferringTransactionManager) Test(org.junit.Test)

Aggregations

OptimisticLockingFailureException (org.springframework.dao.OptimisticLockingFailureException)7 Test (org.junit.Test)6 TransactionStatus (org.springframework.transaction.TransactionStatus)2 QuickBaseClient (com.intuit.quickbase.util.QuickBaseClient)1 Product (com.vladmihalcea.mongo.model.Product)1 HashMap (java.util.HashMap)1 Properties (java.util.Properties)1 EntityManager (javax.persistence.EntityManager)1 EntityManagerFactory (javax.persistence.EntityManagerFactory)1 EntityTransaction (javax.persistence.EntityTransaction)1 OptimisticLockException (javax.persistence.OptimisticLockException)1 UserTransaction (javax.transaction.UserTransaction)1 Ticket (org.opennms.api.integration.ticketing.Ticket)1 DataRetrievalFailureException (org.springframework.dao.DataRetrievalFailureException)1 InvalidDataAccessApiUsageException (org.springframework.dao.InvalidDataAccessApiUsageException)1 MapPersistenceExceptionTranslator (org.springframework.dao.support.DataAccessUtilsTests.MapPersistenceExceptionTranslator)1 ITestBean (org.springframework.tests.sample.beans.ITestBean)1 TestBean (org.springframework.tests.sample.beans.TestBean)1 MockCallbackPreferringTransactionManager (org.springframework.transaction.MockCallbackPreferringTransactionManager)1 DefaultTransactionAttribute (org.springframework.transaction.interceptor.DefaultTransactionAttribute)1