Search in sources :

Example 1 with TransactionTimedOutException

use of org.springframework.transaction.TransactionTimedOutException in project spring-framework by spring-projects.

the class DataSourceTransactionManagerTests method doTestTransactionWithTimeout.

private void doTestTransactionWithTimeout(int timeout) throws Exception {
    Assume.group(TestGroup.PERFORMANCE);
    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);
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
    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;
        }
    }
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
    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(org.springframework.transaction.TransactionTimedOutException) InOrder(org.mockito.InOrder) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) SQLException(java.sql.SQLException) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) Connection(java.sql.Connection) TransactionStatus(org.springframework.transaction.TransactionStatus) PreparedStatement(java.sql.PreparedStatement) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult)

Aggregations

Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 SQLException (java.sql.SQLException)1 InOrder (org.mockito.InOrder)1 DataAccessResourceFailureException (org.springframework.dao.DataAccessResourceFailureException)1 UncategorizedSQLException (org.springframework.jdbc.UncategorizedSQLException)1 TransactionStatus (org.springframework.transaction.TransactionStatus)1 TransactionTimedOutException (org.springframework.transaction.TransactionTimedOutException)1 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)1 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)1