Search in sources :

Example 1 with TransactionTimedOutException

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

use of cn.taketoday.transaction.TransactionTimedOutException in project today-framework 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 3 with TransactionTimedOutException

use of cn.taketoday.transaction.TransactionTimedOutException in project today-framework by TAKETODAY.

the class DataSourceTransactionManagerTests 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 TransactionTimedOutException

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

the class DataSourceTransactionManagerTests 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)

Aggregations

EnabledForTestGroups (cn.taketoday.core.testfixture.EnabledForTestGroups)4 DataAccessResourceFailureException (cn.taketoday.dao.DataAccessResourceFailureException)4 UncategorizedSQLException (cn.taketoday.jdbc.UncategorizedSQLException)4 TransactionStatus (cn.taketoday.transaction.TransactionStatus)4 TransactionTimedOutException (cn.taketoday.transaction.TransactionTimedOutException)4 TransactionCallbackWithoutResult (cn.taketoday.transaction.support.TransactionCallbackWithoutResult)4 TransactionTemplate (cn.taketoday.transaction.support.TransactionTemplate)4 Connection (java.sql.Connection)4 PreparedStatement (java.sql.PreparedStatement)4 SQLException (java.sql.SQLException)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 ValueSource (org.junit.jupiter.params.provider.ValueSource)4 InOrder (org.mockito.InOrder)4