Search in sources :

Example 1 with SQLErrorCodeSQLExceptionTranslator

use of cn.taketoday.jdbc.support.SQLErrorCodeSQLExceptionTranslator in project today-framework by TAKETODAY.

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(cn.taketoday.jdbc.support.SQLErrorCodeSQLExceptionTranslator) SQLException(java.sql.SQLException) UncategorizedSQLException(cn.taketoday.jdbc.UncategorizedSQLException) DataSource(javax.sql.DataSource) SingleConnectionDataSource(cn.taketoday.jdbc.datasource.SingleConnectionDataSource)

Example 2 with SQLErrorCodeSQLExceptionTranslator

use of cn.taketoday.jdbc.support.SQLErrorCodeSQLExceptionTranslator in project today-infrastructure by TAKETODAY.

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(cn.taketoday.jdbc.support.SQLErrorCodeSQLExceptionTranslator) SQLException(java.sql.SQLException) UncategorizedSQLException(cn.taketoday.jdbc.UncategorizedSQLException) DataSource(javax.sql.DataSource) SingleConnectionDataSource(cn.taketoday.jdbc.datasource.SingleConnectionDataSource)

Aggregations

UncategorizedSQLException (cn.taketoday.jdbc.UncategorizedSQLException)2 SingleConnectionDataSource (cn.taketoday.jdbc.datasource.SingleConnectionDataSource)2 SQLErrorCodeSQLExceptionTranslator (cn.taketoday.jdbc.support.SQLErrorCodeSQLExceptionTranslator)2 SQLException (java.sql.SQLException)2 DataSource (javax.sql.DataSource)2