1 package org.hibernate.exception; 3 4 import org.hibernate.JDBCException; 5 6 import java.sql.SQLException ; 7 8 16 public class ErrorCodeConverter implements SQLExceptionConverter { 17 18 private ViolatedConstraintNameExtracter extracter; 19 20 public ErrorCodeConverter(ViolatedConstraintNameExtracter extracter) { 21 this.extracter = extracter; 22 } 23 24 29 protected int[] getSQLGrammarErrorCodes() { 30 return null; 31 } 32 33 38 protected int[] getConnectionErrorCodes() { 39 return null; 40 } 41 42 47 protected int[] getIntegrityViolationErrorCodes() { 48 return null; 49 } 50 51 protected int[] getLockAcquisitionErrorCodes() { 52 return null; 53 } 54 55 63 public JDBCException convert(SQLException sqlException, String message, String sql) { 64 int errorCode = JDBCExceptionHelper.extractErrorCode( sqlException ); 65 66 if ( isMatch( getConnectionErrorCodes(), errorCode ) ) { 67 return new JDBCConnectionException( message, sqlException, sql ); 68 } 69 else if ( isMatch( getSQLGrammarErrorCodes(), errorCode ) ) { 70 return new SQLGrammarException( message, sqlException, sql ); 71 } 72 else if ( isMatch( getIntegrityViolationErrorCodes(), errorCode ) ) { 73 String constraintName = extracter.extractConstraintName( sqlException ); 74 return new ConstraintViolationException( message, sqlException, sql, constraintName ); 75 } 76 else if ( isMatch( getLockAcquisitionErrorCodes(), errorCode ) ) { 77 return new LockAcquisitionException( message, sqlException, sql ); 78 } 79 80 return handledNonSpecificException( sqlException, message, sql ); 81 } 82 83 91 protected JDBCException handledNonSpecificException(SQLException sqlException, String message, String sql) { 92 return new GenericJDBCException( message, sqlException, sql ); 93 } 94 95 private boolean isMatch(int[] errorCodes, int errorCode) { 96 if ( errorCodes != null ) { 97 for ( int i = 0, max = errorCodes.length; i < max; i++ ) { 98 if ( errorCodes[i] == errorCode ) { 99 return true; 100 } 101 } 102 } 103 return false; 104 } 105 } 106 | Popular Tags |