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();
}
}
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);
}
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()));
}
}
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();
}
Aggregations