Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/hibernate/exception/JDBCExceptionHelper.java


1   // $Id: JDBCExceptionHelper.java,v 1.2 2004/11/21 00:11:27 pgmjsd Exp $
2   package org.hibernate.exception;
3   
4   import org.hibernate.JDBCException;
5   import org.hibernate.util.JDBCExceptionReporter;
6   
7   import java.sql.SQLException;
8   
9   /**
10   * Implementation of JDBCExceptionHelper.
11   *
12   * @author Steve Ebersole
13   */
14  public final class JDBCExceptionHelper {
15  
16    private JDBCExceptionHelper() {
17    }
18  
19    /**
20     * Converts the given SQLException into Hibernate's JDBCException hierarchy, as well as performing
21     * appropriate logging.
22     *
23     * @param converter    The converter to use.
24     * @param sqlException The exception to convert.
25     * @param message      An optional error message.
26     * @return The converted JDBCException.
27     */
28    public static JDBCException convert(SQLExceptionConverter converter, SQLException sqlException, String message) {
29      return convert( converter, sqlException, message, "???" );
30    }
31  
32    /**
33     * Converts the given SQLException into Hibernate's JDBCException hierarchy, as well as performing
34     * appropriate logging.
35     *
36     * @param converter    The converter to use.
37     * @param sqlException The exception to convert.
38     * @param message      An optional error message.
39     * @return The converted JDBCException.
40     */
41    public static JDBCException convert(SQLExceptionConverter converter, SQLException sqlException, String message, String sql) {
42      JDBCExceptionReporter.logExceptions( sqlException, message + " [" + sql + "]" );
43      return converter.convert( sqlException, message, sql );
44    }
45  
46    /**
47     * For the given SQLException, locates the vendor-specific error code.
48     *
49     * @param sqlException The exception from which to extract the SQLState
50     * @return The error code.
51     */
52    public static int extractErrorCode(SQLException sqlException) {
53      int errorCode = sqlException.getErrorCode();
54      SQLException nested = sqlException.getNextException();
55      while ( errorCode == 0 && nested != null ) {
56        errorCode = sqlException.getErrorCode();
57        nested = nested.getNextException();
58      }
59      return errorCode;
60    }
61  
62    /**
63     * For the given SQLException, locates the X/Open-compliant SQLState.
64     *
65     * @param sqlException The exception from which to extract the SQLState
66     * @return The SQLState code, or null.
67     */
68    public static String extractSqlState(SQLException sqlException) {
69      String sqlState = sqlException.getSQLState();
70      SQLException nested = sqlException.getNextException();
71      while ( sqlState == null && nested != null ) {
72        sqlState = nested.getSQLState();
73        nested = nested.getNextException();
74      }
75      return sqlState;
76    }
77  
78    /**
79     * For the given SQLException, locates the X/Open-compliant SQLState's class code.
80     *
81     * @param sqlException The exception from which to extract the SQLState class code
82     * @return The SQLState class code, or null.
83     */
84    public static String extractSqlStateClassCode(SQLException sqlException) {
85      String sqlState = extractSqlState( sqlException );
86  
87      if ( sqlState == null || sqlState.length() < 2 ) {
88        return sqlState;
89      }
90  
91      return sqlState.substring( 0, 2 );
92    }
93  
94  }