Search in sources :

Example 1 with TransactionCallbackWithoutResult

use of cn.taketoday.transaction.support.TransactionCallbackWithoutResult 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 2 with TransactionCallbackWithoutResult

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

Example 3 with TransactionCallbackWithoutResult

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

the class JdbcTransactionManagerTests method transactionWithTimeout.

@ParameterizedTest(name = "transaction with {0} second timeout")
@ValueSource(ints = { 1, 10 })
@EnabledForTestGroups(LONG_RUNNING)
public void transactionWithTimeout(int timeout) throws Exception {
    PreparedStatement ps = mock(PreparedStatement.class);
    given(con.getAutoCommit()).willReturn(true);
    given(con.prepareStatement("some SQL statement")).willReturn(ps);
    TransactionTemplate tt = new TransactionTemplate(tm);
    tt.setTimeout(timeout);
    boolean condition1 = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition1).as("Hasn't thread connection").isTrue();
    try {
        tt.execute(new TransactionCallbackWithoutResult() {

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                try {
                    Thread.sleep(1500);
                } catch (InterruptedException ex) {
                }
                try {
                    Connection con = DataSourceUtils.getConnection(ds);
                    PreparedStatement ps = con.prepareStatement("some SQL statement");
                    DataSourceUtils.applyTransactionTimeout(ps, ds);
                } catch (SQLException ex) {
                    throw new DataAccessResourceFailureException("", ex);
                }
            }
        });
        if (timeout <= 1) {
            fail("Should have thrown TransactionTimedOutException");
        }
    } catch (TransactionTimedOutException ex) {
        if (timeout <= 1) {
        // expected
        } else {
            throw ex;
        }
    }
    boolean condition = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition).as("Hasn't thread connection").isTrue();
    if (timeout > 1) {
        verify(ps).setQueryTimeout(timeout - 1);
        verify(con).commit();
    } else {
        verify(con).rollback();
    }
    InOrder ordered = inOrder(con);
    ordered.verify(con).setAutoCommit(false);
    ordered.verify(con).setAutoCommit(true);
    verify(con).close();
}
Also used : TransactionTimedOutException(cn.taketoday.transaction.TransactionTimedOutException) InOrder(org.mockito.InOrder) SQLException(java.sql.SQLException) UncategorizedSQLException(cn.taketoday.jdbc.UncategorizedSQLException) DataAccessResourceFailureException(cn.taketoday.dao.DataAccessResourceFailureException) TransactionTemplate(cn.taketoday.transaction.support.TransactionTemplate) Connection(java.sql.Connection) TransactionStatus(cn.taketoday.transaction.TransactionStatus) PreparedStatement(java.sql.PreparedStatement) TransactionCallbackWithoutResult(cn.taketoday.transaction.support.TransactionCallbackWithoutResult) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) EnabledForTestGroups(cn.taketoday.core.testfixture.EnabledForTestGroups)

Example 4 with TransactionCallbackWithoutResult

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

the class JdbcTransactionManagerTests method testExistingTransactionWithPropagationNestedAndRequiredRollback.

