Search in sources :

Example 1 with OptimisticLockingFailureException

use of cn.taketoday.dao.OptimisticLockingFailureException in project today-infrastructure by TAKETODAY.

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();
    assertThat(DataAccessUtils.translateIfNecessary(in1, chainedPet1)).as("Should not translate yet").isSameAs(in1);
    chainedPet1.addDelegate(mpet1);
    assertThat(DataAccessUtils.translateIfNecessary(in1, chainedPet1)).as("Should now translate").isSameAs(out1);
    // Now add a new translator and verify it wins
    MapPersistenceExceptionTranslator mpet2 = new MapPersistenceExceptionTranslator();
    mpet2.addTranslation(in1, out2);
    chainedPet1.addDelegate(mpet2);
    assertThat(DataAccessUtils.translateIfNecessary(in1, chainedPet1)).as("Should still translate the same due to ordering").isSameAs(out1);
    ChainedPersistenceExceptionTranslator chainedPet2 = new ChainedPersistenceExceptionTranslator();
    chainedPet2.addDelegate(mpet2);
    chainedPet2.addDelegate(mpet1);
    assertThat(DataAccessUtils.translateIfNecessary(in1, chainedPet2)).as("Should translate differently due to ordering").isSameAs(out2);
    RuntimeException in2 = new RuntimeException("in2");
    OptimisticLockingFailureException out3 = new OptimisticLockingFailureException("out2");
    assertThat(chainedPet2.translateExceptionIfPossible(in2)).isNull();
    MapPersistenceExceptionTranslator mpet3 = new MapPersistenceExceptionTranslator();
    mpet3.addTranslation(in2, out3);
    chainedPet2.addDelegate(mpet3);
    assertThat(chainedPet2.translateExceptionIfPossible(in2)).isSameAs(out3);
}
Also used : OptimisticLockingFailureException(cn.taketoday.dao.OptimisticLockingFailureException) MapPersistenceExceptionTranslator(cn.taketoday.dao.support.DataAccessUtilsTests.MapPersistenceExceptionTranslator) InvalidDataAccessApiUsageException(cn.taketoday.dao.InvalidDataAccessApiUsageException) Test(org.junit.jupiter.api.Test)

Example 2 with OptimisticLockingFailureException

use of cn.taketoday.dao.OptimisticLockingFailureException in project today-infrastructure by TAKETODAY.

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);
    assertThatExceptionOfType(OptimisticLockingFailureException.class).isThrownBy(() -> itb.exceptional(new OptimisticLockingFailureException("")));
    checkTransactionStatus(false);
    assertThat(ptm.getDefinition()).isSameAs(txatt);
    assertThat(ptm.getStatus().isRollbackOnly()).isFalse();
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) OptimisticLockingFailureException(cn.taketoday.dao.OptimisticLockingFailureException) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) MockCallbackPreferringTransactionManager(cn.taketoday.transaction.MockCallbackPreferringTransactionManager) Test(org.junit.jupiter.api.Test)

Example 3 with OptimisticLockingFailureException

