KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > exception > SQLStateConverter


1 // $Id: SQLStateConverter.java,v 1.4 2005/03/16 15:43:46 oneovthafew Exp $
2
package org.hibernate.exception;
3
4 import org.hibernate.JDBCException;
5
6 import java.sql.SQLException JavaDoc;
7 import java.util.HashSet JavaDoc;
8 import java.util.Set JavaDoc;
9
10 /**
11  * A SQLExceptionConverter implementation which performs converion based on
12  * the underlying SQLState. Interpretation of a SQL error based on SQLState
13  * is not nearly as accurate as using the ErrorCode (which is, however, vendor-
14  * specific). Use of a ErrorCcode-based converter should be preferred approach
15  * for converting/interpreting SQLExceptions.
16  *
17  * @author Steve Ebersole
18  */

19 public class SQLStateConverter implements SQLExceptionConverter {
20
21     private ViolatedConstraintNameExtracter extracter;
22
23     private static final Set JavaDoc SQL_GRAMMAR_CATEGORIES = new HashSet JavaDoc();
24     private static final Set JavaDoc INTEGRITY_VIOLATION_CATEGORIES = new HashSet JavaDoc();
25     private static final Set JavaDoc CONNECTION_CATEGORIES = new HashSet JavaDoc();
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     /**
46      * Convert the given SQLException into Hibernate's JDBCException hierarchy.
47      *
48      * @param sqlException The SQLException to be converted.
49      * @param message An optional error message.
50      * @param sql Optionally, the sql being performed when the exception occurred.
51      * @return The resulting JDBCException.
52      */

53     public JDBCException convert(SQLException JavaDoc sqlException, String JavaDoc message, String JavaDoc sql) {
54
55         String JavaDoc 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 JavaDoc 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     /**
74      * Handle an exception not converted to a specific type based on the SQLState.
75      *
76      * @param sqlException The exception to be handled.
77      * @param message An optional message
78      * @param sql Optionally, the sql being performed when the exception occurred.
79      * @return The converted exception; should <b>never</b> be null.
80      */

81     protected JDBCException handledNonSpecificException(SQLException JavaDoc sqlException, String JavaDoc message, String JavaDoc sql) {
82         return new GenericJDBCException( message, sqlException, sql );
83     }
84 }
85
Popular Tags