Search in sources :

Example 1 with UncategorizedSQLException

use of cn.taketoday.jdbc.UncategorizedSQLException in project today-infrastructure by TAKETODAY.

the class JdbcTransactionManagerTests method doTestTransactionCommitRestoringAutoCommit.

private void doTestTransactionCommitRestoringAutoCommit(boolean autoCommit, boolean lazyConnection, final boolean createStatement) throws Exception {
    if (lazyConnection) {
        given(con.getAutoCommit()).willReturn(autoCommit);
        given(con.getTransactionIsolation()).willReturn(Connection.TRANSACTION_READ_COMMITTED);
        given(con.getWarnings()).willThrow(new SQLException());
    }
    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();
    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();
            assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).isFalse();
            assertThat(TransactionSynchronizationManager.isActualTransactionActive()).isTrue();
            Connection tCon = DataSourceUtils.getConnection(dsToUse);
            try {
                if (createStatement) {
                    tCon.createStatement();
                } else {
                    tCon.getWarnings();
                    tCon.clearWarnings();
                }
            } catch (SQLException ex) {
                throw new UncategorizedSQLException("", "", ex);
            }
        }
    });
    boolean condition1 = !TransactionSynchronizationManager.hasResource(dsToUse);
    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).commit();
        ordered.verify(con).setAutoCommit(true);
    }
    if (createStatement) {
        verify(con, times(2)).close();
    } else {
        verify(con).close();
    }
}
Also used : LazyConnectionDataSourceProxy(cn.taketoday.jdbc.datasource.LazyConnectionDataSourceProxy) InOrder(org.mockito.InOrder) SQLException(java.sql.SQLException) UncategorizedSQLException(cn.taketoday.jdbc.UncategorizedSQLException) TransactionTemplate(cn.taketoday.transaction.support.TransactionTemplate) Connection(java.sql.Connection) TransactionStatus(cn.taketoday.transaction.TransactionStatus) DataSource(javax.sql.DataSource) UncategorizedSQLException(cn.taketoday.jdbc.UncategorizedSQLException) TransactionCallbackWithoutResult(cn.taketoday.transaction.support.TransactionCallbackWithoutResult)

Example 2 with UncategorizedSQLException

use of cn.taketoday.jdbc.UncategorizedSQLException in project today-infrastructure by TAKETODAY.

the class JdbcTransactionManagerTests method testTransactionAwareDataSourceProxyWithSuspension.

@Test
public void testTransactionAwareDataSourceProxyWithSuspension() 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);
            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(cn.taketoday.jdbc.UncategorizedSQLException) InOrder(org.mockito.InOrder) TransactionAwareDataSourceProxy(cn.taketoday.jdbc.datasource.TransactionAwareDataSourceProxy) SQLException(java.sql.SQLException) UncategorizedSQLException(cn.taketoday.jdbc.UncategorizedSQLException) TransactionTemplate(cn.taketoday.transaction.support.TransactionTemplate) TransactionStatus(cn.taketoday.transaction.TransactionStatus) ConnectionProxy(cn.taketoday.jdbc.datasource.ConnectionProxy) TransactionCallbackWithoutResult(cn.taketoday.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with UncategorizedSQLException

use of cn.taketoday.jdbc.UncategorizedSQLException in project today-infrastructure by TAKETODAY.

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(cn.taketoday.jdbc.UncategorizedSQLException) InOrder(org.mockito.InOrder) TransactionAwareDataSourceProxy(cn.taketoday.jdbc.datasource.TransactionAwareDataSourceProxy) SQLException(java.sql.SQLException) UncategorizedSQLException(cn.taketoday.jdbc.UncategorizedSQLException) TransactionTemplate(cn.taketoday.transaction.support.TransactionTemplate) TransactionStatus(cn.taketoday.transaction.TransactionStatus) ConnectionProxy(cn.taketoday.jdbc.datasource.ConnectionProxy) TransactionCallbackWithoutResult(cn.taketoday.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with UncategorizedSQLException

use of cn.taketoday.jdbc.UncategorizedSQLException in project today-infrastructure by TAKETODAY.

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(cn.taketoday.jdbc.UncategorizedSQLException) InOrder(org.mockito.InOrder) SQLException(java.sql.SQLException) UncategorizedSQLException(cn.taketoday.jdbc.UncategorizedSQLException) TransactionAwareDataSourceProxy(cn.taketoday.jdbc.datasource.TransactionAwareDataSourceProxy) TransactionTemplate(cn.taketoday.transaction.support.TransactionTemplate) Connection(java.sql.Connection) 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 UncategorizedSQLException

use of cn.taketoday.jdbc.UncategorizedSQLException in project today-infrastructure by TAKETODAY.

the class JdbcTemplateTests method testBatchUpdateWithBatchFailure.

@Test
public void testBatchUpdateWithBatchFailure() throws Exception {
    final String[] sql = { "A", "B", "C", "D" };
    given(this.statement.executeBatch()).willThrow(new BatchUpdateException(new int[] { 1, Statement.EXECUTE_FAILED, 1, Statement.EXECUTE_FAILED }));
    mockDatabaseMetaData(true);
    given(this.connection.createStatement()).willReturn(this.statement);
    JdbcTemplate template = new JdbcTemplate(this.dataSource, false);
    try {
        template.batchUpdate(sql);
    } catch (UncategorizedSQLException ex) {
        assertThat(ex.getSql()).isEqualTo("B; D");
    }
}
Also used : UncategorizedSQLException(cn.taketoday.jdbc.UncategorizedSQLException) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) BatchUpdateException(java.sql.BatchUpdateException) Test(org.junit.jupiter.api.Test)

Aggregations

UncategorizedSQLException (cn.taketoday.jdbc.UncategorizedSQLException)22 TransactionStatus (cn.taketoday.transaction.TransactionStatus)20 TransactionCallbackWithoutResult (cn.taketoday.transaction.support.TransactionCallbackWithoutResult)20 TransactionTemplate (cn.taketoday.transaction.support.TransactionTemplate)20 SQLException (java.sql.SQLException)20 InOrder (org.mockito.InOrder)20 Test (org.junit.jupiter.api.Test)14 Connection (java.sql.Connection)12 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)12 DataSource (javax.sql.DataSource)8 TransactionAwareDataSourceProxy (cn.taketoday.jdbc.datasource.TransactionAwareDataSourceProxy)6 ConnectionProxy (cn.taketoday.jdbc.datasource.ConnectionProxy)4 LazyConnectionDataSourceProxy (cn.taketoday.jdbc.datasource.LazyConnectionDataSourceProxy)4 BatchUpdateException (java.sql.BatchUpdateException)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2