001: // $Id: JDBCExceptionHelper.java 9557 2006-03-06 15:16:27Z steve.ebersole@jboss.com $
002: package org.hibernate.exception;
003:
004: import org.hibernate.JDBCException;
005: import org.hibernate.util.JDBCExceptionReporter;
006:
007: import java.sql.SQLException;
008:
009: /**
010: * Implementation of JDBCExceptionHelper.
011: *
012: * @author Steve Ebersole
013: */
014: public final class JDBCExceptionHelper {
015:
016: private JDBCExceptionHelper() {
017: }
018:
019: /**
020: * Converts the given SQLException into Hibernate's JDBCException hierarchy, as well as performing
021: * appropriate logging.
022: *
023: * @param converter The converter to use.
024: * @param sqlException The exception to convert.
025: * @param message An optional error message.
026: * @return The converted JDBCException.
027: */
028: public static JDBCException convert(
029: SQLExceptionConverter converter, SQLException sqlException,
030: String message) {
031: return convert(converter, sqlException, message, "???");
032: }
033:
034: /**
035: * Converts the given SQLException into Hibernate's JDBCException hierarchy, as well as performing
036: * appropriate logging.
037: *
038: * @param converter The converter to use.
039: * @param sqlException The exception to convert.
040: * @param message An optional error message.
041: * @return The converted JDBCException.
042: */
043: public static JDBCException convert(
044: SQLExceptionConverter converter, SQLException sqlException,
045: String message, String sql) {
046: JDBCExceptionReporter.logExceptions(sqlException, message
047: + " [" + sql + "]");
048: return converter.convert(sqlException, message, sql);
049: }
050:
051: /**
052: * For the given SQLException, locates the vendor-specific error code.
053: *
054: * @param sqlException The exception from which to extract the SQLState
055: * @return The error code.
056: */
057: public static int extractErrorCode(SQLException sqlException) {
058: int errorCode = sqlException.getErrorCode();
059: SQLException nested = sqlException.getNextException();
060: while (errorCode == 0 && nested != null) {
061: errorCode = nested.getErrorCode();
062: nested = nested.getNextException();
063: }
064: return errorCode;
065: }
066:
067: /**
068: * For the given SQLException, locates the X/Open-compliant SQLState.
069: *
070: * @param sqlException The exception from which to extract the SQLState
071: * @return The SQLState code, or null.
072: */
073: public static String extractSqlState(SQLException sqlException) {
074: String sqlState = sqlException.getSQLState();
075: SQLException nested = sqlException.getNextException();
076: while (sqlState == null && nested != null) {
077: sqlState = nested.getSQLState();
078: nested = nested.getNextException();
079: }
080: return sqlState;
081: }
082:
083: /**
084: * For the given SQLException, locates the X/Open-compliant SQLState's class code.
085: *
086: * @param sqlException The exception from which to extract the SQLState class code
087: * @return The SQLState class code, or null.
088: */
089: public static String extractSqlStateClassCode(
090: SQLException sqlException) {
091: return determineSqlStateClassCode(extractSqlState(sqlException));
092: }
093:
094: public static String determineSqlStateClassCode(String sqlState) {
095: if (sqlState == null || sqlState.length() < 2) {
096: return sqlState;
097: }
098: return sqlState.substring(0, 2);
099: }
100: }
|