/** * Gets the SQL state code from the supplied {@link SQLException exception}. * <p>Some JDBC drivers nest the actual exception from a batched update, so we * might need to dig down into the nested exception. * @param ex the exception from which the {@link SQLException#getSQLState() SQL state} * is to be extracted * @return the SQL state code */ @Nullable private String getSqlState(SQLException ex) { String sqlState = ex.getSQLState(); if (sqlState == null) { SQLException nestedEx = ex.getNextException(); if (nestedEx != null) { sqlState = nestedEx.getSQLState(); } } return sqlState; }
/** * @param throwable The exception to examine. * @return The wrapped exception, or {@code null} if not found. */ private static Throwable getCauseUsingWellKnownTypes(Throwable throwable) { if (throwable instanceof SQLException) return ((SQLException)throwable).getNextException(); if (throwable instanceof InvocationTargetException) return ((InvocationTargetException)throwable).getTargetException(); return null; }
/** * For the given SQLException, locates the X/Open-compliant SQLState. * * @param sqlException The exception from which to extract the SQLState * @return The SQLState code, or null. */ public static String extractSqlState(SQLException sqlException) { String sqlState = sqlException.getSQLState(); SQLException nested = sqlException.getNextException(); while ( sqlState == null && nested != null ) { sqlState = nested.getSQLState(); nested = nested.getNextException(); } return sqlState; }
/** * For the given SQLException, locates the vendor-specific error code. * * @param sqlException The exception from which to extract the SQLState * @return The error code. */ public static int extractErrorCode(SQLException sqlException) { int errorCode = sqlException.getErrorCode(); SQLException nested = sqlException.getNextException(); while ( errorCode == 0 && nested != null ) { errorCode = nested.getErrorCode(); nested = nested.getNextException(); } return errorCode; }
public static void log(String msg, SQLException e) { String s = msg + e.getMessage(); while (e.getNextException() != null) { e = e.getNextException(); s += ", due to: " + e.getMessage(); } LOG.warn(s, e); }
/** * Logback does not log exceptions associated to {@link java.sql.SQLException#getNextException()}. * See http://jira.qos.ch/browse/LOGBACK-775 */ public static void log(Logger logger, SQLException e) { SQLException next = e.getNextException(); while (next != null) { logger.error("SQL error: {}. Message: {}", next.getSQLState(), next.getMessage()); next = next.getNextException(); } }
@Override public String extractConstraintName(SQLException sqle) { try { String constraintName = null; // handle nested exceptions do { constraintName = doExtractConstraintName(sqle); if (sqle.getNextException() == null || sqle.getNextException() == sqle) { break; } else { sqle = sqle.getNextException(); } } while (constraintName == null); return constraintName; } catch (NumberFormatException nfe) { return null; } }
/** * Gets the next {@code SQLWarning} chained to this {@code SQLWarning} object. * * @return the {@code SQLWarning} chained to this {@code SQLWarning}. * {@code null} if no {@code SQLWarning} is chained to this {@code * SQLWarning}. */ public SQLWarning getNextWarning() { SQLException next = super.getNextException(); if (next == null) { return null; } if (next instanceof SQLWarning) { return (SQLWarning) next; } throw new Error("SQLWarning chain holds value that is not a SQLWarning"); }
/** * <p> * Uses <code>instanceof</code> checks to examine the exception, looking for well known types which could contain * chained or wrapped exceptions. * </p> * * @param throwable the exception to examine * @return The wrapped exception, or <code>null</code> if not found. */ private static Throwable getCauseUsingWellKnownTypes( Throwable throwable ) { if ( throwable instanceof SQLException ) { return ( (SQLException) throwable ).getNextException(); } else if ( throwable instanceof InvocationTargetException ) { return ( (InvocationTargetException) throwable ).getTargetException(); } else { return null; } }
/** * SQLExceptions thrown from batch executions have errors * in a {@link SQLException#getNextException()} chain, which * doesn't print out when you log them. Convert them to be * {@link Throwable#addSuppressed(Throwable)} exceptions, * which do print out with common logging frameworks. * * @param e the exception * @return the 'suppressed' munged exception change */ static SQLException mungeBatchException(SQLException e) { for (SQLException next = e.getNextException(); next != null; next = next.getNextException()) { e.addSuppressed(next); } return e; } }
/** * <p>Finds a <code>Throwable</code> for known types.</p> * * <p>Uses <code>instanceof</code> checks to examine the exception, * looking for well known types which could contain chained or * wrapped exceptions.</p> * * @param throwable the exception to examine * @return the wrapped exception, or <code>null</code> if not found */ private static Throwable getCauseUsingWellKnownTypes(Throwable throwable) { if (throwable instanceof Nestable) { return ((Nestable) throwable).getCause(); } else if (throwable instanceof SQLException) { return ((SQLException) throwable).getNextException(); } else if (throwable instanceof InvocationTargetException) { return ((InvocationTargetException) throwable).getTargetException(); } else { return null; } }
/** * Prints the stack trace to the specified print stream. * * @param s the print stream */ @Override public void printStackTrace(PrintStream s) { if (s != null) { super.printStackTrace(s); // getNextException().printStackTrace(s) would be very very slow // if many exceptions are joined SQLException next = getNextException(); for (int i = 0; i < 100 && next != null; i++) { s.println(next.toString()); next = next.getNextException(); } if (next != null) { s.println("(truncated)"); } } }
/** * Prints the stack trace to the specified print writer. * * @param s the print writer */ @Override public void printStackTrace(PrintWriter s) { if (s != null) { super.printStackTrace(s); // getNextException().printStackTrace(s) would be very very slow // if many exceptions are joined SQLException next = getNextException(); for (int i = 0; i < 100 && next != null; i++) { s.println(next.toString()); next = next.getNextException(); } if (next != null) { s.println("(truncated)"); } } }
public void printSQLException( SQLException ex ) { log.logError( "==> SQLException: " ); while ( ex != null ) { log.logError( "Message: " + ex.getMessage() ); log.logError( "SQLState: " + ex.getSQLState() ); log.logError( "ErrorCode: " + ex.getErrorCode() ); ex = ex.getNextException(); log.logError( "" ); } }
public static KettleDatabaseBatchException createKettleDatabaseBatchException( String message, SQLException ex ) { KettleDatabaseBatchException kdbe = new KettleDatabaseBatchException( message, ex ); if ( ex instanceof BatchUpdateException ) { kdbe.setUpdateCounts( ( (BatchUpdateException) ex ).getUpdateCounts() ); } else { // Null update count forces rollback of batch kdbe.setUpdateCounts( null ); } List<Exception> exceptions = new ArrayList<Exception>(); SQLException nextException = ex.getNextException(); SQLException oldException = null; // This construction is specifically done for some JDBC drivers, these // drivers // always return the same exception on getNextException() (and thus go // into an infinite loop). // So it's not "equals" but != (comments from Sven Boden). while ( ( nextException != null ) && ( oldException != nextException ) ) { exceptions.add( nextException ); oldException = nextException; nextException = nextException.getNextException(); } kdbe.setExceptionsList( exceptions ); return kdbe; }
public void executeUpdateSql(String sql, Object... params) { try (Connection connection = getConnection()) { new QueryRunner().update(connection, sql, params); if (!connection.getAutoCommit()) { connection.commit(); } } catch (SQLException e) { SQLException nextException = e.getNextException(); if (nextException != null) { throw new IllegalStateException("Fail to execute sql: " + sql, new SQLException(e.getMessage(), nextException.getSQLState(), nextException.getErrorCode(), nextException)); } throw new IllegalStateException("Fail to execute sql: " + sql, e); } catch (Exception e) { throw new IllegalStateException("Fail to execute sql: " + sql, e); } }
private void assertSuppressions(Throwable cause) { LoggerFactory.getLogger(TestBatchExceptionRewrite.class).info("exception", cause); SQLException e = (SQLException) cause; SQLException nextException = e.getNextException(); assertThat((Exception) nextException).isEqualTo(e.getSuppressed()[0]); assertThat((Exception) nextException.getNextException()).isNull(); assertThat(e.getSuppressed()).hasSize(1); } }
protected DataAccessException doTranslate(String task, @Nullable String sql, SQLException ex) { SQLException sqlEx = ex; if (sqlEx instanceof BatchUpdateException && sqlEx.getNextException() != null) { SQLException nestedSqlEx = sqlEx.getNextException(); if (nestedSqlEx.getErrorCode() > 0 || nestedSqlEx.getSQLState() != null) { logger.debug("Using nested SQLException from the BatchUpdateException");
public void handleCompletion() throws SQLException { updateGeneratedKeys(); SQLException batchException = getException(); if (batchException != null) { if (isAutoCommit()) { // Re-create batch exception since rows after exception might indeed succeed. BatchUpdateException newException = new BatchUpdateException( batchException.getMessage(), batchException.getSQLState(), uncompressUpdateCount() ); newException.initCause(batchException.getCause()); SQLException next = batchException.getNextException(); if (next != null) { newException.setNextException(next); } batchException = newException; } throw batchException; } }
System.err.println( e.getSQLException().getNextException() );