@Test
public void testExistingTransactionWithPropagationNestedAndRequiredRollback() throws Exception {
    DatabaseMetaData md = mock(DatabaseMetaData.class);
    Savepoint sp = mock(Savepoint.class);
    given(md.supportsSavepoints()).willReturn(true);
    given(con.getMetaData()).willReturn(md);
    given(con.setSavepoint("SAVEPOINT_1")).willReturn(sp);
    final TransactionTemplate tt = new TransactionTemplate(tm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);
    boolean condition2 = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition2).as("Hasn't thread connection").isTrue();
    boolean condition1 = !TransactionSynchronizationManager.isSynchronizationActive();
    assertThat(condition1).as("Synchronization not active").isTrue();
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
            assertThat(status.isNewTransaction()).as("Is new transaction").isTrue();
            boolean condition1 = !status.hasSavepoint();
            assertThat(condition1).as("Isn't nested transaction").isTrue();
            assertThatIllegalStateException().isThrownBy(() -> tt.execute(new TransactionCallbackWithoutResult() {

                @Override
                protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
                    assertThat(TransactionSynchronizationManager.hasResource(ds)).as("Has thread connection").isTrue();
                    assertThat(TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization active").isTrue();
                    boolean condition = !status.isNewTransaction();
                    assertThat(condition).as("Isn't new transaction").isTrue();
                    assertThat(status.hasSavepoint()).as("Is nested transaction").isTrue();
                    TransactionTemplate ntt = new TransactionTemplate(tm);
                    ntt.execute(new TransactionCallbackWithoutResult() {

                        @Override
                        protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
                            assertThat(TransactionSynchronizationManager.hasResource(ds)).as("Has thread connection").isTrue();
                            assertThat(TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization active").isTrue();
                            boolean condition1 = !status.isNewTransaction();
                            assertThat(condition1).as("Isn't new transaction").isTrue();
                            boolean condition = !status.hasSavepoint();
                            assertThat(condition).as("Is regular transaction").isTrue();
                            throw new IllegalStateException();
                        }
                    });
                }
            }));
            assertThat(status.isNewTransaction()).as("Is new transaction").isTrue();
            boolean condition = !status.hasSavepoint();
            assertThat(condition).as("Isn't nested transaction").isTrue();
        }
    });
    boolean condition = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition).as("Hasn't thread connection").isTrue();
    verify(con).rollback(sp);
    verify(con).releaseSavepoint(sp);
    verify(con).commit();
    verify(con).close();
}
Also used : Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) TransactionTemplate(cn.taketoday.transaction.support.TransactionTemplate) TransactionStatus(cn.taketoday.transaction.TransactionStatus) Savepoint(java.sql.Savepoint) DatabaseMetaData(java.sql.DatabaseMetaData) TransactionCallbackWithoutResult(cn.taketoday.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with TransactionCallbackWithoutResult

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

the class JdbcTransactionManagerTests method testExistingTransactionWithManualSavepoint.

@Test
public void testExistingTransactionWithManualSavepoint() throws Exception {
    DatabaseMetaData md = mock(DatabaseMetaData.class);
    Savepoint sp = mock(Savepoint.class);
    given(md.supportsSavepoints()).willReturn(true);
    given(con.getMetaData()).willReturn(md);
    given(con.setSavepoint("SAVEPOINT_1")).willReturn(sp);
    final TransactionTemplate tt = new TransactionTemplate(tm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);
    boolean condition2 = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition2).as("Hasn't thread connection").isTrue();
    boolean condition1 = !TransactionSynchronizationManager.isSynchronizationActive();
    assertThat(condition1).as("Synchronization not active").isTrue();
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
            assertThat(status.isNewTransaction()).as("Is new transaction").isTrue();
            Object savepoint = status.createSavepoint();
            status.releaseSavepoint(savepoint);
            assertThat(status.isNewTransaction()).as("Is new transaction").isTrue();
        }
    });
    boolean condition = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition).as("Hasn't thread connection").isTrue();
    verify(con).releaseSavepoint(sp);
    verify(con).commit();
    verify(con).close();
    verify(ds).getConnection();
}
Also used : TransactionTemplate(cn.taketoday.transaction.support.TransactionTemplate) TransactionStatus(cn.taketoday.transaction.TransactionStatus) Savepoint(java.sql.Savepoint) DatabaseMetaData(java.sql.DatabaseMetaData) TransactionCallbackWithoutResult(cn.taketoday.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

TransactionCallbackWithoutResult (cn.taketoday.transaction.support.TransactionCallbackWithoutResult)268 TransactionTemplate (cn.taketoday.transaction.support.TransactionTemplate)266 Test (org.junit.jupiter.api.Test)234 TransactionStatus (cn.taketoday.transaction.TransactionStatus)172 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)28 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