Search in sources :

Example 51 with UncategorizedSQLException

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

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

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

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(org.springframework.jdbc.UncategorizedSQLException) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) BatchUpdateException(java.sql.BatchUpdateException) Test(org.junit.jupiter.api.Test)

Aggregations

UncategorizedSQLException (org.springframework.jdbc.UncategorizedSQLException)52 SQLException (java.sql.SQLException)39 InOrder (org.mockito.InOrder)30 TransactionStatus (org.springframework.transaction.TransactionStatus)30 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)30 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)30 Test (org.junit.jupiter.api.Test)19 Connection (java.sql.Connection)18 DataSource (javax.sql.DataSource)12 DataAccessException (org.springframework.dao.DataAccessException)10 Test (org.junit.Test)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 BatchUpdateException (java.sql.BatchUpdateException)5 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 NonNull (org.springframework.lang.NonNull)4 Fields (com.mqttsnet.thinglinks.tdengine.api.domain.Fields)3 TransactionAwareDataSourceProxy (org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy)3 FieldsVo (com.mqttsnet.thinglinks.tdengine.api.domain.FieldsVo)2 EmptyResultDataAccessException (org.springframework.dao.EmptyResultDataAccessException)2 ObservationState (ca.nrc.cadc.caom2.ObservationState)1