下面列出了怎么用org.hibernate.exception.JDBCConnectionException的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
return new SQLExceptionConversionDelegate() {
@Override
public JDBCException convert(SQLException sqlException, String message, String sql) {
final int errorCode = JdbcExceptionHelper.extractErrorCode(sqlException) & 0xFF;
if (errorCode == SQLITE_TOOBIG || errorCode == SQLITE_MISMATCH) {
return new DataException(message, sqlException, sql);
} else if (errorCode == SQLITE_BUSY || errorCode == SQLITE_LOCKED) {
return new LockAcquisitionException(message, sqlException, sql);
} else if ((errorCode >= SQLITE_IOERR && errorCode <= SQLITE_PROTOCOL) || errorCode == SQLITE_NOTADB) {
return new JDBCConnectionException(message, sqlException, sql);
}
// returning null allows other delegates to operate
return null;
}
};
}
public List execQuery(String queryString) {
List result = null;
Transaction tx = null;
try {
Session session = _factory.getCurrentSession();
tx = session.beginTransaction();
Query query = session.createQuery(queryString);
if (query != null) result = query.list();
}
catch (JDBCConnectionException jce) {
_log.error("Caught Exception: Couldn't connect to datasource - " +
"starting with an empty dataset");
}
catch (HibernateException he) {
_log.error("Caught Exception: Error executing query: " + queryString, he);
if (tx != null) tx.rollback();
}
return result;
}
/**
* executes a Query object based on the hql string passed
* @param queryString - the hibernate query to execute
* @return the List of objects returned, or null if the query has some problem
*/
public List execQuery(String queryString) {
List result = null;
Transaction tx = null;
try {
tx = getOrBeginTransaction();
Query query = getSession().createQuery(queryString);
if (query != null) result = query.list();
}
catch (JDBCConnectionException jce) {
_log.error("Caught Exception: Couldn't connect to datasource - " +
"continuing with an empty dataset");
if (tx != null) tx.rollback();
}
catch (HibernateException he) {
_log.error("Caught Exception: Error executing query: " + queryString, he);
if (tx != null) tx.rollback();
}
return result;
}
/**
* executes a plain SQL Query
* @param queryString - the SQL query to execute
* @return the List of objects returned, or null if the query has some problem
*/
public List execSQLQuery(String queryString) {
List result = null;
Transaction tx = null;
try {
tx = getOrBeginTransaction();
Query query = getSession().createSQLQuery(queryString);
if (query != null) result = query.list();
commit();
}
catch (JDBCConnectionException jce) {
_log.error("Caught Exception: Couldn't connect to datasource - " +
"starting with an empty dataset");
if (tx != null) tx.rollback();
}
catch (HibernateException he) {
_log.error("Caught Exception: Error executing query: " + queryString, he);
rollback();
}
return result;
}
public int execUpdate(String queryString, boolean commit) {
int result = -1;
Transaction tx = null;
try {
tx = getOrBeginTransaction();
result = getSession().createQuery(queryString).executeUpdate();
if (commit) commit();
}
catch (JDBCConnectionException jce) {
_log.error("Caught Exception: Couldn't connect to datasource - " +
"starting with an empty dataset");
}
catch (HibernateException he) {
_log.error("Caught Exception: Error executing query: " + queryString, he);
if (tx != null) tx.rollback();
}
return result;
}
/**
* Returns true if the exception indicates that there is a problem connecting
* to the database as opposed to with the SQL.
*
* @param e
* @return
*/
private boolean shouldKeepTryingBecauseConnectionException(HibernateException e) {
// Need to know if it is a problem with the database not
// being accessible or if there is a problem with the SQL/data.
// If there is a problem accessibility of the database then
// want to keep trying writing the old data. But if it is
// a problem with the SQL/data then only want to try to write
// the good data from the batch a single time to make sure
// all good data is written.
// From javadocs for for org.hivernate.exception at
// http://docs.jboss.org/hibernate/orm/3.5/javadocs/org/hibernate/exception/package-frame.html
// can see that there are a couple of different exception types.
// From looking at documentation and testing found out that
// bad SQL is indicated by
// ConstraintViolationException
// DataException
// SQLGrammarException
// Appears that for bad connection could get:
// JDBCConnectionException (was not able to verify experimentally)
// GenericJDBCException (obtained when committing transaction with db turned off)
// So if exception is JDBCConnectionException or JDBCGenericException
// then should keep retrying until successful.
boolean keepTryingTillSuccessfull = e instanceof JDBCConnectionException ||
e instanceof GenericJDBCException;
return keepTryingTillSuccessfull;
}
@Override
public SQLExceptionConverter buildSQLExceptionConverter() {
return new SQLExceptionConverter() {
@Override
public JDBCException convert(SQLException sqlException, String message, String sql) {
final int errorCode = sqlException.getErrorCode();
if (errorCode == SQLITE_CONSTRAINT) {
final String constraintName = EXTRACTER.extractConstraintName(sqlException);
return new ConstraintViolationException(message, sqlException, sql, constraintName);
} else if (errorCode == SQLITE_TOOBIG || errorCode == SQLITE_MISMATCH) {
return new DataException(message, sqlException, sql);
} else if (errorCode == SQLITE_BUSY || errorCode == SQLITE_LOCKED) {
return new LockAcquisitionException(message, sqlException, sql);
} else if ((errorCode >= SQLITE_IOERR && errorCode <= SQLITE_PROTOCOL) || errorCode == SQLITE_NOTADB) {
return new JDBCConnectionException(message, sqlException, sql);
}
return new GenericJDBCException(message, sqlException, sql);
}
};
}
@Override
public JDBCException convert(SQLException sqlException, String message, String sql) {
if ( SQLClientInfoException.class.isInstance( sqlException )
|| SQLInvalidAuthorizationSpecException.class.isInstance( sqlException )
|| SQLNonTransientConnectionException.class.isInstance( sqlException )
|| SQLTransientConnectionException.class.isInstance( sqlException ) ) {
return new JDBCConnectionException( message, sqlException, sql );
}
else if ( DataTruncation.class.isInstance( sqlException ) ||
SQLDataException.class.isInstance( sqlException ) ) {
throw new DataException( message, sqlException, sql );
}
else if ( SQLIntegrityConstraintViolationException.class.isInstance( sqlException ) ) {
return new ConstraintViolationException(
message,
sqlException,
sql,
getConversionContext().getViolatedConstraintNameExtracter().extractConstraintName( sqlException )
);
}
else if ( SQLSyntaxErrorException.class.isInstance( sqlException ) ) {
return new SQLGrammarException( message, sqlException, sql );
}
else if ( SQLTimeoutException.class.isInstance( sqlException ) ) {
return new QueryTimeoutException( message, sqlException, sql );
}
else if ( SQLTransactionRollbackException.class.isInstance( sqlException ) ) {
// Not 100% sure this is completely accurate. The JavaDocs for SQLTransactionRollbackException state that
// it indicates sql states starting with '40' and that those usually indicate that:
// <quote>
// the current statement was automatically rolled back by the database because of deadlock or
// other transaction serialization failures.
// </quote>
return new LockAcquisitionException( message, sqlException, sql );
}
return null; // allow other delegates the chance to look
}
@Override
public JDBCException convert(SQLException sqlException, String message, String sql) {
final String sqlState = JdbcExceptionHelper.extractSqlState( sqlException );
final int errorCode = JdbcExceptionHelper.extractErrorCode( 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 ) ) {
final String constraintName = getConversionContext()
.getViolatedConstraintNameExtracter()
.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 ( "40XL1".equals( sqlState ) || "40XL2".equals( sqlState )) {
// Derby "A lock could not be obtained within the time requested."
return new PessimisticLockException( message, sqlException, sql );
}
// MySQL Query execution was interrupted
if ( "70100".equals( sqlState ) ||
// Oracle user requested cancel of current operation
( "72000".equals( sqlState ) && errorCode == 1013 ) ) {
throw new QueryTimeoutException( message, sqlException, sql );
}
}
return null;
}