Search in sources :

Example 16 with UncategorizedSQLException

use of org.springframework.jdbc.UncategorizedSQLException in project spring by mybatis.

the class MyBatisExceptionTranslator method translateExceptionIfPossible.

/**
 * {@inheritDoc}
 */
@Override
public DataAccessException translateExceptionIfPossible(RuntimeException e) {
    if (e instanceof PersistenceException) {
        // recursion has a risk of infinite loop so better make another if
        if (e.getCause() instanceof PersistenceException) {
            e = (PersistenceException) e.getCause();
        }
        if (e.getCause() instanceof SQLException) {
            this.initExceptionTranslator();
            String task = e.getMessage() + "\n";
            SQLException se = (SQLException) e.getCause();
            DataAccessException dae = this.exceptionTranslator.translate(task, null, se);
            return dae != null ? dae : new UncategorizedSQLException(task, null, se);
        } else if (e.getCause() instanceof TransactionException) {
            throw (TransactionException) e.getCause();
        }
        return new MyBatisSystemException(e);
    }
    return null;
}
Also used : UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) TransactionException(org.springframework.transaction.TransactionException) SQLException(java.sql.SQLException) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) PersistenceException(org.apache.ibatis.exceptions.PersistenceException) DataAccessException(org.springframework.dao.DataAccessException)

Example 17 with UncategorizedSQLException

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

the class HibernateFailureJobFunctionalTests method testLaunchJob.

@Test
public void testLaunchJob() throws Exception {
    validatePreConditions();
    JobParameters params = new JobParametersBuilder().addString("key", "failureJob").toJobParameters();
    writer.setFailOnFlush(2);
    try {
        jobLauncherTestUtils.launchJob(params);
    } catch (HibernateJdbcException e) {
        // RepeatContext:
        throw e;
    } catch (UncategorizedSQLException e) {
        // assertEquals(1, writer.getErrors().size());
        throw e;
    }
    int after = JdbcTestUtils.countRowsInTable(jdbcTemplate, "CUSTOMER");
    assertEquals(4, after);
    validatePostConditions();
}
Also used : UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) JobParametersBuilder(org.springframework.batch.core.JobParametersBuilder) HibernateJdbcException(org.springframework.orm.hibernate5.HibernateJdbcException) JobParameters(org.springframework.batch.core.JobParameters) Test(org.junit.Test)

Example 18 with UncategorizedSQLException

use of org.springframework.jdbc.UncategorizedSQLException in project springframework-source-5.1.x by wb02125055.

the class AbstractFallbackSQLExceptionTranslator method translate.

/**
 * Pre-checks the arguments, calls {@link #doTranslate}, and invokes the
 * {@link #getFallbackTranslator() fallback translator} if necessary.
 */
@Override
@NonNull
public DataAccessException translate(String task, @Nullable String sql, SQLException ex) {
    Assert.notNull(ex, "Cannot translate a null SQLException");
    DataAccessException dae = doTranslate(task, sql, ex);
    if (dae != null) {
        // Specific exception match found.
        return dae;
    }
    // Looking for a fallback...
    SQLExceptionTranslator fallback = getFallbackTranslator();
    if (fallback != null) {
        dae = fallback.translate(task, sql, ex);
        if (dae != null) {
            // Fallback exception match found.
            return dae;
        }
    }
    // We couldn't identify it more precisely.
    return new UncategorizedSQLException(task, sql, ex);
}
Also used : UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) DataAccessException(org.springframework.dao.DataAccessException) NonNull(org.springframework.lang.NonNull)

Example 19 with UncategorizedSQLException

use of org.springframework.jdbc.UncategorizedSQLException in project springframework-source-5.1.x by wb02125055.

the class DataSourceTransactionManagerTests 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 DataSourceTransactionManager(dsToUse);
    TransactionTemplate tt = new TransactionTemplate(tm);
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(dsToUse));
    assertTrue("Synchronization not active", !TransactionSynchronizationManager.isSynchronizationActive());
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
            assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dsToUse));
            assertTrue("Synchronization active", TransactionSynchronizationManager.isSynchronizationActive());
            assertTrue("Is new transaction", status.isNewTransaction());
            assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
            assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
            Connection tCon = DataSourceUtils.getConnection(dsToUse);
            try {
                if (createStatement) {
                    tCon.createStatement();
                } else {
                    tCon.getWarnings();
                    tCon.clearWarnings();
                }
            } catch (SQLException ex) {
                throw new UncategorizedSQLException("", "", ex);
            }
        }
    });
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(dsToUse));
    assertTrue("Synchronization not active", !TransactionSynchronizationManager.isSynchronizationActive());
    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 : 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) DataSource(javax.sql.DataSource)

Example 20 with UncategorizedSQLException

use of org.springframework.jdbc.UncategorizedSQLException in project springframework-source-5.1.x by wb02125055.

the class DataSourceTransactionManagerTests 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);
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            // something transactional
            assertEquals(con, DataSourceUtils.getConnection(ds));
            final TransactionAwareDataSourceProxy dsProxy = new TransactionAwareDataSourceProxy(ds);
            try {
                assertEquals(con, ((ConnectionProxy) dsProxy.getConnection()).getTargetConnection());
                // 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
                    assertEquals(con, DataSourceUtils.getConnection(ds));
                    try {
                        assertEquals(con, ((ConnectionProxy) dsProxy.getConnection()).getTargetConnection());
                        // should be ignored
                        dsProxy.getConnection().close();
                    } catch (SQLException ex) {
                        throw new UncategorizedSQLException("", "", ex);
                    }
                }
            });
            try {
                assertEquals(con, ((ConnectionProxy) dsProxy.getConnection()).getTargetConnection());
                // should be ignored
                dsProxy.getConnection().close();
            } catch (SQLException ex) {
                throw new UncategorizedSQLException("", "", ex);
            }
        }
    });
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
    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.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