1 package org.hibernate.exception; 3 4 import org.hibernate.JDBCException; 5 6 import java.sql.SQLException ; 7 import java.util.HashSet ; 8 import java.util.Set ; 9 10 19 public class SQLStateConverter implements SQLExceptionConverter { 20 21 private ViolatedConstraintNameExtracter extracter; 22 23 private static final Set SQL_GRAMMAR_CATEGORIES = new HashSet (); 24 private static final Set INTEGRITY_VIOLATION_CATEGORIES = new HashSet (); 25 private static final Set CONNECTION_CATEGORIES = new HashSet (); 26 27 static { 28 SQL_GRAMMAR_CATEGORIES.add( "07" ); 29 SQL_GRAMMAR_CATEGORIES.add( "37" ); 30 SQL_GRAMMAR_CATEGORIES.add( "42" ); 31 SQL_GRAMMAR_CATEGORIES.add( "65" ); 32 SQL_GRAMMAR_CATEGORIES.add( "S0" ); 33 34 INTEGRITY_VIOLATION_CATEGORIES.add( "23" ); 35 INTEGRITY_VIOLATION_CATEGORIES.add( "27" ); 36 INTEGRITY_VIOLATION_CATEGORIES.add( "44" ); 37 38 CONNECTION_CATEGORIES.add( "08" ); 39 } 40 41 public SQLStateConverter(ViolatedConstraintNameExtracter extracter) { 42 this.extracter = extracter; 43 } 44 45 53 public JDBCException convert(SQLException sqlException, String message, String sql) { 54 55 String sqlStateClassCode = JDBCExceptionHelper.extractSqlStateClassCode( sqlException ); 56 57 if ( sqlStateClassCode != null ) { 58 if ( SQL_GRAMMAR_CATEGORIES.contains( sqlStateClassCode ) ) { 59 return new SQLGrammarException( message, sqlException, sql ); 60 } 61 else if ( INTEGRITY_VIOLATION_CATEGORIES.contains( sqlStateClassCode ) ) { 62 String constraintName = extracter.extractConstraintName( sqlException ); 63 return new ConstraintViolationException( message, sqlException, sql, constraintName ); 64 } 65 else if ( CONNECTION_CATEGORIES.contains( sqlStateClassCode ) ) { 66 return new JDBCConnectionException( message, sqlException, sql ); 67 } 68 } 69 70 return handledNonSpecificException( sqlException, message, sql ); 71 } 72 73 81 protected JDBCException handledNonSpecificException(SQLException sqlException, String message, String sql) { 82 return new GenericJDBCException( message, sqlException, sql ); 83 } 84 } 85 | Popular Tags |