Search in sources :

Example 1 with UncategorizedSQLException

use of org.springframework.jdbc.UncategorizedSQLException in project spring-framework by spring-projects.

the class DataSourceTransactionManagerTests method doTestTransactionRollbackRestoringAutoCommit.

private void doTestTransactionRollbackRestoringAutoCommit(boolean autoCommit, boolean lazyConnection, final boolean createStatement) throws Exception {
    if (lazyConnection) {
        given(con.getAutoCommit()).willReturn(autoCommit);
        given(con.getTransactionIsolation()).willReturn(Connection.TRANSACTION_READ_COMMITTED);
    }
    if (!lazyConnection || createStatement) {
        given(con.getAutoCommit()).willReturn(autoCommit);
    }
    final DataSource dsToUse = (lazyConnection ? new LazyConnectionDataSourceProxy(ds) : ds);
    tm = new DataSourceTransactionManager(dsToUse);
    TransactionTemplate tt = new TransactionTemplate(tm);
    boolean condition3 = !TransactionSynchronizationManager.hasResource(dsToUse);
    assertThat(condition3).as("Hasn't thread connection").isTrue();
    boolean condition2 = !TransactionSynchronizationManager.isSynchronizationActive();
    assertThat(condition2).as("Synchronization not active").isTrue();
    final RuntimeException ex = new RuntimeException("Application exception");
    assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
            assertThat(TransactionSynchronizationManager.hasResource(dsToUse)).as("Has thread connection").isTrue();
            assertThat(TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization active").isTrue();
            assertThat(status.isNewTransaction()).as("Is new transaction").isTrue();
            Connection con = DataSourceUtils.getConnection(dsToUse);
            if (createStatement) {
                try {
                    con.createStatement();
                } catch (SQLException ex) {
                    throw new UncategorizedSQLException("", "", ex);
                }
            }
            throw ex;
        }
    })).isEqualTo(ex);
    boolean condition1 = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition1).as("Hasn't thread connection").isTrue();
    boolean condition = !TransactionSynchronizationManager.isSynchronizationActive();
    assertThat(condition).as("Synchronization not active").isTrue();
    if (autoCommit && (!lazyConnection || createStatement)) {
        InOrder ordered = inOrder(con);
        ordered.verify(con).setAutoCommit(false);
        ordered.verify(con).rollback();
        ordered.verify(con).setAutoCommit(true);
    }
    if (createStatement) {
        verify(con, times(2)).close();
    } else {
        verify(con).close();
    }
}
Also used : InOrder(org.mockito.InOrder) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) SQLException(java.sql.SQLException) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) Connection(java.sql.Connection) TransactionStatus(org.springframework.transaction.TransactionStatus) DataSource(javax.sql.DataSource) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult)

Example 2 with UncategorizedSQLException

use of org.springframework.jdbc.UncategorizedSQLException in project spring-framework by spring-projects.

the class DataSourceTransactionManagerTests method testTransactionAwareDataSourceProxyWithSuspensionAndReobtaining.

@Test
public void testTransactionAwareDataSourceProxyWithSuspensionAndReobtaining() throws Exception {
    given(con.getAutoCommit()).willReturn(true);
    final TransactionTemplate tt = new TransactionTemplate(tm);
    tt.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRES_NEW);
    boolean condition1 = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition1).as("Hasn't thread connection").isTrue();
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            // something transactional
            assertThat(DataSourceUtils.getConnection(ds)).isEqualTo(con);
            final TransactionAwareDataSourceProxy dsProxy = new TransactionAwareDataSourceProxy(ds);
            dsProxy.setReobtainTransactionalConnections(true);
            try {
                assertThat(((ConnectionProxy) dsProxy.getConnection()).getTargetConnection()).isEqualTo(con);
                // should be ignored
                dsProxy.getConnection().close();
            } catch (SQLException ex) {
                throw new UncategorizedSQLException("", "", ex);
            }
            tt.execute(new TransactionCallbackWithoutResult() {

                @Override
                protected void doInTransactionWithoutResult(TransactionStatus status) {
                    // something transactional
                    assertThat(DataSourceUtils.getConnection(ds)).isEqualTo(con);
                    try {
                        assertThat(((ConnectionProxy) dsProxy.getConnection()).getTargetConnection()).isEqualTo(con);
                        // should be ignored
                        dsProxy.getConnection().close();
                    } catch (SQLException ex) {
                        throw new UncategorizedSQLException("", "", ex);
                    }
                }
            });
            try {
                assertThat(((ConnectionProxy) dsProxy.getConnection()).getTargetConnection()).isEqualTo(con);
                // should be ignored
                dsProxy.getConnection().close();
            } catch (SQLException ex) {
                throw new UncategorizedSQLException("", "", ex);
            }
        }
    });
    boolean condition = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition).as("Hasn't thread connection").isTrue();
    InOrder ordered = inOrder(con);
    ordered.verify(con).setAutoCommit(false);
    ordered.verify(con).commit();
    ordered.verify(con).setAutoCommit(true);
    verify(con, times(2)).close();
}
Also used : UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) InOrder(org.mockito.InOrder) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) SQLException(java.sql.SQLException) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with UncategorizedSQLException

