Search in sources :

Example 1 with SQLErrorCodeSQLExceptionTranslator

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()));
}
Also used : SQLErrorCodeSQLExceptionTranslator(org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator) SQLExceptionTranslator(org.springframework.jdbc.support.SQLExceptionTranslator) SQLErrorCodeSQLExceptionTranslator(org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator) SQLDialect(org.jooq.SQLDialect)

Example 2 with SQLErrorCodeSQLExceptionTranslator

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()));
    }
}
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 3 with SQLErrorCodeSQLExceptionTranslator

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);
}
Also used : SQLErrorCodeSQLExceptionTranslator(org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) SQLException(java.sql.SQLException) SingleConnectionDataSource(org.springframework.jdbc.datasource.SingleConnectionDataSource) DataSource(javax.sql.DataSource)

Example 4 with SQLErrorCodeSQLExceptionTranslator

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);
}
Also used : SQLErrorCodeSQLExceptionTranslator(org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) SQLException(java.sql.SQLException) SingleConnectionDataSource(org.springframework.jdbc.datasource.SingleConnectionDataSource) DataSource(javax.sql.DataSource)

Aggregations

SQLErrorCodeSQLExceptionTranslator (org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator)4 SQLException (java.sql.SQLException)2 DataSource (javax.sql.DataSource)2 SQLDialect (org.jooq.SQLDialect)2 UncategorizedSQLException (org.springframework.jdbc.UncategorizedSQLException)2 SingleConnectionDataSource (org.springframework.jdbc.datasource.SingleConnectionDataSource)2 SQLExceptionTranslator (org.springframework.jdbc.support.SQLExceptionTranslator)2 SQLStateSQLExceptionTranslator (org.springframework.jdbc.support.SQLStateSQLExceptionTranslator)1