Search in sources :

Example 6 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);
    TransactionTemplate tt = new TransactionTemplate(tm);
    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));
            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);
            }
        }
    });
    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).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)

Example 7 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(), equalTo("B; D"));
    }
}
Also used : UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) BatchUpdateException(java.sql.BatchUpdateException) Test(org.junit.Test)

Example 8 with UncategorizedSQLException

use of org.springframework.jdbc.UncategorizedSQLException in project uPortal by Jasig.

the class DataSourceSchemaExport method perform.

private void perform(String[] sqlCommands, boolean executeSql, String outputFile, boolean append, boolean failFast) {
    final PrintWriter sqlWriter = getSqlWriter(outputFile, append);
    try {
        for (final String sqlCommand : sqlCommands) {
            final String formatted = formatter.format(sqlCommand);
            sqlWriter.println(formatted);
            if (executeSql) {
                try {
                    jdbcOperations.execute(sqlCommand);
                    logger.info(sqlCommand);
                } catch (BadSqlGrammarException | UncategorizedSQLException e) {
                    // Needed until Hibernate 5.  See https://hibernate.atlassian.net/browse/HHH-7002.
                    if (sqlCommand.contains("drop constraint")) {
                        logger.info("Failed to execute (probably ignorable): {}, error message {}", sqlCommand, e.getMessage());
                    } else {
                        handleSqlException(failFast, sqlCommand, e);
                    }
                } catch (Exception e) {
                    handleSqlException(failFast, sqlCommand, e);
                }
            }
        }
    } finally {
        IOUtils.closeQuietly(sqlWriter);
    }
}
Also used : BadSqlGrammarException(org.springframework.jdbc.BadSqlGrammarException) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) DataAccessException(org.springframework.dao.DataAccessException) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) SQLException(java.sql.SQLException) IOException(java.io.IOException) BadSqlGrammarException(org.springframework.jdbc.BadSqlGrammarException) PrintWriter(java.io.PrintWriter)

Aggregations

UncategorizedSQLException (org.springframework.jdbc.UncategorizedSQLException)8 SQLException (java.sql.SQLException)6 InOrder (org.mockito.InOrder)5 TransactionStatus (org.springframework.transaction.TransactionStatus)5 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)5 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)5 Test (org.junit.Test)4 Connection (java.sql.Connection)2 DataSource (javax.sql.DataSource)2 DataAccessException (org.springframework.dao.DataAccessException)2 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 BatchUpdateException (java.sql.BatchUpdateException)1 BadSqlGrammarException (org.springframework.jdbc.BadSqlGrammarException)1