use of org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator in project tutorials by eugenp.
the class ExceptionTranslator method exception.
@Override
public void exception(ExecuteContext context) {
SQLDialect dialect = context.configuration().dialect();
SQLExceptionTranslator translator = new SQLErrorCodeSQLExceptionTranslator(dialect.name());
context.exception(translator.translate("Access database using jOOQ", context.sql(), context.sqlException()));
}
use of org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator 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.SQLErrorCodeSQLExceptionTranslator in project spring-framework by spring-projects.
the class JdbcTemplateTests method doTestCouldntGetConnectionInOperationWithExceptionTranslatorInitialized.
/**
* If beanProperty is true, initialize via exception translator bean property;
* if false, use afterPropertiesSet().
*/
private void doTestCouldntGetConnectionInOperationWithExceptionTranslatorInitialized(boolean beanProperty) throws SQLException {
SQLException sqlException = new SQLException("foo", "07xxx");
this.dataSource = mock(DataSource.class);
given(this.dataSource.getConnection()).willThrow(sqlException);
this.template = new JdbcTemplate();
this.template.setDataSource(this.dataSource);
this.template.setLazyInit(false);
if (beanProperty) {
// This will get a connection.
this.template.setExceptionTranslator(new SQLErrorCodeSQLExceptionTranslator(this.dataSource));
} else {
// This will cause creation of default SQL translator.
this.template.afterPropertiesSet();
}
RowCountCallbackHandler rcch = new RowCountCallbackHandler();
this.thrown.expect(CannotGetJdbcConnectionException.class);
this.thrown.expect(exceptionCause(sameInstance(sqlException)));
this.template.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch);
}
use of org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator in project spring-framework by spring-projects.
the class JdbcTemplateTests method doTestCouldNotGetConnectionInOperationWithExceptionTranslatorInitialized.
/**
* If beanProperty is true, initialize via exception translator bean property;
* if false, use afterPropertiesSet().
*/
private void doTestCouldNotGetConnectionInOperationWithExceptionTranslatorInitialized(boolean beanProperty) throws SQLException {
SQLException sqlException = new SQLException("foo", "07xxx");
this.dataSource = mock(DataSource.class);
given(this.dataSource.getConnection()).willThrow(sqlException);
this.template = new JdbcTemplate();
this.template.setDataSource(this.dataSource);
this.template.setLazyInit(false);
if (beanProperty) {
// This will get a connection.
this.template.setExceptionTranslator(new SQLErrorCodeSQLExceptionTranslator(this.dataSource));
} else {
// This will cause creation of default SQL translator.
this.template.afterPropertiesSet();
}
RowCountCallbackHandler rcch = new RowCountCallbackHandler();
assertThatExceptionOfType(CannotGetJdbcConnectionException.class).isThrownBy(() -> this.template.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch)).withCause(sqlException);
}
Aggregations