/** * Specify the database product name for the DataSource that this accessor uses. * This allows to initialize a SQLErrorCodeSQLExceptionTranslator without * obtaining a Connection from the DataSource to get the meta-data. * @param dbName the database product name that identifies the error codes entry * @see SQLErrorCodeSQLExceptionTranslator#setDatabaseProductName * @see java.sql.DatabaseMetaData#getDatabaseProductName() */ public void setDatabaseProductName(String dbName) { this.exceptionTranslator = new SQLErrorCodeSQLExceptionTranslator(dbName); }
DataAccessException dae = customTranslate(task, sql, sqlEx); if (dae != null) { return dae; if (Arrays.binarySearch(customTranslation.getErrorCodes(), errorCode) >= 0 && customTranslation.getExceptionClass() != null) { DataAccessException customException = createCustomException( task, sql, sqlEx, customTranslation.getExceptionClass()); if (customException != null) { logTranslation(task, sql, sqlEx, true); return customException; logTranslation(task, sql, sqlEx, false); return new BadSqlGrammarException(task, (sql != null ? sql : ""), sqlEx); logTranslation(task, sql, sqlEx, false); return new InvalidResultSetAccessException(task, (sql != null ? sql : ""), sqlEx); logTranslation(task, sql, sqlEx, false); return new DuplicateKeyException(buildMessage(task, sql, sqlEx), sqlEx); logTranslation(task, sql, sqlEx, false); return new DataIntegrityViolationException(buildMessage(task, sql, sqlEx), sqlEx); logTranslation(task, sql, sqlEx, false); return new PermissionDeniedDataAccessException(buildMessage(task, sql, sqlEx), sqlEx); logTranslation(task, sql, sqlEx, false); return new DataAccessResourceFailureException(buildMessage(task, sql, sqlEx), sqlEx);
/** * Create a SQL error code translator for the given DataSource. * Invoking this constructor will cause a Connection to be obtained * from the DataSource to get the meta-data. * @param dataSource the DataSource to use to find meta-data and establish * which error codes are usable * @see SQLErrorCodesFactory */ public SQLErrorCodeSQLExceptionTranslator(DataSource dataSource) { this(); setDataSource(dataSource); }
@Test @SuppressWarnings("resource") public void customErrorCodeTranslation() { new ClassPathXmlApplicationContext("test-custom-translators-context.xml", CustomSQLExceptionTranslatorRegistrarTests.class); SQLErrorCodes codes = SQLErrorCodesFactory.getInstance().getErrorCodes("H2"); SQLErrorCodeSQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(); sext.setSqlErrorCodes(codes); DataAccessException exFor4200 = sext.doTranslate("", "", new SQLException("Ouch", "42000", 42000)); assertNotNull("Should have been translated", exFor4200); assertTrue("Should have been instance of BadSqlGrammarException", BadSqlGrammarException.class.isAssignableFrom(exFor4200.getClass())); DataAccessException exFor2 = sext.doTranslate("", "", new SQLException("Ouch", "42000", 2)); assertNotNull("Should have been translated", exFor2); assertTrue("Should have been instance of TransientDataAccessResourceException", TransientDataAccessResourceException.class.isAssignableFrom(exFor2.getClass())); DataAccessException exFor3 = sext.doTranslate("", "", new SQLException("Ouch", "42000", 3)); assertNull("Should not have been translated", exFor3); }
@Test public void customExceptionTranslation() { final String TASK = "TASK"; final String SQL = "SQL SELECT *"; final SQLErrorCodes customErrorCodes = new SQLErrorCodes(); final CustomSQLErrorCodesTranslation customTranslation = new CustomSQLErrorCodesTranslation(); customErrorCodes.setBadSqlGrammarCodes(new String[] {"1", "2"}); customErrorCodes.setDataIntegrityViolationCodes(new String[] {"3", "4"}); customTranslation.setErrorCodes(new String[] {"1"}); customTranslation.setExceptionClass(CustomErrorCodeException.class); customErrorCodes.setCustomTranslations(new CustomSQLErrorCodesTranslation[] {customTranslation}); SQLErrorCodeSQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(); sext.setSqlErrorCodes(customErrorCodes); // Should custom translate this SQLException badSqlEx = new SQLException("", "", 1); assertEquals(CustomErrorCodeException.class, sext.translate(TASK, SQL, badSqlEx).getClass()); assertEquals(badSqlEx, sext.translate(TASK, SQL, badSqlEx).getCause()); // Shouldn't custom translate this SQLException invResEx = new SQLException("", "", 3); DataIntegrityViolationException diex = (DataIntegrityViolationException) sext.translate(TASK, SQL, invResEx); assertEquals(invResEx, diex.getCause()); // Shouldn't custom translate this - invalid class exception.expect(IllegalArgumentException.class); customTranslation.setExceptionClass(String.class); }
final SQLErrorCodeSQLExceptionTranslator translator = new SQLErrorCodeSQLExceptionTranslator(configuration.getDataSource()); throw translator.translate("Loading table-column type mapping.", null, sqle); } finally { try {
@Override protected DataAccessException customTranslate(String task, String sql, SQLException sqlEx) { logger.debug("Checking sql error code {} against {}", sqlEx.getErrorCode(), errorCodes); if (errorCodes.contains(sqlEx.getErrorCode())) { return new SuppressDataAccessException(task, sqlEx); } return super.customTranslate(task, sql, sqlEx); } }
protected Object executeWith(Connection connection, SqlMapClientCallback action) { SqlMapSession session = getSqlMapClient().openSession(); try { try { session.setUserConnection(connection); } catch (SQLException e) { throw new CannotGetJdbcConnectionException("Could not get JDBC Connection", e); } try { return action.doInSqlMapClient(session); } catch (SQLException ex) { throw new SQLErrorCodeSQLExceptionTranslator().translate("SqlMapClient operation", null, ex); } } finally { session.close(); } }
@Override protected DataAccessException customTranslate(String task, String sql, SQLException sqlEx) { logger.debug("Checking sql error code {} against {}", sqlEx.getErrorCode(), errorCodes); if (errorCodes.contains(sqlEx.getErrorCode())) { return new SuppressDataAccessException(task, sqlEx); } return super.customTranslate(task, sql, sqlEx); } }
/** * Return the exception translator for this instance. * <p>Creates a default {@link SQLErrorCodeSQLExceptionTranslator} * for the specified DataSource if none set, or a * {@link SQLStateSQLExceptionTranslator} in case of no DataSource. * @see #getDataSource() */ public SQLExceptionTranslator getExceptionTranslator() { SQLExceptionTranslator exceptionTranslator = this.exceptionTranslator; if (exceptionTranslator != null) { return exceptionTranslator; } synchronized (this) { exceptionTranslator = this.exceptionTranslator; if (exceptionTranslator == null) { DataSource dataSource = getDataSource(); if (dataSource != null) { exceptionTranslator = new SQLErrorCodeSQLExceptionTranslator(dataSource); } else { exceptionTranslator = new SQLStateSQLExceptionTranslator(); } this.exceptionTranslator = exceptionTranslator; } return exceptionTranslator; } }
return action.doInSqlMapClient(session); } catch (SQLException ex) { throw new SQLErrorCodeSQLExceptionTranslator().translate("SqlMapClient operation", null, ex); } catch (Throwable t) {
DataAccessException dex = customTranslate(task, sql, sqlEx); if (dex != null) { return dex; if (Arrays.binarySearch(customTranslation.getErrorCodes(), errorCode) >= 0) { if (customTranslation.getExceptionClass() != null) { DataAccessException customException = createCustomException( task, sql, sqlEx, customTranslation.getExceptionClass()); if (customException != null) { logTranslation(task, sql, sqlEx, true); return customException; logTranslation(task, sql, sqlEx, false); return new BadSqlGrammarException(task, sql, sqlEx); logTranslation(task, sql, sqlEx, false); return new InvalidResultSetAccessException(task, sql, sqlEx); logTranslation(task, sql, sqlEx, false); return new DuplicateKeyException(buildMessage(task, sql, sqlEx), sqlEx); logTranslation(task, sql, sqlEx, false); return new DataIntegrityViolationException(buildMessage(task, sql, sqlEx), sqlEx); logTranslation(task, sql, sqlEx, false); return new PermissionDeniedDataAccessException(buildMessage(task, sql, sqlEx), sqlEx); logTranslation(task, sql, sqlEx, false); return new DataAccessResourceFailureException(buildMessage(task, sql, sqlEx), sqlEx);
@Override protected DataAccessException customTranslate(String task, String sql, SQLException sqlEx) { if (isForeignKeyError(sqlEx)) { return new ForeignKeyViolationException(buildMessage(task, sql, sqlEx), sqlEx); } return super.customTranslate(task, sql, sqlEx); } });
/** * Create a SQL error code translator for the given DataSource. * Invoking this constructor will cause a Connection to be obtained * from the DataSource to get the meta-data. * @param dataSource the DataSource to use to find meta-data and establish * which error codes are usable * @see SQLErrorCodesFactory */ public SQLErrorCodeSQLExceptionTranslator(DataSource dataSource) { this(); setDataSource(dataSource); }
/** * Creates a default SQLErrorCodeSQLExceptionTranslator for the specified * DataSource if none is set. * * @return the exception translator for this instance. */ protected SQLExceptionTranslator getExceptionTranslator() { synchronized(this) { if (exceptionTranslator == null) { if (dataSource != null) { exceptionTranslator = new SQLErrorCodeSQLExceptionTranslator(dataSource); } else { exceptionTranslator = new SQLStateSQLExceptionTranslator(); } } } return exceptionTranslator; }
DataAccessException dae = customTranslate(task, sql, sqlEx); if (dae != null) { return dae; if (Arrays.binarySearch(customTranslation.getErrorCodes(), errorCode) >= 0 && customTranslation.getExceptionClass() != null) { DataAccessException customException = createCustomException( task, sql, sqlEx, customTranslation.getExceptionClass()); if (customException != null) { logTranslation(task, sql, sqlEx, true); return customException; logTranslation(task, sql, sqlEx, false); return new BadSqlGrammarException(task, (sql != null ? sql : ""), sqlEx); logTranslation(task, sql, sqlEx, false); return new InvalidResultSetAccessException(task, (sql != null ? sql : ""), sqlEx); logTranslation(task, sql, sqlEx, false); return new DuplicateKeyException(buildMessage(task, sql, sqlEx), sqlEx); logTranslation(task, sql, sqlEx, false); return new DataIntegrityViolationException(buildMessage(task, sql, sqlEx), sqlEx); logTranslation(task, sql, sqlEx, false); return new PermissionDeniedDataAccessException(buildMessage(task, sql, sqlEx), sqlEx); logTranslation(task, sql, sqlEx, false); return new DataAccessResourceFailureException(buildMessage(task, sql, sqlEx), sqlEx);
/** * Create a SQL error code translator for the given DataSource. * Invoking this constructor will cause a Connection to be obtained * from the DataSource to get the metadata. * @param dataSource DataSource to use to find metadata and establish * which error codes are usable * @see SQLErrorCodesFactory */ public SQLErrorCodeSQLExceptionTranslator(DataSource dataSource) { this(); setDataSource(dataSource); }
@Test public void dataTruncationTranslation() { SQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(ERROR_CODES); SQLException dataAccessEx = new SQLException("", "", 5); DataTruncation dataTruncation = new DataTruncation(1, true, true, 1, 1, dataAccessEx); DataAccessResourceFailureException daex = (DataAccessResourceFailureException) sext.translate("task", "SQL", dataTruncation); assertEquals(dataTruncation, daex.getCause()); }
@Test public void batchExceptionTranslation() { SQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(ERROR_CODES); SQLException badSqlEx = new SQLException("", "", 1); BatchUpdateException batchUpdateEx = new BatchUpdateException(); batchUpdateEx.setNextException(badSqlEx); BadSqlGrammarException bsgex = (BadSqlGrammarException) sext.translate("task", "SQL", batchUpdateEx); assertEquals("SQL", bsgex.getSql()); assertEquals(badSqlEx, bsgex.getSQLException()); }
/** * 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(); this.thrown.expect(CannotGetJdbcConnectionException.class); this.thrown.expect(exceptionCause(sameInstance(sqlException))); this.template.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch); }