use of org.springframework.jdbc.UncategorizedSQLException in project spring-framework by spring-projects.

the class JdbcTransactionManagerTests method testTransactionAwareDataSourceProxy.

@Test
public void testTransactionAwareDataSourceProxy() throws Exception {
    given(con.getAutoCommit()).willReturn(true);
    given(con.getWarnings()).willThrow(new SQLException());
    TransactionTemplate tt = new TransactionTemplate(tm);
    boolean condition1 = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition1).as("Hasn't thread connection").isTrue();
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            // something transactional
            assertThat(DataSourceUtils.getConnection(ds)).isEqualTo(con);
            TransactionAwareDataSourceProxy dsProxy = new TransactionAwareDataSourceProxy(ds);
            try {
                Connection tCon = dsProxy.getConnection();
                tCon.getWarnings();
                tCon.clearWarnings();
                assertThat(((ConnectionProxy) dsProxy.getConnection()).getTargetConnection()).isEqualTo(con);
                // should be ignored
                dsProxy.getConnection().close();
            } catch (SQLException ex) {
                throw new UncategorizedSQLException("", "", ex);
            }
        }
    });
    boolean condition = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition).as("Hasn't thread connection").isTrue();
    InOrder ordered = inOrder(con);
    ordered.verify(con).setAutoCommit(false);
    ordered.verify(con).commit();
    ordered.verify(con).setAutoCommit(true);
    verify(con).close();
}
Also used : UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) InOrder(org.mockito.InOrder) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) SQLException(java.sql.SQLException) TransactionAwareDataSourceProxy(org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) Connection(java.sql.Connection) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with UncategorizedSQLException

use of org.springframework.jdbc.UncategorizedSQLException in project spring-framework by spring-projects.

the class JdbcTransactionManagerTests method testTransactionAwareDataSourceProxyWithSuspensionAndReobtaining.

@Test
public void testTransactionAwareDataSourceProxyWithSuspensionAndReobtaining() throws Exception {
    given(con.getAutoCommit()).willReturn(true);
    final TransactionTemplate tt = new TransactionTemplate(tm);
    tt.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRES_NEW);
    boolean condition1 = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition1).as("Hasn't thread connection").isTrue();
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            // something transactional
            assertThat(DataSourceUtils.getConnection(ds)).isEqualTo(con);
            final TransactionAwareDataSourceProxy dsProxy = new TransactionAwareDataSourceProxy(ds);
            dsProxy.setReobtainTransactionalConnections(true);
            try {
                assertThat(((ConnectionProxy) dsProxy.getConnection()).getTargetConnection()).isEqualTo(con);
                // should be ignored
                dsProxy.getConnection().close();
            } catch (SQLException ex) {
                throw new UncategorizedSQLException("", "", ex);
            }
            tt.execute(new TransactionCallbackWithoutResult() {

                @Override
                protected void doInTransactionWithoutResult(TransactionStatus status) {
                    // something transactional
                    assertThat(DataSourceUtils.getConnection(ds)).isEqualTo(con);
                    try {
                        assertThat(((ConnectionProxy) dsProxy.getConnection()).getTargetConnection()).isEqualTo(con);
                        // should be ignored
                        dsProxy.getConnection().close();
                    } catch (SQLException ex) {
                        throw new UncategorizedSQLException("", "", ex);
                    }
                }
            });
            try {
                assertThat(((ConnectionProxy) dsProxy.getConnection()).getTargetConnection()).isEqualTo(con);
                // should be ignored
                dsProxy.getConnection().close();
            } catch (SQLException ex) {
                throw new UncategorizedSQLException("", "", ex);
            }
        }
    });
    boolean condition = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition).as("Hasn't thread connection").isTrue();
    InOrder ordered = inOrder(con);
    ordered.verify(con).setAutoCommit(false);
    ordered.verify(con).commit();
    ordered.verify(con).setAutoCommit(true);
    verify(con, times(2)).close();
}
Also used : UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) InOrder(org.mockito.InOrder) TransactionAwareDataSourceProxy(org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) SQLException(java.sql.SQLException) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) ConnectionProxy(org.springframework.jdbc.datasource.ConnectionProxy) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with UncategorizedSQLException

