public StoredProcedureExceptionTranslator(DataSource ds) { setDataSource(ds); setSql(SQL); getJdbcTemplate().setExceptionTranslator(new SQLExceptionTranslator() { @Override public DataAccessException translate(String task, @Nullable String sql, SQLException ex) { return new CustomDataException(sql, ex); } }); compile(); }
@Before public void setUp() throws SQLException { given(connection.createStatement()).willReturn(statement); given(connection.prepareStatement(anyString())).willReturn(preparedStatement); given(statement.executeQuery(anyString())).willReturn(resultSet); given(preparedStatement.executeQuery()).willReturn(resultSet); given(resultSet.next()).willReturn(true, true, false); given(resultSet.getString(1)).willReturn("tb1", "tb2"); given(resultSet.getInt(2)).willReturn(1, 2); template.setDataSource(new SingleConnectionDataSource(connection, false)); template.setExceptionTranslator(new SQLStateSQLExceptionTranslator()); template.afterPropertiesSet(); }
/** * Test that we see an SQLException translated using Error Code. * If we provide the SQLExceptionTranslator, we shouldn't use a connection * to get the metadata */ @Test public void testUseCustomSQLErrorCodeTranslator() throws Exception { // Bad SQL state final SQLException sqlException = new SQLException("I have a known problem", "07000", 1054); final String sql = "SELECT ID FROM CUSTOMER"; given(this.resultSet.next()).willReturn(true); given(this.connection.createStatement()).willReturn(this.preparedStatement); JdbcTemplate template = new JdbcTemplate(); template.setDataSource(this.dataSource); // Set custom exception translator template.setExceptionTranslator(new SQLStateSQLExceptionTranslator()); template.afterPropertiesSet(); this.thrown.expect(BadSqlGrammarException.class); this.thrown.expect(exceptionCause(sameInstance(sqlException))); try { template.query(sql, (RowCallbackHandler) rs -> { throw sqlException; }); } finally { verify(this.resultSet).close(); verify(this.preparedStatement).close(); verify(this.connection).close(); } }
/** * 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); }
@SuppressWarnings("unchecked") public Mock(MockType type) throws Exception { connection = mock(Connection.class); statement = mock(Statement.class); resultSet = mock(ResultSet.class); resultSetMetaData = mock(ResultSetMetaData.class); given(connection.createStatement()).willReturn(statement); given(statement.executeQuery(anyString())).willReturn(resultSet); given(resultSet.getMetaData()).willReturn(resultSetMetaData); given(resultSet.next()).willReturn(true, false); given(resultSet.getString(1)).willReturn("Bubba"); given(resultSet.getLong(2)).willReturn(22L); given(resultSet.getTimestamp(3)).willReturn(new Timestamp(1221222L)); given(resultSet.getObject(anyInt(), any(Class.class))).willThrow(new SQLFeatureNotSupportedException()); given(resultSet.getDate(3)).willReturn(new java.sql.Date(1221222L)); given(resultSet.getBigDecimal(4)).willReturn(new BigDecimal("1234.56")); given(resultSet.wasNull()).willReturn(type == MockType.TWO); given(resultSetMetaData.getColumnCount()).willReturn(4); given(resultSetMetaData.getColumnLabel(1)).willReturn( type == MockType.THREE ? "Last Name" : "name"); given(resultSetMetaData.getColumnLabel(2)).willReturn("age"); given(resultSetMetaData.getColumnLabel(3)).willReturn("birth_date"); given(resultSetMetaData.getColumnLabel(4)).willReturn("balance"); jdbcTemplate = new JdbcTemplate(); jdbcTemplate.setDataSource(new SingleConnectionDataSource(connection, false)); jdbcTemplate.setExceptionTranslator(new SQLStateSQLExceptionTranslator()); jdbcTemplate.afterPropertiesSet(); }
public void setExceptionTranslator( SQLExceptionTranslator exceptionTranslator) { delegate.setExceptionTranslator(exceptionTranslator); }
public void setExceptionTranslator( SQLExceptionTranslator exceptionTranslator) { delegate.setExceptionTranslator(exceptionTranslator); }
@Bean public JdbcTemplate jdbcTemplate() { JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); jdbcTemplate.setExceptionTranslator(postgreSqlExceptionTranslator); return jdbcTemplate; }
@Produces @Singleton public JdbcTemplate produceJdbcTemplate( DataSource dataSource, DbEngineDialect dbEngineDialect, SQLExceptionTranslator sqlExceptionTranslator) { final JdbcTemplate jdbcTemplate = dbEngineDialect.createJdbcTemplate(dataSource); jdbcTemplate.setExceptionTranslator(sqlExceptionTranslator); return jdbcTemplate; }
private void createJdbcTemplate() { jdbcTemplate = new JdbcTemplate(dataSource); jdbcTemplate.setExceptionTranslator(new OracleSQLExceptionTranslator()); }
/** * this is a utility class which is issues SQL queries on database * */ protected FsSqlDriver(DataSource dataSource) throws ChimeraFsException { _jdbc = new JdbcTemplate(dataSource); _jdbc.setExceptionTranslator(new SQLErrorCodeSQLExceptionTranslator(dataSource) { @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); } }); Long root = getInumber("000000000000000000000000000000000000"); if (root == null) { throw new FileNotFoundHimeraFsException("Root inode does not exist."); } _root = root; }
/** * Execute list of {@code SqlCommand} by suppressing errors if those are given * with a command. * * @param connection the connection * @param commands the sql commands */ public void execute(Connection connection, List<SqlCommand> commands) { JdbcTemplate jdbcTemplate = new JdbcTemplate(new SingleConnectionDataSource(connection, true)); SQLExceptionTranslator origExceptionTranslator = jdbcTemplate.getExceptionTranslator(); for (SqlCommand command : commands) { if(!ObjectUtils.isEmpty(command.getSuppressedErrorCodes())) { jdbcTemplate.setExceptionTranslator(new SuppressSQLErrorCodesTranslator(command.getSuppressedErrorCodes())); } try { logger.debug("Executing command {}", command.getCommand()); jdbcTemplate.execute(command.getCommand()); } catch (SuppressDataAccessException e) { logger.debug("Suppressing error {}", e); } // restore original translator in case next command // doesn't define suppressing codes. jdbcTemplate.setExceptionTranslator(origExceptionTranslator); } } }
/** * Execute list of {@code SqlCommand} by suppressing errors if those are given * with a command. * * @param connection the connection * @param commands the sql commands */ public void execute(Connection connection, List<SqlCommand> commands) { JdbcTemplate jdbcTemplate = new JdbcTemplate(new SingleConnectionDataSource(connection, true)); SQLExceptionTranslator origExceptionTranslator = jdbcTemplate.getExceptionTranslator(); for (SqlCommand command : commands) { if (!ObjectUtils.isEmpty(command.getSuppressedErrorCodes())) { jdbcTemplate.setExceptionTranslator(new SuppressSQLErrorCodesTranslator(command.getSuppressedErrorCodes())); } try { logger.debug("executing command {}", command.getCommand()); jdbcTemplate.execute(command.getCommand()); } catch (SuppressDataAccessException e) { logger.debug("Suppressing error {}", e); } // restore original translator in case next command // doesn't define suppressing codes. jdbcTemplate.setExceptionTranslator(origExceptionTranslator); } } }