Search in sources :

Example 1 with SQLStateSQLExceptionTranslator

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

the class JdbcTemplateTests method testUseCustomSQLErrorCodeTranslator.

/**
	 * Test that we see an SQLException translated using Error Code.
	 * If we provide the SQLExceptionTranslator, we shouldn't use a connection
	 * to get the metadata
	 */
@Test
public void testUseCustomSQLErrorCodeTranslator() throws Exception {
    // Bad SQL state
    final SQLException sqlException = new SQLException("I have a known problem", "07000", 1054);
    final String sql = "SELECT ID FROM CUSTOMER";
    given(this.resultSet.next()).willReturn(true);
    given(this.connection.createStatement()).willReturn(this.preparedStatement);
    JdbcTemplate template = new JdbcTemplate();
    template.setDataSource(this.dataSource);
    // Set custom exception translator
    template.setExceptionTranslator(new SQLStateSQLExceptionTranslator());
    template.afterPropertiesSet();
    this.thrown.expect(BadSqlGrammarException.class);
    this.thrown.expect(exceptionCause(sameInstance(sqlException)));
    try {
        template.query(sql, new RowCallbackHandler() {

            @Override
            public void processRow(ResultSet rs) throws SQLException {
                throw sqlException;
            }
        });
    } finally {
        verify(this.resultSet).close();
        verify(this.preparedStatement).close();
        verify(this.connection).close();
    }
}
Also used : UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) SQLStateSQLExceptionTranslator(org.springframework.jdbc.support.SQLStateSQLExceptionTranslator) Test(org.junit.Test)

Example 2 with SQLStateSQLExceptionTranslator

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

the class StoredProcedureTests method testStoredProcedureConfiguredViaJdbcTemplateWithCustomExceptionTranslator.

/**
	 * Confirm no connection was used to get metadata. Does not use superclass replay
	 * mechanism.
	 *
	 * @throws Exception
	 */
@Test
public void testStoredProcedureConfiguredViaJdbcTemplateWithCustomExceptionTranslator() throws Exception {
    given(callableStatement.execute()).willReturn(false);
    given(callableStatement.getUpdateCount()).willReturn(-1);
    given(callableStatement.getObject(2)).willReturn(5);
    given(connection.prepareCall("{call " + StoredProcedureConfiguredViaJdbcTemplate.SQL + "(?, ?)}")).willReturn(callableStatement);
    class TestJdbcTemplate extends JdbcTemplate {

        int calls;

        @Override
        public Map<String, Object> call(CallableStatementCreator csc, List<SqlParameter> declaredParameters) throws DataAccessException {
            calls++;
            return super.call(csc, declaredParameters);
        }
    }
    TestJdbcTemplate t = new TestJdbcTemplate();
    t.setDataSource(dataSource);
    // Will fail without the following, because we're not able to get a connection
    // from the DataSource here if we need to create an ExceptionTranslator
    t.setExceptionTranslator(new SQLStateSQLExceptionTranslator());
    StoredProcedureConfiguredViaJdbcTemplate sp = new StoredProcedureConfiguredViaJdbcTemplate(t);
    assertEquals(sp.execute(11), 5);
    assertEquals(1, t.calls);
    verify(callableStatement).setObject(1, 11, Types.INTEGER);
    verify(callableStatement).registerOutParameter(2, Types.INTEGER);
}
Also used : CallableStatementCreator(org.springframework.jdbc.core.CallableStatementCreator) List(java.util.List) SQLStateSQLExceptionTranslator(org.springframework.jdbc.support.SQLStateSQLExceptionTranslator) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) Test(org.junit.Test)

Example 3 with SQLStateSQLExceptionTranslator

use of org.springframework.jdbc.support.SQLStateSQLExceptionTranslator in project jOOQ by jOOQ.

the class ExceptionTranslator method exception.

@Override
public void exception(ExecuteContext ctx) {
    // [#4391] Translate only SQLExceptions
    if (ctx.sqlException() != null) {
        SQLDialect dialect = ctx.dialect();
        SQLExceptionTranslator translator = (dialect != null) ? new SQLErrorCodeSQLExceptionTranslator(dialect.thirdParty().springDbName()) : new SQLStateSQLExceptionTranslator();
        ctx.exception(translator.translate("jOOQ", ctx.sql(), ctx.sqlException()));
    }
}
Also used : SQLErrorCodeSQLExceptionTranslator(org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator) SQLExceptionTranslator(org.springframework.jdbc.support.SQLExceptionTranslator) SQLStateSQLExceptionTranslator(org.springframework.jdbc.support.SQLStateSQLExceptionTranslator) SQLErrorCodeSQLExceptionTranslator(org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator) SQLDialect(org.jooq.SQLDialect) SQLStateSQLExceptionTranslator(org.springframework.jdbc.support.SQLStateSQLExceptionTranslator)

Example 4 with SQLStateSQLExceptionTranslator

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

the class RowMapperTests method setUp.

@Before
public void setUp() throws SQLException {
    given(connection.createStatement()).willReturn(statement);
    given(connection.prepareStatement(anyString())).willReturn(preparedStatement);
    given(statement.executeQuery(anyString())).willReturn(resultSet);
    given(preparedStatement.executeQuery()).willReturn(resultSet);
    given(resultSet.next()).willReturn(true, true, false);
    given(resultSet.getString(1)).willReturn("tb1", "tb2");
    given(resultSet.getInt(2)).willReturn(1, 2);
    template.setDataSource(new SingleConnectionDataSource(connection, false));
    template.setExceptionTranslator(new SQLStateSQLExceptionTranslator());
    template.afterPropertiesSet();
}
Also used : SingleConnectionDataSource(org.springframework.jdbc.datasource.SingleConnectionDataSource) SQLStateSQLExceptionTranslator(org.springframework.jdbc.support.SQLStateSQLExceptionTranslator) Before(org.junit.Before)

Aggregations

SQLStateSQLExceptionTranslator (org.springframework.jdbc.support.SQLStateSQLExceptionTranslator)4 Test (org.junit.Test)2 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 List (java.util.List)1 SQLDialect (org.jooq.SQLDialect)1 Before (org.junit.Before)1 UncategorizedSQLException (org.springframework.jdbc.UncategorizedSQLException)1 CallableStatementCreator (org.springframework.jdbc.core.CallableStatementCreator)1 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)1 SingleConnectionDataSource (org.springframework.jdbc.datasource.SingleConnectionDataSource)1 SQLErrorCodeSQLExceptionTranslator (org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator)1 SQLExceptionTranslator (org.springframework.jdbc.support.SQLExceptionTranslator)1