use of cn.taketoday.dao.OptimisticLockingFailureException in project today-framework by TAKETODAY.

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();
    assertThat(DataAccessUtils.translateIfNecessary(in1, chainedPet1)).as("Should not translate yet").isSameAs(in1);
    chainedPet1.addDelegate(mpet1);
    assertThat(DataAccessUtils.translateIfNecessary(in1, chainedPet1)).as("Should now translate").isSameAs(out1);
    // Now add a new translator and verify it wins
    MapPersistenceExceptionTranslator mpet2 = new MapPersistenceExceptionTranslator();
    mpet2.addTranslation(in1, out2);
    chainedPet1.addDelegate(mpet2);
    assertThat(DataAccessUtils.translateIfNecessary(in1, chainedPet1)).as("Should still translate the same due to ordering").isSameAs(out1);
    ChainedPersistenceExceptionTranslator chainedPet2 = new ChainedPersistenceExceptionTranslator();
    chainedPet2.addDelegate(mpet2);
    chainedPet2.addDelegate(mpet1);
    assertThat(DataAccessUtils.translateIfNecessary(in1, chainedPet2)).as("Should translate differently due to ordering").isSameAs(out2);
    RuntimeException in2 = new RuntimeException("in2");
    OptimisticLockingFailureException out3 = new OptimisticLockingFailureException("out2");
    assertThat(chainedPet2.translateExceptionIfPossible(in2)).isNull();
    MapPersistenceExceptionTranslator mpet3 = new MapPersistenceExceptionTranslator();
    mpet3.addTranslation(in2, out3);
    chainedPet2.addDelegate(mpet3);
    assertThat(chainedPet2.translateExceptionIfPossible(in2)).isSameAs(out3);
}
Also used : OptimisticLockingFailureException(cn.taketoday.dao.OptimisticLockingFailureException) MapPersistenceExceptionTranslator(cn.taketoday.dao.support.DataAccessUtilsTests.MapPersistenceExceptionTranslator) InvalidDataAccessApiUsageException(cn.taketoday.dao.InvalidDataAccessApiUsageException) Test(org.junit.jupiter.api.Test)

Example 4 with OptimisticLockingFailureException

use of cn.taketoday.dao.OptimisticLockingFailureException in project today-framework by TAKETODAY.

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);
    assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
    assertThatExceptionOfType(OptimisticLockingFailureException.class).isThrownBy(() -> tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
            TransactionSynchronizationManager.registerSynchronization(synch);
        }
    }));
    assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
    verify(ut).setRollbackOnly();
    verify(synch).beforeCompletion();
    verify(synch).afterCompletion(TransactionSynchronization.STATUS_UNKNOWN);
}
Also used : UserTransaction(jakarta.transaction.UserTransaction) OptimisticLockingFailureException(cn.taketoday.dao.OptimisticLockingFailureException) JtaTransactionManager(cn.taketoday.transaction.jta.JtaTransactionManager) TransactionSynchronization(cn.taketoday.transaction.support.TransactionSynchronization) TransactionTemplate(cn.taketoday.transaction.support.TransactionTemplate) TransactionCallbackWithoutResult(cn.taketoday.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test)

Example 5 with OptimisticLockingFailureException

use of cn.taketoday.dao.OptimisticLockingFailureException in project today-framework by TAKETODAY.

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);
    assertThatExceptionOfType(OptimisticLockingFailureException.class).isThrownBy(() -> itb.exceptional(new OptimisticLockingFailureException("")));
    checkTransactionStatus(false);
    assertThat(ptm.getDefinition()).isSameAs(txatt);
    assertThat(ptm.getStatus().isRollbackOnly()).isFalse();
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) OptimisticLockingFailureException(cn.taketoday.dao.OptimisticLockingFailureException) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) MockCallbackPreferringTransactionManager(cn.taketoday.transaction.MockCallbackPreferringTransactionManager) Test(org.junit.jupiter.api.Test)

Aggregations

OptimisticLockingFailureException (cn.taketoday.dao.OptimisticLockingFailureException)6 Test (org.junit.jupiter.api.Test)6 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)2 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)2 InvalidDataAccessApiUsageException (cn.taketoday.dao.InvalidDataAccessApiUsageException)2 MapPersistenceExceptionTranslator (cn.taketoday.dao.support.DataAccessUtilsTests.MapPersistenceExceptionTranslator)2 MockCallbackPreferringTransactionManager (cn.taketoday.transaction.MockCallbackPreferringTransactionManager)2 JtaTransactionManager (cn.taketoday.transaction.jta.JtaTransactionManager)2 TransactionCallbackWithoutResult (cn.taketoday.transaction.support.TransactionCallbackWithoutResult)2 TransactionSynchronization (cn.taketoday.transaction.support.TransactionSynchronization)2 TransactionTemplate (cn.taketoday.transaction.support.TransactionTemplate)2 UserTransaction (jakarta.transaction.UserTransaction)2