Search in sources :

Example 1 with TransactionStatus

use of cn.taketoday.transaction.TransactionStatus in project today-infrastructure by TAKETODAY.

the class LocalContainerEntityManagerFactoryBeanTests method testApplicationManagedEntityManagerWithJtaTransaction.

@Test
public void testApplicationManagedEntityManagerWithJtaTransaction() throws Exception {
    Object testEntity = new Object();
    // 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(mockEmf.createEntityManager()).willReturn(sharedEm, mockEm);
    LocalContainerEntityManagerFactoryBean cefb = parseValidPersistenceUnit();
    MutablePersistenceUnitInfo pui = ((MutablePersistenceUnitInfo) cefb.getPersistenceUnitInfo());
    pui.setTransactionType(PersistenceUnitTransactionType.JTA);
    JpaTransactionManager jpatm = new JpaTransactionManager();
    jpatm.setEntityManagerFactory(cefb.getObject());
    TransactionStatus txStatus = jpatm.getTransaction(new DefaultTransactionAttribute());
    EntityManagerFactory emf = cefb.getObject();
    assertThat(cefb.getObject()).as("EntityManagerFactory reference must be cached after init").isSameAs(emf);
    assertThat(emf).as("EMF must be proxied").isNotSameAs(mockEmf);
    EntityManager em = emf.createEntityManager();
    em.joinTransaction();
    assertThat(em.contains(testEntity)).isFalse();
    jpatm.commit(txStatus);
    cefb.destroy();
    verify(mockEm).joinTransaction();
    verify(mockEm).contains(testEntity);
    verify(mockEmf).close();
}
Also used : EntityManager(jakarta.persistence.EntityManager) DefaultTransactionAttribute(cn.taketoday.transaction.interceptor.DefaultTransactionAttribute) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) TransactionStatus(cn.taketoday.transaction.TransactionStatus) MutablePersistenceUnitInfo(cn.taketoday.orm.jpa.persistenceunit.MutablePersistenceUnitInfo) Test(org.junit.jupiter.api.Test)

Example 2 with TransactionStatus

use of cn.taketoday.transaction.TransactionStatus in project today-infrastructure by TAKETODAY.

the class LocalContainerEntityManagerFactoryBeanTests method testApplicationManagedEntityManagerWithTransaction.

@Test
public void testApplicationManagedEntityManagerWithTransaction() throws Exception {
    Object testEntity = new Object();
    EntityTransaction mockTx = mock(EntityTransaction.class);
    // 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();
    assertThat(cefb.getObject()).as("EntityManagerFactory reference must be cached after init").isSameAs(emf);
    assertThat(emf).as("EMF must be proxied").isNotSameAs(mockEmf);
    EntityManager em = emf.createEntityManager();
    em.joinTransaction();
    assertThat(em.contains(testEntity)).isFalse();
    jpatm.commit(txStatus);
    cefb.destroy();
    verify(mockTx).begin();
    verify(mockTx).commit();
    verify(mockEm).contains(testEntity);
    verify(mockEmf).close();
}
Also used : EntityTransaction(jakarta.persistence.EntityTransaction) EntityManager(jakarta.persistence.EntityManager) DefaultTransactionAttribute(cn.taketoday.transaction.interceptor.DefaultTransactionAttribute) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) TransactionStatus(cn.taketoday.transaction.TransactionStatus) Test(org.junit.jupiter.api.Test)

Example 3 with TransactionStatus

use of cn.taketoday.transaction.TransactionStatus in project today-infrastructure by TAKETODAY.

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();
    assertThat(cefb.getObject()).as("EntityManagerFactory reference must be cached after init").isSameAs(emf);
    assertThat(emf).as("EMF must be proxied").isNotSameAs(mockEmf);
    EntityManager em = emf.createEntityManager();
    em.joinTransaction();
    assertThat(em.contains(testEntity)).isFalse();
    assertThatExceptionOfType(OptimisticLockingFailureException.class).isThrownBy(() -> jpatm.commit(txStatus));
    cefb.destroy();
    verify(mockTx).begin();
    verify(mockEm).contains(testEntity);
    verify(mockEmf).close();
}
Also used : EntityTransaction(jakarta.persistence.EntityTransaction) EntityManager(jakarta.persistence.EntityManager) OptimisticLockingFailureException(cn.taketoday.dao.OptimisticLockingFailureException) DefaultTransactionAttribute(cn.taketoday.transaction.interceptor.DefaultTransactionAttribute) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) OptimisticLockException(jakarta.persistence.OptimisticLockException) TransactionStatus(cn.taketoday.transaction.TransactionStatus) Test(org.junit.jupiter.api.Test)

