Save This Page
Home » Hibernate-3.3.2.GA » org.hibernate » exception » [javadoc | source]
    1   /*
    2    * Hibernate, Relational Persistence for Idiomatic Java
    3    *
    4    * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
    5    * indicated by the @author tags or express copyright attribution
    6    * statements applied by the authors.  All third-party contributions are
    7    * distributed under license by Red Hat Middleware LLC.
    8    *
    9    * This copyrighted material is made available to anyone wishing to use, modify,
   10    * copy, or redistribute it subject to the terms and conditions of the GNU
   11    * Lesser General Public License, as published by the Free Software Foundation.
   12    *
   13    * This program is distributed in the hope that it will be useful,
   14    * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   15    * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
   16    * for more details.
   17    *
   18    * You should have received a copy of the GNU Lesser General Public License
   19    * along with this distribution; if not, write to:
   20    * Free Software Foundation, Inc.
   21    * 51 Franklin Street, Fifth Floor
   22    * Boston, MA  02110-1301  USA
   23    *
   24    */
   25   package org.hibernate.exception;
   26   
   27   import org.hibernate.JDBCException;
   28   import org.hibernate.util.JDBCExceptionReporter;
   29   
   30   import java.sql.SQLException;
   31   
   32   /**
   33    * Implementation of JDBCExceptionHelper.
   34    *
   35    * @author Steve Ebersole
   36    */
   37   public final class JDBCExceptionHelper {
   38   
   39   	private JDBCExceptionHelper() {
   40   	}
   41   
   42   	/**
   43   	 * Converts the given SQLException into Hibernate's JDBCException hierarchy, as well as performing
   44   	 * appropriate logging.
   45   	 *
   46   	 * @param converter    The converter to use.
   47   	 * @param sqlException The exception to convert.
   48   	 * @param message      An optional error message.
   49   	 * @return The converted JDBCException.
   50   	 */
   51   	public static JDBCException convert(SQLExceptionConverter converter, SQLException sqlException, String message) {
   52   		return convert( converter, sqlException, message, "???" );
   53   	}
   54   
   55   	/**
   56   	 * Converts the given SQLException into Hibernate's JDBCException hierarchy, as well as performing
   57   	 * appropriate logging.
   58   	 *
   59   	 * @param converter    The converter to use.
   60   	 * @param sqlException The exception to convert.
   61   	 * @param message      An optional error message.
   62   	 * @return The converted JDBCException.
   63   	 */
   64   	public static JDBCException convert(SQLExceptionConverter converter, SQLException sqlException, String message, String sql) {
   65   		JDBCExceptionReporter.logExceptions( sqlException, message + " [" + sql + "]" );
   66   		return converter.convert( sqlException, message, sql );
   67   	}
   68   
   69   	/**
   70   	 * For the given SQLException, locates the vendor-specific error code.
   71   	 *
   72   	 * @param sqlException The exception from which to extract the SQLState
   73   	 * @return The error code.
   74   	 */
   75   	public static int extractErrorCode(SQLException sqlException) {
   76   		int errorCode = sqlException.getErrorCode();
   77   		SQLException nested = sqlException.getNextException();
   78   		while ( errorCode == 0 && nested != null ) {
   79   			errorCode = nested.getErrorCode();
   80   			nested = nested.getNextException();
   81   		}
   82   		return errorCode;
   83   	}
   84   
   85   	/**
   86   	 * For the given SQLException, locates the X/Open-compliant SQLState.
   87   	 *
   88   	 * @param sqlException The exception from which to extract the SQLState
   89   	 * @return The SQLState code, or null.
   90   	 */
   91   	public static String extractSqlState(SQLException sqlException) {
   92   		String sqlState = sqlException.getSQLState();
   93   		SQLException nested = sqlException.getNextException();
   94   		while ( sqlState == null && nested != null ) {
   95   			sqlState = nested.getSQLState();
   96   			nested = nested.getNextException();
   97   		}
   98   		return sqlState;
   99   	}
  100   
  101   	/**
  102   	 * For the given SQLException, locates the X/Open-compliant SQLState's class code.
  103   	 *
  104   	 * @param sqlException The exception from which to extract the SQLState class code
  105   	 * @return The SQLState class code, or null.
  106   	 */
  107   	public static String extractSqlStateClassCode(SQLException sqlException) {
  108   		return determineSqlStateClassCode( extractSqlState( sqlException ) );
  109   	}
  110   
  111   	public static String determineSqlStateClassCode(String sqlState) {
  112   		if ( sqlState == null || sqlState.length() < 2 ) {
  113   			return sqlState;
  114   		}
  115   		return sqlState.substring( 0, 2 );
  116   	}
  117   }

Save This Page
Home » Hibernate-3.3.2.GA » org.hibernate » exception » [javadoc | source]