FileDocCategorySizeDatePackage
SQLStateConverter.javaAPI DocHibernate 3.2.53829Mon Mar 06 08:16:28 GMT 2006org.hibernate.exception

SQLStateConverter

public class SQLStateConverter extends Object implements SQLExceptionConverter
A SQLExceptionConverter implementation which performs converion based on the underlying SQLState. Interpretation of a SQL error based on SQLState is not nearly as accurate as using the ErrorCode (which is, however, vendor- specific). Use of a ErrorCode-based converter should be preferred approach for converting/interpreting SQLExceptions.
author
Steve Ebersole

Fields Summary
private ViolatedConstraintNameExtracter
extracter
private static final Set
SQL_GRAMMAR_CATEGORIES
private static final Set
DATA_CATEGORIES
private static final Set
INTEGRITY_VIOLATION_CATEGORIES
private static final Set
CONNECTION_CATEGORIES
Constructors Summary
public SQLStateConverter(ViolatedConstraintNameExtracter extracter)


	 
		SQL_GRAMMAR_CATEGORIES.add( "07" );
		SQL_GRAMMAR_CATEGORIES.add( "37" );
		SQL_GRAMMAR_CATEGORIES.add( "42" );
		SQL_GRAMMAR_CATEGORIES.add( "65" );
		SQL_GRAMMAR_CATEGORIES.add( "S0" );
		SQL_GRAMMAR_CATEGORIES.add( "20" );
		
		DATA_CATEGORIES.add("22");
		DATA_CATEGORIES.add("21");
		DATA_CATEGORIES.add("02");

		INTEGRITY_VIOLATION_CATEGORIES.add( "23" );
		INTEGRITY_VIOLATION_CATEGORIES.add( "27" );
		INTEGRITY_VIOLATION_CATEGORIES.add( "44" );

		CONNECTION_CATEGORIES.add( "08" );
	
		this.extracter = extracter;
	
Methods Summary
public org.hibernate.JDBCExceptionconvert(java.sql.SQLException sqlException, java.lang.String message, java.lang.String sql)
Convert the given SQLException into Hibernate's JDBCException hierarchy.

param
sqlException The SQLException to be converted.
param
message An optional error message.
param
sql Optionally, the sql being performed when the exception occurred.
return
The resulting JDBCException.

		String sqlState = JDBCExceptionHelper.extractSqlState( sqlException );

		if ( sqlState != null ) {
			String sqlStateClassCode = JDBCExceptionHelper.determineSqlStateClassCode( sqlState );

			if ( sqlStateClassCode != null ) {
				if ( SQL_GRAMMAR_CATEGORIES.contains( sqlStateClassCode ) ) {
					return new SQLGrammarException( message, sqlException, sql );
				}
				else if ( INTEGRITY_VIOLATION_CATEGORIES.contains( sqlStateClassCode ) ) {
					String constraintName = extracter.extractConstraintName( sqlException );
					return new ConstraintViolationException( message, sqlException, sql, constraintName );
				}
				else if ( CONNECTION_CATEGORIES.contains( sqlStateClassCode ) ) {
					return new JDBCConnectionException( message, sqlException, sql );
				}
				else if ( DATA_CATEGORIES.contains( sqlStateClassCode ) ) {
					return new DataException( message, sqlException, sql );
				}
			}

			if ( "40001".equals( sqlState ) ) {
				return new LockAcquisitionException( message, sqlException, sql );
			}

			if ( "61000".equals( sqlState ) ) {
				// oracle sql-state code for deadlock
				return new LockAcquisitionException( message, sqlException, sql );
			}
		}

		return handledNonSpecificException( sqlException, message, sql );
	
protected org.hibernate.JDBCExceptionhandledNonSpecificException(java.sql.SQLException sqlException, java.lang.String message, java.lang.String sql)
Handle an exception not converted to a specific type based on the SQLState.

param
sqlException The exception to be handled.
param
message An optional message
param
sql Optionally, the sql being performed when the exception occurred.
return
The converted exception; should never be null.

		return new GenericJDBCException( message, sqlException, sql );