Search in sources :

Example 1 with TransactionTemplate

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

the class PersistenceContextTransactionTests method setup.

@BeforeEach
public void setup() {
    factory = mock(EntityManagerFactory.class);
    manager = mock(EntityManager.class);
    tx = mock(EntityTransaction.class);
    JpaTransactionManager tm = new JpaTransactionManager(factory);
    tt = new TransactionTemplate(tm);
    given(factory.createEntityManager()).willReturn(manager);
    given(manager.getTransaction()).willReturn(tx);
    given(manager.isOpen()).willReturn(true);
    bean = new EntityManagerHoldingBean();
    @SuppressWarnings("serial") PersistenceAnnotationBeanPostProcessor pabpp = new PersistenceAnnotationBeanPostProcessor() {

        @Override
        protected EntityManagerFactory findEntityManagerFactory(@Nullable String unitName, String requestingBeanName) {
            return factory;
        }
    };
    pabpp.processDependencies(null, bean, "bean");
    assertThat(TransactionSynchronizationManager.getResourceMap().isEmpty()).isTrue();
    assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
}
Also used : EntityTransaction(jakarta.persistence.EntityTransaction) EntityManager(jakarta.persistence.EntityManager) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) JpaTransactionManager(cn.taketoday.orm.jpa.JpaTransactionManager) TransactionTemplate(cn.taketoday.transaction.support.TransactionTemplate) Nullable(cn.taketoday.lang.Nullable) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with TransactionTemplate

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

the class JpaTransactionManagerTests method testPropagationSupportsAndRequiresNew.

@Test
public void testPropagationSupportsAndRequiresNew() {
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
    given(manager.getTransaction()).willReturn(tx);
    final List<String> l = new ArrayList<>();
    l.add("test");
    boolean condition3 = !TransactionSynchronizationManager.hasResource(factory);
    assertThat(condition3).isTrue();
    boolean condition2 = !TransactionSynchronizationManager.isSynchronizationActive();
    assertThat(condition2).isTrue();
    Object result = tt.execute(status -> {
        assertThat(TransactionSynchronizationManager.hasResource(factory)).isFalse();
        TransactionTemplate tt2 = new TransactionTemplate(tm);
        tt2.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
        return tt2.execute(status1 -> {
            EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
            return l;
        });
    });
    assertThat(result).isSameAs(l);
    boolean condition1 = !TransactionSynchronizationManager.hasResource(factory);
    assertThat(condition1).isTrue();
    boolean condition = !TransactionSynchronizationManager.isSynchronizationActive();
    assertThat(condition).isTrue();
    verify(tx).commit();
    verify(manager).flush();
    verify(manager).close();
}
Also used : ArrayList(java.util.ArrayList) TransactionTemplate(cn.taketoday.transaction.support.TransactionTemplate) Test(org.junit.jupiter.api.Test)

Example 3 with TransactionTemplate

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

the class JpaTransactionManagerTests method testPropagationSupportsAndRequiresNewAndEarlyAccess.

@Test
public void testPropagationSupportsAndRequiresNewAndEarlyAccess() {
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
    given(factory.createEntityManager()).willReturn(manager);
    given(manager.getTransaction()).willReturn(tx);
    given(manager.isOpen()).willReturn(true);
    final List<String> l = new ArrayList<>();
    l.add("test");
    boolean condition3 = !TransactionSynchronizationManager.hasResource(factory);
    assertThat(condition3).isTrue();
    boolean condition2 = !TransactionSynchronizationManager.isSynchronizationActive();
    assertThat(condition2).isTrue();
    Object result = tt.execute(status -> {
        EntityManagerFactoryUtils.getTransactionalEntityManager(factory);
        assertThat(TransactionSynchronizationManager.hasResource(factory)).isTrue();
        TransactionTemplate tt2 = new TransactionTemplate(tm);
        tt2.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
        return tt2.execute(status1 -> {
            EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
            return l;
        });
    });
    assertThat(result).isSameAs(l);
    boolean condition1 = !TransactionSynchronizationManager.hasResource(factory);
    assertThat(condition1).isTrue();
    boolean condition = !TransactionSynchronizationManager.isSynchronizationActive();
    assertThat(condition).isTrue();
    verify(tx).commit();
    verify(manager).flush();
    verify(manager, times(2)).close();
}
Also used : ArrayList(java.util.ArrayList) TransactionTemplate(cn.taketoday.transaction.support.TransactionTemplate) Test(org.junit.jupiter.api.Test)

Example 4 with TransactionTemplate

use of cn.taketoday.transaction.support.TransactionTemplate 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)

Example 5 with TransactionTemplate

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

the class JdbcTransactionManagerTests method testTransactionWithExceptionOnBegin.

/**
 * Test behavior if the first operation on a connection (getAutoCommit) throws SQLException.
 */
@Test
public void testTransactionWithExceptionOnBegin() throws Exception {
    willThrow(new SQLException("Cannot begin")).given(con).getAutoCommit();
    TransactionTemplate tt = new TransactionTemplate(tm);
    assertThatExceptionOfType(CannotCreateTransactionException.class).isThrownBy(() -> tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
        // something transactional
        }
    }));
    boolean condition = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition).as("Hasn't thread connection").isTrue();
    verify(con).close();
}
Also used : SQLException(java.sql.SQLException) UncategorizedSQLException(cn.taketoday.jdbc.UncategorizedSQLException) CannotCreateTransactionException(cn.taketoday.transaction.CannotCreateTransactionException) TransactionTemplate(cn.taketoday.transaction.support.TransactionTemplate) TransactionStatus(cn.taketoday.transaction.TransactionStatus) TransactionCallbackWithoutResult(cn.taketoday.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

TransactionTemplate (cn.taketoday.transaction.support.TransactionTemplate)280 TransactionCallbackWithoutResult (cn.taketoday.transaction.support.TransactionCallbackWithoutResult)266 Test (org.junit.jupiter.api.Test)240 TransactionStatus (cn.taketoday.transaction.TransactionStatus)170 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)140 JtaTransactionManager (cn.taketoday.transaction.jta.JtaTransactionManager)100 UserTransaction (jakarta.transaction.UserTransaction)86 SQLException (java.sql.SQLException)60 UncategorizedSQLException (cn.taketoday.jdbc.UncategorizedSQLException)56 TransactionSynchronization (cn.taketoday.transaction.support.TransactionSynchronization)44 Connection (java.sql.Connection)42 InOrder (org.mockito.InOrder)40 DataSource (javax.sql.DataSource)30 DatabaseMetaData (java.sql.DatabaseMetaData)24 Savepoint (java.sql.Savepoint)24 TransactionManager (jakarta.transaction.TransactionManager)18 SystemException (jakarta.transaction.SystemException)16 Assertions.assertThatIllegalStateException (org.assertj.core.api.Assertions.assertThatIllegalStateException)14 IllegalTransactionStateException (cn.taketoday.transaction.IllegalTransactionStateException)12 TransactionSystemException (cn.taketoday.transaction.TransactionSystemException)12