use of org.springframework.jdbc.UncategorizedSQLException in project spring-framework by spring-projects.

the class JdbcTransactionManagerTests method doTestTransactionRollbackRestoringAutoCommit.

private void doTestTransactionRollbackRestoringAutoCommit(boolean autoCommit, boolean lazyConnection, final boolean createStatement) throws Exception {
    if (lazyConnection) {
        given(con.getAutoCommit()).willReturn(autoCommit);
        given(con.getTransactionIsolation()).willReturn(Connection.TRANSACTION_READ_COMMITTED);
    }
    if (!lazyConnection || createStatement) {
        given(con.getAutoCommit()).willReturn(autoCommit);
    }
    final DataSource dsToUse = (lazyConnection ? new LazyConnectionDataSourceProxy(ds) : ds);
    tm = new JdbcTransactionManager(dsToUse);
    TransactionTemplate tt = new TransactionTemplate(tm);
    boolean condition3 = !TransactionSynchronizationManager.hasResource(dsToUse);
    assertThat(condition3).as("Hasn't thread connection").isTrue();
    boolean condition2 = !TransactionSynchronizationManager.isSynchronizationActive();
    assertThat(condition2).as("Synchronization not active").isTrue();
    final RuntimeException ex = new RuntimeException("Application exception");
    assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
            assertThat(TransactionSynchronizationManager.hasResource(dsToUse)).as("Has thread connection").isTrue();
            assertThat(TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization active").isTrue();
            assertThat(status.isNewTransaction()).as("Is new transaction").isTrue();
            Connection con = DataSourceUtils.getConnection(dsToUse);
            if (createStatement) {
                try {
                    con.createStatement();
                } catch (SQLException ex) {
                    throw new UncategorizedSQLException("", "", ex);
                }
            }
            throw ex;
        }
    })).isEqualTo(ex);
    boolean condition1 = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition1).as("Hasn't thread connection").isTrue();
    boolean condition = !TransactionSynchronizationManager.isSynchronizationActive();
    assertThat(condition).as("Synchronization not active").isTrue();
    if (autoCommit && (!lazyConnection || createStatement)) {
        InOrder ordered = inOrder(con);
        ordered.verify(con).setAutoCommit(false);
        ordered.verify(con).rollback();
        ordered.verify(con).setAutoCommit(true);
    }
    if (createStatement) {
        verify(con, times(2)).close();
    } else {
        verify(con).close();
    }
}
Also used : LazyConnectionDataSourceProxy(org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy) InOrder(org.mockito.InOrder) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) SQLException(java.sql.SQLException) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) Connection(java.sql.Connection) TransactionStatus(org.springframework.transaction.TransactionStatus) DataSource(javax.sql.DataSource) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult)

Aggregations

UncategorizedSQLException (org.springframework.jdbc.UncategorizedSQLException)12 SQLException (java.sql.SQLException)11 InOrder (org.mockito.InOrder)10 TransactionStatus (org.springframework.transaction.TransactionStatus)10 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)10 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)10 Test (org.junit.jupiter.api.Test)7 Connection (java.sql.Connection)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 DataSource (javax.sql.DataSource)4 TransactionAwareDataSourceProxy (org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy)3 ConnectionProxy (org.springframework.jdbc.datasource.ConnectionProxy)2 LazyConnectionDataSourceProxy (org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy)2 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 BatchUpdateException (java.sql.BatchUpdateException)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1 DataAccessException (org.springframework.dao.DataAccessException)1 BadSqlGrammarException (org.springframework.jdbc.BadSqlGrammarException)1