/** * 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); }
/** * 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; } }
/** * 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; }
@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 @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 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); }
@Test public void errorCodeTranslation() { SQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(ERROR_CODES); SQLException badSqlEx = new SQLException("", "", 1); BadSqlGrammarException bsgex = (BadSqlGrammarException) sext.translate("task", "SQL", badSqlEx); assertEquals("SQL", bsgex.getSql()); assertEquals(badSqlEx, bsgex.getSQLException()); SQLException invResEx = new SQLException("", "", 4); InvalidResultSetAccessException irsex = (InvalidResultSetAccessException) sext.translate("task", "SQL", invResEx); assertEquals("SQL", irsex.getSql()); assertEquals(invResEx, irsex.getSQLException()); checkTranslation(sext, 5, DataAccessResourceFailureException.class); checkTranslation(sext, 6, DataIntegrityViolationException.class); checkTranslation(sext, 7, CannotAcquireLockException.class); checkTranslation(sext, 8, DeadlockLoserDataAccessException.class); checkTranslation(sext, 9, CannotSerializeTransactionException.class); checkTranslation(sext, 10, DuplicateKeyException.class); SQLException dupKeyEx = new SQLException("", "", 10); DataAccessException dksex = sext.translate("task", "SQL", dupKeyEx); assertTrue("Not instance of DataIntegrityViolationException", DataIntegrityViolationException.class.isAssignableFrom(dksex.getClass())); // Test fallback. We assume that no database will ever return this error code, // but 07xxx will be bad grammar picked up by the fallback SQLState translator SQLException sex = new SQLException("", "07xxx", 666666666); BadSqlGrammarException bsgex2 = (BadSqlGrammarException) sext.translate("task", "SQL2", sex); assertEquals("SQL2", bsgex2.getSql()); assertEquals(sex, bsgex2.getSQLException()); }
@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); }
@Test public void errorCodeTranslation() { SQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(ERROR_CODES);
/** * 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 metadata. * @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); }
public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; this.exceptionTranslator = new SQLErrorCodeSQLExceptionTranslator( dataSource); }
/** * Obtain a default SQLExceptionTranslator, lazily creating it if necessary. * <p> * Creates a default {@link org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator} * for the SessionFactory's underlying DataSource. */ protected synchronized SQLExceptionTranslator getDefaultJdbcExceptionTranslator() { if (this.defaultJdbcExceptionTranslator == null) { this.defaultJdbcExceptionTranslator = new SQLErrorCodeSQLExceptionTranslator(getDataSource()); } return this.defaultJdbcExceptionTranslator; }
@Bean @ConditionalOnProperty(prefix = DomaProperties.DOMA_PREFIX, name = "exception-translation-enabled", matchIfMissing = true) public PersistenceExceptionTranslator exceptionTranslator(Config config) { return new DomaPersistenceExceptionTranslator( new SQLErrorCodeSQLExceptionTranslator(config.getDataSource())); }
public static SQLExceptionTranslator newJdbcExceptionTranslator(SQLSessionFactory sessionFactory) { DataSource ds = getDataSource(sessionFactory); if (ds != null) { return new SQLErrorCodeSQLExceptionTranslator(ds); } return new SQLStateSQLExceptionTranslator(); }
/** * Return the JDBC exception translator for this transaction manager. * <p>Creates a default SQLErrorCodeSQLExceptionTranslator or SQLStateSQLExceptionTranslator * for the specified SessionFactory, if no exception translator explicitly specified. * @see #setJdbcExceptionTranslator */ public SQLExceptionTranslator getJdbcExceptionTranslator() { if (this.jdbcExceptionTranslator == null) { if (getDataSource() != null) { this.jdbcExceptionTranslator = new SQLErrorCodeSQLExceptionTranslator(getDataSource()); } else { this.jdbcExceptionTranslator = new SQLStateSQLExceptionTranslator(); } } return this.jdbcExceptionTranslator; }
protected synchronized SQLExceptionTranslator getDefaultJdbcExceptionTranslator() { if (this.defaultJdbcExceptionTranslator == null) { if (getDataSource() != null) { this.defaultJdbcExceptionTranslator = new SQLErrorCodeSQLExceptionTranslator(getDataSource()); } else { this.defaultJdbcExceptionTranslator = SQLSessionFactoryUtils .newJdbcExceptionTranslator(getSessionFactory()); } } return this.defaultJdbcExceptionTranslator; }
/** * Obtain a default SQLExceptionTranslator, lazily creating it if necessary. * <p>Creates a default * {@link org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator} * for the SessionFactory's underlying DataSource. */ protected synchronized SQLExceptionTranslator getDefaultJdbcExceptionTranslator() { if (this.defaultJdbcExceptionTranslator == null) { if (getDataSource() != null) { this.defaultJdbcExceptionTranslator = new SQLErrorCodeSQLExceptionTranslator(getDataSource()); } else { this.defaultJdbcExceptionTranslator = SessionFactoryUtils.newJdbcExceptionTranslator(getSessionFactory()); } } return this.defaultJdbcExceptionTranslator; }
private ResultSetIterator<?> iterator(Object payload) { SQLExceptionTranslator exceptionTranslator = new SQLErrorCodeSQLExceptionTranslator(this.dataSource); String sql = "SELECT * from FOO"; PreparedStatement preparedStatement = null; try { Connection connection = this.dataSource.getConnection(); preparedStatement = connection.prepareStatement(sql); ResultSet resultSet = preparedStatement.executeQuery(); return new ResultSetIterator<>(connection, resultSet, (rs, rowNum) -> new Foo(rs.getInt(1), rs.getString(2))); } catch (SQLException e) { throw exceptionTranslator.translate("PreparedStatement", sql, e); } } }
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(); } }