Example 4 with TransactionStatus

use of cn.taketoday.transaction.TransactionStatus in project today-infrastructure by TAKETODAY.

the class TransactionTemplate method execute.

@Override
@Nullable
public <T> T execute(TransactionCallback<T> action) throws TransactionException {
    PlatformTransactionManager transactionManager = getTransactionManager();
    Assert.state(transactionManager != null, "No PlatformTransactionManager set");
    if (transactionManager instanceof CallbackPreferringPlatformTransactionManager) {
        return ((CallbackPreferringPlatformTransactionManager) transactionManager).execute(this, action);
    } else {
        TransactionStatus status = transactionManager.getTransaction(this);
        T result;
        try {
            result = action.doInTransaction(status);
        } catch (RuntimeException | Error ex) {
            // Transactional code threw application exception -> rollback
            rollbackOnException(transactionManager, status, ex);
            throw ex;
        } catch (Throwable ex) {
            // Transactional code threw unexpected exception -> rollback
            rollbackOnException(transactionManager, status, ex);
            throw new UndeclaredThrowableException(ex, "TransactionCallback threw undeclared checked exception");
        }
        transactionManager.commit(status);
        return result;
    }
}
Also used : UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) TransactionStatus(cn.taketoday.transaction.TransactionStatus) PlatformTransactionManager(cn.taketoday.transaction.PlatformTransactionManager) Nullable(cn.taketoday.lang.Nullable)

Example 5 with TransactionStatus

use of cn.taketoday.transaction.TransactionStatus in project today-infrastructure by TAKETODAY.

the class JdbcTransactionManagerTests method testPropagationNeverWithExistingTransaction.

@Test
public void testPropagationNeverWithExistingTransaction() throws Exception {
    final TransactionTemplate tt = new TransactionTemplate(tm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    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(() -> tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
            assertThat(status.isNewTransaction()).as("Is new transaction").isTrue();
            tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NEVER);
            tt.execute(new TransactionCallbackWithoutResult() {

                @Override
                protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
                    fail("Should have thrown IllegalTransactionStateException");
                }
            });
            fail("Should have thrown IllegalTransactionStateException");
        }
    }));
    boolean condition = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition).as("Hasn't thread connection").isTrue();
    verify(con).rollback();
    verify(con).close();
}
Also used : TransactionTemplate(cn.taketoday.transaction.support.TransactionTemplate) IllegalTransactionStateException(cn.taketoday.transaction.IllegalTransactionStateException) TransactionStatus(cn.taketoday.transaction.TransactionStatus) TransactionCallbackWithoutResult(cn.taketoday.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

TransactionStatus (cn.taketoday.transaction.TransactionStatus)250 Test (org.junit.jupiter.api.Test)212 TransactionCallbackWithoutResult (cn.taketoday.transaction.support.TransactionCallbackWithoutResult)172 TransactionTemplate (cn.taketoday.transaction.support.TransactionTemplate)170 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)140 SQLException (java.sql.SQLException)60 DefaultTransactionDefinition (cn.taketoday.transaction.support.DefaultTransactionDefinition)58 UncategorizedSQLException (cn.taketoday.jdbc.UncategorizedSQLException)56 Connection (java.sql.Connection)42 InOrder (org.mockito.InOrder)40 DataSource (javax.sql.DataSource)28 PlatformTransactionManager (cn.taketoday.transaction.PlatformTransactionManager)24 DatabaseMetaData (java.sql.DatabaseMetaData)24 Savepoint (java.sql.Savepoint)24 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)16 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)16 TransactionSystemException (cn.taketoday.transaction.TransactionSystemException)14 JtaTransactionManager (cn.taketoday.transaction.jta.JtaTransactionManager)14 IllegalTransactionStateException (cn.taketoday.transaction.IllegalTransactionStateException)12 UnexpectedRollbackException (cn.taketoday.transaction.UnexpectedRollbackException)10