/** * Convert the given SQLException into Hibernate's JDBCException hierarchy. * * @param sqlException The SQLException to be converted. * @param message An optional error message. * @param sql Optionally, the sql being performed when the exception occurred. * @return The resulting JDBCException; returns null if it could not be converted. */ @Override public JDBCException convert(SQLException sqlException, String message, String sql) { String sqlStateClassCode = JdbcExceptionHelper.extractSqlStateClassCode( sqlException ); if ( sqlStateClassCode != null ) { Integer errorCode = JdbcExceptionHelper.extractErrorCode( sqlException ); if ( INTEGRITY_VIOLATION_CATEGORIES.contains( errorCode ) ) { String constraintName = getConversionContext() .getViolatedConstraintNameExtracter() .extractConstraintName( sqlException ); return new ConstraintViolationException( message, sqlException, sql, constraintName ); } else if ( DATA_CATEGORIES.contains( sqlStateClassCode ) ) { return new DataException( message, sqlException, sql ); } } return null; // allow other delegates the chance to look }
@Override public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() { return new SQLExceptionConversionDelegate() { @Override public JDBCException convert(SQLException sqlException, String message, String sql) { final String sqlState = JdbcExceptionHelper.extractSqlState( sqlException ); final int errorCode = JdbcExceptionHelper.extractErrorCode( sqlException ); if("JZ0TO".equals( sqlState ) || "JZ006".equals( sqlState )){ throw new LockTimeoutException( message, sqlException, sql ); } if ( 515 == errorCode && "ZZZZZ".equals( sqlState ) ) { // Attempt to insert NULL value into column; column does not allow nulls. final String constraintName = getViolatedConstraintNameExtracter().extractConstraintName( sqlException ); return new ConstraintViolationException( message, sqlException, sql, constraintName ); } return null; } }; }
@Override public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() { return new SQLExceptionConversionDelegate() { @Override public JDBCException convert(SQLException sqlException, String message, String sql) { final String sqlState = JdbcExceptionHelper.extractSqlState( sqlException ); final int errorCode = JdbcExceptionHelper.extractErrorCode( sqlException ); if ( "HY008".equals( sqlState ) ) { throw new QueryTimeoutException( message, sqlException, sql ); } if (1222 == errorCode ) { throw new LockTimeoutException( message, sqlException, sql ); } return null; } }; }
public String extractConstraintName(SQLException sqle) { try { final int sqlState = Integer.valueOf( JdbcExceptionHelper.extractSqlState( sqle ) ); switch (sqlState) { // CHECK VIOLATION case 23514: return extractUsingTemplate( "violates check constraint \"","\"", sqle.getMessage() ); // UNIQUE VIOLATION case 23505: return extractUsingTemplate( "violates unique constraint \"","\"", sqle.getMessage() ); // FOREIGN KEY VIOLATION case 23503: return extractUsingTemplate( "violates foreign key constraint \"","\"", sqle.getMessage() ); // NOT NULL VIOLATION case 23502: return extractUsingTemplate( "null value in column \"","\" violates not-null constraint", sqle.getMessage() ); // TODO: RESTRICT VIOLATION case 23001: return null; // ALL OTHER default: return null; } } catch (NumberFormatException nfe) { return null; } }
@Override public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() { return new SQLExceptionConversionDelegate() { @Override public JDBCException convert(SQLException sqlException, String message, String sql) { final String sqlState = JdbcExceptionHelper.extractSqlState( sqlException ); if ( "40P01".equals( sqlState ) ) { // DEADLOCK DETECTED return new LockAcquisitionException( message, sqlException, sql ); } if ( "55P03".equals( sqlState ) ) { // LOCK NOT AVAILABLE return new PessimisticLockException( message, sqlException, sql ); } // returning null allows other delegates to operate return null; } }; }
@Override public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() { return new SQLExceptionConversionDelegate() { @Override public JDBCException convert(SQLException sqlException, String message, String sql) { final int errorCode = JdbcExceptionHelper.extractErrorCode(sqlException); if (errorCode == SQLITE_TOOBIG || errorCode == SQLITE_MISMATCH) { return new DataException(message, sqlException, sql); } else if (errorCode == SQLITE_BUSY || errorCode == SQLITE_LOCKED) { return new LockAcquisitionException(message, sqlException, sql); } else if ((errorCode >= SQLITE_IOERR && errorCode <= SQLITE_PROTOCOL) || errorCode == SQLITE_NOTADB) { return new JDBCConnectionException(message, sqlException, sql); } // returning null allows other delegates to operate return null; } }; }
@Override public SQLExceptionConverter buildSQLExceptionConverter() { return new SQLExceptionConverter() { @Override public JDBCException convert(SQLException sqlException, String message, String sql) { final String sqlState = JdbcExceptionHelper .extractSqlState(sqlException); if (sqlState != null) { if (SQL_GRAMMAR_CATEGORIES.contains(sqlState)) { return new SQLGrammarException(message, sqlException, sql); } else if (DATA_CATEGORIES.contains(sqlState)) { return new DataException(message, sqlException, sql); } else if (LOCK_ACQUISITION_CATEGORIES.contains(sqlState)) { return new LockAcquisitionException(message, sqlException, sql); } } return null; } }; }
public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() { return new SQLExceptionConversionDelegate() { @Override public JDBCException convert(SQLException sqlException, String message, String sql) { final String sqlState = JdbcExceptionHelper .extractSqlState(sqlException); if (sqlState != null) { if (SQL_GRAMMAR_CATEGORIES.contains(sqlState)) { return new SQLGrammarException(message, sqlException, sql); } else if (DATA_CATEGORIES.contains(sqlState)) { return new DataException(message, sqlException, sql); } else if (LOCK_ACQUISITION_CATEGORIES.contains(sqlState)) { return new LockAcquisitionException(message, sqlException, sql); } } return null; } }; }
@Override public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() { return new SQLExceptionConversionDelegate() { @Override public JDBCException convert(SQLException sqlException, String message, String sql) { final int errorCode = JdbcExceptionHelper.extractErrorCode(sqlException); if (errorCode == SQLITE_TOOBIG || errorCode == SQLITE_MISMATCH) { return new DataException(message, sqlException, sql); } else if (errorCode == SQLITE_BUSY || errorCode == SQLITE_LOCKED) { return new LockAcquisitionException(message, sqlException, sql); } else if ((errorCode >= SQLITE_IOERR && errorCode <= SQLITE_PROTOCOL) || errorCode == SQLITE_NOTADB) { return new JDBCConnectionException(message, sqlException, sql); } return null; } }; }
public String extractConstraintName(SQLException sqle) { try { final int sqlState = Integer.valueOf( JdbcExceptionHelper.extractSqlState( sqle ) ); switch (sqlState) { // CHECK VIOLATION - TODO: When added //case 23514: return extractUsingTemplate( "violates check constraint \"","\"", sqle.getMessage() ); // UNIQUE VIOLATION case 23501: return extractUsingTemplate( "violates unique constraint ","\"", sqle.getMessage() ); // FOREIGN KEY VIOLATION case 23503: return extractUsingTemplate( "due to foreign key constraint "," on ", sqle.getMessage() ); // NOT NULL VIOLATION case 23502: return extractUsingTemplate( "NULL value not permitted for ","\"", sqle.getMessage() ); // TODO: RESTRICT VIOLATION //case 23001: return null; // ALL OTHER default: return null; } } catch (NumberFormatException nfe) { return null; } }
@Override public JDBCException convert(SQLException sqlException, String message, String sql) { final int errorCode = JdbcExceptionHelper.extractErrorCode(sqlException); if (errorCode == SQLITE_CONSTRAINT) { final String constraintName = EXTRACTER.extractConstraintName(sqlException); return new ConstraintViolationException(message, sqlException, sql, constraintName); } else if (errorCode == SQLITE_TOO_BIG || errorCode == SQLITE_MISMATCH) { return new DataException(message, sqlException, sql); } else if (errorCode == SQLITE_BUSY || errorCode == SQLITE_LOCKED) { return new LockAcquisitionException(message, sqlException, sql); } else if ((errorCode >= SQLITE_IO_ERR && errorCode <= SQLITE_PROTOCOL) || errorCode == SQLITE_NOT_ADB) { return new JDBCConnectionException(message, sqlException, sql); } return new GenericJDBCException(message, sqlException, sql); }
@Before public void setup() { conversionDelegate = new SQLiteSQLExceptionConversionDelegate(); sqlException = mock(SQLException.class); when(sqlException.getMessage()).thenReturn("message"); PowerMockito.mockStatic(JdbcExceptionHelper.class); }
@Test public void returnsConstraintViolationException() { when(JdbcExceptionHelper.extractErrorCode(sqlException)).thenReturn(19); JDBCException exception = conversionDelegate.convert(sqlException, "message", "sql"); assertTrue(exception instanceof ConstraintViolationException); assertEquals("message", exception.getMessage()); assertEquals("sql", exception.getSQL()); }
@Test public void returnsDataExceptionForSqliteTooBigError() { when(JdbcExceptionHelper.extractErrorCode(sqlException)).thenReturn(18); JDBCException exception = conversionDelegate.convert(sqlException, "message", "sql"); assertTrue(exception instanceof DataException); assertEquals("message", exception.getMessage()); assertEquals("sql", exception.getSQL()); }
@Test public void returnsDataExceptionForSqliteMismatch() { when(JdbcExceptionHelper.extractErrorCode(sqlException)).thenReturn(20); JDBCException exception = conversionDelegate.convert(sqlException, "message", "sql"); assertTrue(exception instanceof DataException); assertEquals("message", exception.getMessage()); assertEquals("sql", exception.getSQL()); }
@Test public void returnsLockAcquisitionExceptionForSqliteBusy() { when(JdbcExceptionHelper.extractErrorCode(sqlException)).thenReturn(5); JDBCException exception = conversionDelegate.convert(sqlException, "message", "sql"); assertTrue(exception instanceof LockAcquisitionException); assertEquals("message", exception.getMessage()); assertEquals("sql", exception.getSQL()); }
@Test public void returnsLockAcquisitionExceptionForSqliteLocked() { when(JdbcExceptionHelper.extractErrorCode(sqlException)).thenReturn(6); JDBCException exception = conversionDelegate.convert(sqlException, "message", "sql"); assertTrue(exception instanceof LockAcquisitionException); assertEquals("message", exception.getMessage()); assertEquals("sql", exception.getSQL()); }
@Test public void returnsJDBCConnectionExceptionForSqliteCorrupt() { when(JdbcExceptionHelper.extractErrorCode(sqlException)).thenReturn(11); JDBCException exception = conversionDelegate.convert(sqlException, "message", "sql"); assertTrue(exception instanceof JDBCConnectionException); assertEquals("message", exception.getMessage()); assertEquals("sql", exception.getSQL()); }
@Test public void returnsJDBCConnectionExceptionForSqliteNotAbd() { when(JdbcExceptionHelper.extractErrorCode(sqlException)).thenReturn(26); JDBCException exception = conversionDelegate.convert(sqlException, "message", "sql"); assertTrue(exception instanceof JDBCConnectionException); assertEquals("message", exception.getMessage()); assertEquals("sql", exception.getSQL()); }
@Test public void returnsGenericJDBCExceptionForEverythingElse() { when(JdbcExceptionHelper.extractErrorCode(sqlException)).thenReturn(41); JDBCException exception = conversionDelegate.convert(sqlException, "message", "sql"); assertTrue(exception instanceof GenericJDBCException); assertEquals("message", exception.getMessage()); assertEquals("sql", exception.getSQL()); }
@Override public String extractConstraintName(SQLException sqle) { String constraintName = null; final int errorCode = JdbcExceptionHelper.extractErrorCode( sqle ); if ( errorCode == -8 ) { constraintName = extractUsingTemplate( "Integrity constraint violation ", " table:", sqle.getMessage() ); } else if ( errorCode == -9 ) { constraintName = extractUsingTemplate( "Violation of unique index: ", " in statement [", sqle.getMessage() ); } else if ( errorCode == -104 ) { constraintName = extractUsingTemplate( "Unique constraint violation: ", " in statement [", sqle.getMessage() ); } else if ( errorCode == -177 ) { constraintName = extractUsingTemplate( "Integrity constraint violation - no parent ", " table:", sqle.getMessage() ); } return constraintName; }
@Override public String extractConstraintName(SQLException sqle) { String constraintName = null; final int errorCode = JdbcExceptionHelper.extractErrorCode( sqle ); if ( errorCode == -8 ) { constraintName = extractUsingTemplate( "; ", " table: ", sqle.getMessage() ); } else if ( errorCode == -9 ) { constraintName = extractUsingTemplate( "; ", " table: ", sqle.getMessage() ); } else if ( errorCode == -104 ) { constraintName = extractUsingTemplate( "; ", " table: ", sqle.getMessage() ); } else if ( errorCode == -177 ) { constraintName = extractUsingTemplate( "; ", " table: ", sqle.getMessage() ); } return constraintName; }
public String extractConstraintName(SQLException sqle) { try { final int sqlState = Integer.valueOf( JdbcExceptionHelper.extractSqlState( sqle ) ).intValue(); switch ( sqlState ) { case 23000: return extractUsingTemplate( " for key '", "'", sqle.getMessage() ); default: return null; } } catch ( NumberFormatException nfe ) { return null; } }
/** * Extract the name of the violated constraint from the given SQLException. * * @param sqle The exception that was the result of the constraint violation. * @return The extracted constraint name. */ public String extractConstraintName(SQLException sqle) { final int errorCode = JdbcExceptionHelper.extractErrorCode( sqle ); if ( errorCode == 1 || errorCode == 2291 || errorCode == 2292 ) { return extractUsingTemplate( "(", ")", sqle.getMessage() ); } else if ( errorCode == 1400 ) { // simple nullability constraint return null; } else { return null; } }
@Override public String extractConstraintName(SQLException sqle) { String constraintName = null; final int errorCode = JdbcExceptionHelper.extractErrorCode( sqle ); if ( errorCode == -268 ) { constraintName = extractUsingTemplate( "Unique constraint (", ") violated.", sqle.getMessage() ); } else if ( errorCode == -691 ) { constraintName = extractUsingTemplate( "Missing key in referenced table for referential constraint (", ").", sqle.getMessage() ); } else if ( errorCode == -692 ) { constraintName = extractUsingTemplate( "Key value for constraint (", ") is still being referenced.", sqle.getMessage() ); } if ( constraintName != null ) { // strip table-owner because Informix always returns constraint names as "<table-owner>.<constraint-name>" final int i = constraintName.indexOf( '.' ); if ( i != -1 ) { constraintName = constraintName.substring( i + 1 ); } } return constraintName; }
@Override public String extractConstraintName(SQLException sqle) { final int errorCode = JdbcExceptionHelper.extractErrorCode( sqle ); if ( errorCode == 1 || errorCode == 2291 || errorCode == 2292 ) { return extractUsingTemplate( "constraint (", ") violated", sqle.getMessage() ); } else if ( errorCode == 1400 ) { // simple nullability constraint return null; } else { return null; } }
@Override public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() { return new SQLExceptionConversionDelegate() { @Override public JDBCException convert(SQLException sqlException, String message, String sql) { final String sqlState = JdbcExceptionHelper.extractSqlState( sqlException ); final int errorCode = JdbcExceptionHelper.extractErrorCode( sqlException ); if( -952 == errorCode && "57014".equals( sqlState )){ throw new LockTimeoutException( message, sqlException, sql ); } return null; } }; }
@Override protected String doExtractConstraintName(SQLException sqle) throws NumberFormatException { final int errorCode = JdbcExceptionHelper.extractErrorCode(sqle); if (errorCode == SQLITE_CONSTRAINT) { return extractUsingTemplate("constraint ", " failed", sqle.getMessage()); } return null; }
@Override public JDBCException convert(SQLException sqlException, String message, String sql) { final String sqlState = JdbcExceptionHelper.extractSqlState( sqlException ); final int errorCode = JdbcExceptionHelper.extractErrorCode( sqlException ); if ( sqlState != null ) { String sqlStateClassCode = JdbcExceptionHelper.determineSqlStateClassCode( sqlState ); if ( sqlStateClassCode != null ) { if ( SQL_GRAMMAR_CATEGORIES.contains( sqlStateClassCode ) ) { return new SQLGrammarException( message, sqlException, sql ); } else if ( INTEGRITY_VIOLATION_CATEGORIES.contains( sqlStateClassCode ) ) { final String constraintName = getConversionContext() .getViolatedConstraintNameExtracter() .extractConstraintName( sqlException ); return new ConstraintViolationException( message, sqlException, sql, constraintName ); } else if ( CONNECTION_CATEGORIES.contains( sqlStateClassCode ) ) { return new JDBCConnectionException( message, sqlException, sql ); } else if ( DATA_CATEGORIES.contains( sqlStateClassCode ) ) { return new DataException( message, sqlException, sql ); } } if ( "40001".equals( sqlState ) ) { return new LockAcquisitionException( message, sqlException, sql ); } if ( "40XL1".equals( sqlState ) || "40XL2".equals( sqlState )) { // Derby "A lock could not be obtained within the time requested." return new PessimisticLockException( message, sqlException, sql ); } // MySQL Query execution was interrupted if ( "70100".equals( sqlState ) || // Oracle user requested cancel of current operation ( "72000".equals( sqlState ) && errorCode == 1013 ) ) { throw new QueryTimeoutException( message, sqlException, sql ); } } return null; }
@Override public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() { return new SQLExceptionConversionDelegate() { @Override public JDBCException convert(SQLException sqlException, String message, String sql) { final String sqlState = JdbcExceptionHelper.extractSqlState( sqlException ); if ( "41000".equals( sqlState ) ) { return new LockTimeoutException( message, sqlException, sql ); } if ( "40001".equals( sqlState ) ) { return new LockAcquisitionException( message, sqlException, sql ); } return null; } }; }
@Override public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() { return new SQLExceptionConversionDelegate() { @Override public JDBCException convert(SQLException sqlException, String message, String sql) { // interpreting Oracle exceptions is much much more precise based on their specific vendor codes. final int errorCode = JdbcExceptionHelper.extractErrorCode( sqlException ); // lock timeouts ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ if ( errorCode == 30006 ) { // ORA-30006: resource busy; acquire with WAIT timeout expired throw new LockTimeoutException( message, sqlException, sql ); } else if ( errorCode == 54 ) { // ORA-00054: resource busy and acquire with NOWAIT specified or timeout expired throw new LockTimeoutException( message, sqlException, sql ); } else if ( 4021 == errorCode ) { // ORA-04021 timeout occurred while waiting to lock object throw new LockTimeoutException( message, sqlException, sql ); } // deadlocks ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ if ( 60 == errorCode ) { // ORA-00060: deadlock detected while waiting for resource return new LockAcquisitionException( message, sqlException, sql ); } else if ( 4020 == errorCode ) { // ORA-04020 deadlock detected while trying to lock object return new LockAcquisitionException( message, sqlException, sql ); } // query cancelled ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ if ( 1013 == errorCode ) { // ORA-01013: user requested cancel of current operation throw new QueryTimeoutException( message, sqlException, sql ); } // data integrity violation ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ if ( 1407 == errorCode ) { // ORA-01407: cannot update column to NULL final String constraintName = getViolatedConstraintNameExtracter().extractConstraintName( sqlException ); return new ConstraintViolationException( message, sqlException, sql, constraintName ); } return null; } }; }
@Override public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() { return new SQLExceptionConversionDelegate() { @Override public JDBCException convert(final SQLException sqlException, final String message, final String sql) { final int errorCode = JdbcExceptionHelper.extractErrorCode( sqlException ); if ( errorCode == 131 ) { // 131 - Transaction rolled back by lock wait timeout return new LockTimeoutException( message, sqlException, sql ); } if ( errorCode == 146 ) { // 146 - Resource busy and acquire with NOWAIT specified return new LockTimeoutException( message, sqlException, sql ); } if ( errorCode == 132 ) { // 132 - Transaction rolled back due to unavailable resource return new LockAcquisitionException( message, sqlException, sql ); } if ( errorCode == 133 ) { // 133 - Transaction rolled back by detected deadlock return new LockAcquisitionException( message, sqlException, sql ); } // 259 - Invalid table name // 260 - Invalid column name // 261 - Invalid index name // 262 - Invalid query name // 263 - Invalid alias name if ( errorCode == 257 || ( errorCode >= 259 && errorCode <= 263 ) ) { throw new SQLGrammarException( message, sqlException, sql ); } // 257 - Cannot insert NULL or update to NULL // 301 - Unique constraint violated // 461 - foreign key constraint violation // 462 - failed on update or delete by foreign key constraint violation if ( errorCode == 287 || errorCode == 301 || errorCode == 461 || errorCode == 462 ) { final String constraintName = getViolatedConstraintNameExtracter().extractConstraintName( sqlException ); return new ConstraintViolationException( message, sqlException, sql, constraintName ); } return null; } }; }