下面列出了org.hibernate.NonUniqueObjectException#org.hibernate.JDBCException 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
default CompletionStage<List<Object>> reactiveProcessResultSet(
ResultSet rs,
QueryParameters queryParameters,
SharedSessionContractImplementor session,
boolean returnProxies,
ResultTransformer forcedResultTransformer,
List<AfterLoadAction> afterLoadActions) {
try {
return getReactiveResultSetProcessor()
.reactiveExtractResults(
rs,
session,
queryParameters,
null,
returnProxies,
queryParameters.isReadOnly( session ),
forcedResultTransformer,
afterLoadActions
);
}
catch (SQLException sqle) {
//don't log or convert it - just pass it on to the caller
throw new JDBCException( "could not load batch", sqle );
}
}
public void testUpdateFailureWithExceptionChecking() {
Session s = openSession();
s.beginTransaction();
ExceptionCheckingEntity e = new ExceptionCheckingEntity();
e.setId( new Long( 1 ) );
e.setName( "dummy" );
s.update( e );
try {
s.flush();
fail( "expection flush failure!" );
}
catch( JDBCException ex ) {
// these should specifically be JDBCExceptions...
}
s.clear();
s.getTransaction().commit();
s.close();
}
@Override
public SQLExceptionConverter buildSQLExceptionConverter() {
return new SQLExceptionConverter() {
@Override
public JDBCException convert(SQLException sqlException, String message,
String sql) {
final String sqlState = JDBCExceptionHelper
.extractSqlState(sqlException);
if (sqlState != null) {
if (SQL_GRAMMAR_CATEGORIES.contains(sqlState)) {
return new SQLGrammarException(message, sqlException, sql);
}
else if (DATA_CATEGORIES.contains(sqlState)) {
return new DataException(message, sqlException, sql);
}
else if (LOCK_ACQUISITION_CATEGORIES.contains(sqlState)) {
return new LockAcquisitionException(message, sqlException, sql);
}
}
return null;
}
};
}
@Override
public SQLExceptionConverter buildSQLExceptionConverter() {
return new SQLExceptionConverter() {
@Override
public JDBCException convert(SQLException sqlException,
String message, String sql) {
final String sqlState = JdbcExceptionHelper
.extractSqlState(sqlException);
if (sqlState != null) {
if (SQL_GRAMMAR_CATEGORIES.contains(sqlState)) {
return new SQLGrammarException(message, sqlException,
sql);
} else if (DATA_CATEGORIES.contains(sqlState)) {
return new DataException(message, sqlException, sql);
} else if (LOCK_ACQUISITION_CATEGORIES.contains(sqlState)) {
return new LockAcquisitionException(message,
sqlException, sql);
}
}
return null;
}
};
}
/**
* 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; returns null if it could not be converted.
*/
@Override
public JDBCException convert(SQLException sqlException, String message, String sql) {
String sqlStateClassCode = JdbcExceptionHelper.extractSqlStateClassCode( sqlException );
if ( sqlStateClassCode != null ) {
Integer errorCode = JdbcExceptionHelper.extractErrorCode( sqlException );
if ( INTEGRITY_VIOLATION_CATEGORIES.contains( errorCode ) ) {
String constraintName =
getConversionContext()
.getViolatedConstraintNameExtracter()
.extractConstraintName( sqlException );
return new ConstraintViolationException( message, sqlException, sql, constraintName );
}
else if ( DATA_CATEGORIES.contains( sqlStateClassCode ) ) {
return new DataException( message, sqlException, sql );
}
}
return null; // allow other delegates the chance to look
}
public void testBadInsertionFails() {
Session session = openSession();
session.beginTransaction();
Organization org = new Organization( "hola!" );
try {
session.save( org );
session.delete( org );
fail( "expecting bad custom insert statement to fail" );
}
catch( JDBCException e ) {
// expected failure
}
session.getTransaction().rollback();
session.close();
}
@Override
public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
return new SQLExceptionConversionDelegate() {
@Override
public JDBCException convert(SQLException sqlException, String message, String sql) {
final String sqlState = JdbcExceptionHelper.extractSqlState( sqlException );
final int errorCode = JdbcExceptionHelper.extractErrorCode( sqlException );
if("JZ0TO".equals( sqlState ) || "JZ006".equals( sqlState )){
throw new LockTimeoutException( message, sqlException, sql );
}
if ( 515 == errorCode && "ZZZZZ".equals( sqlState ) ) {
// Attempt to insert NULL value into column; column does not allow nulls.
final String constraintName = getViolatedConstraintNameExtracter().extractConstraintName( sqlException );
return new ConstraintViolationException( message, sqlException, sql, constraintName );
}
return null;
}
};
}
/**
* 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.
*/
public JDBCException convert(SQLException sqlException, String message, String sql) {
String sqlStateClassCode = JDBCExceptionHelper.extractSqlStateClassCode( sqlException );
Integer errorCode = new Integer( JDBCExceptionHelper.extractErrorCode( sqlException ) );
if ( sqlStateClassCode != null ) {
if ( SQL_GRAMMAR_CATEGORIES.contains( sqlStateClassCode ) ) {
return new SQLGrammarException( message, sqlException, sql );
}
else if ( INTEGRITY_VIOLATION_CATEGORIES.contains( errorCode ) ) {
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 );
}
}
return handledNonSpecificException( sqlException, message, sql );
}
@Override
public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
return new SQLExceptionConversionDelegate() {
@Override
public JDBCException convert(SQLException sqlException, String message, String sql) {
final String sqlState = JdbcExceptionHelper.extractSqlState( sqlException );
final int errorCode = JdbcExceptionHelper.extractErrorCode( sqlException );
if ( "HY008".equals( sqlState ) ) {
throw new QueryTimeoutException( message, sqlException, sql );
}
if (1222 == errorCode ) {
throw new LockTimeoutException( message, sqlException, sql );
}
return null;
}
};
}
@Override
public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
return new SQLExceptionConversionDelegate() {
@Override
public JDBCException convert(SQLException sqlException, String message, String sql) {
final String sqlState = JdbcExceptionHelper.extractSqlState( sqlException );
if ( "40P01".equals( sqlState ) ) {
// DEADLOCK DETECTED
return new LockAcquisitionException( message, sqlException, sql );
}
if ( "55P03".equals( sqlState ) ) {
// LOCK NOT AVAILABLE
return new PessimisticLockException( message, sqlException, sql );
}
// returning null allows other delegates to operate
return null;
}
};
}
@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 SQLExceptionConverter buildSQLExceptionConverter() {
return new SQLExceptionConverter() {
@Override
public JDBCException convert(SQLException sqlException, String message,
String sql) {
final String sqlState = JDBCExceptionHelper
.extractSqlState(sqlException);
if (sqlState != null) {
if (SQL_GRAMMAR_CATEGORIES.contains(sqlState)) {
return new SQLGrammarException(message, sqlException, sql);
}
else if (DATA_CATEGORIES.contains(sqlState)) {
return new DataException(message, sqlException, sql);
}
else if (LOCK_ACQUISITION_CATEGORIES.contains(sqlState)) {
return new LockAcquisitionException(message, sqlException, sql);
}
}
return null;
}
};
}
private void create(boolean script, boolean export, Writer fileOutput, Statement statement)
throws IOException {
for ( int j = 0; j < createSQL.length; j++ ) {
try {
execute( script, export, fileOutput, statement, createSQL[j] );
}
catch ( SQLException e ) {
if ( haltOnError ) {
throw new JDBCException( "Error during DDL export", e );
}
exceptions.add( e );
log.error( "Unsuccessful: " + createSQL[j] );
log.error( e.getMessage() );
}
}
}
public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
return new SQLExceptionConversionDelegate() {
@Override
public JDBCException convert(SQLException sqlException,
String message, String sql) {
final String sqlState = JdbcExceptionHelper
.extractSqlState(sqlException);
if (sqlState != null) {
if (SQL_GRAMMAR_CATEGORIES.contains(sqlState)) {
return new SQLGrammarException(message, sqlException,
sql);
} else if (DATA_CATEGORIES.contains(sqlState)) {
return new DataException(message, sqlException, sql);
} else if (LOCK_ACQUISITION_CATEGORIES.contains(sqlState)) {
return new LockAcquisitionException(message,
sqlException, sql);
}
}
return null;
}
};
}
/**
* Convert the given HibernateException to an appropriate exception from the
* {@code org.springframework.dao} hierarchy.
* <p>Will automatically apply a specified SQLExceptionTranslator to a
* Hibernate JDBCException, otherwise rely on Hibernate's default translation.
* @param ex the HibernateException that occurred
* @return a corresponding DataAccessException
* @see SessionFactoryUtils#convertHibernateAccessException
*/
protected DataAccessException convertHibernateAccessException(HibernateException ex) {
if (this.jdbcExceptionTranslator != null && ex instanceof JDBCException) {
JDBCException jdbcEx = (JDBCException) ex;
DataAccessException dae = this.jdbcExceptionTranslator.translate(
"Hibernate operation: " + jdbcEx.getMessage(), jdbcEx.getSQL(), jdbcEx.getSQLException());
if (dae != null) {
throw dae;
}
}
return SessionFactoryUtils.convertHibernateAccessException(ex);
}
default Object[] processSnapshot(SharedSessionContractImplementor session, ResultSet resultSet) {
try {
if ( resultSet.next() ) {
//return the "hydrated" state (ie. associations are not resolved)
Type[] types = getPropertyTypes();
Object[] values = new Object[types.length];
boolean[] includeProperty = getPropertyUpdateability();
for ( int i = 0; i < types.length; i++ ) {
if ( includeProperty[i] ) {
values[i] = types[i].hydrate(
resultSet,
getPropertyAliases( "", i ),
session,
null
); //null owner ok??
}
}
return values;
}
else {
//no corresponding row: transient!
return null;
}
}
catch (SQLException e) {
//can't actually occur!
throw new JDBCException( "error while binding parameters", e );
}
}
/**
* Convert the given HibernateException to an appropriate exception from the
* {@code org.springframework.dao} hierarchy.
* <p>Will automatically apply a specified SQLExceptionTranslator to a
* Hibernate JDBCException, otherwise rely on Hibernate's default translation.
* @param ex the HibernateException that occurred
* @return a corresponding DataAccessException
* @see SessionFactoryUtils#convertHibernateAccessException
*/
protected DataAccessException convertHibernateAccessException(HibernateException ex) {
if (this.jdbcExceptionTranslator != null && ex instanceof JDBCException) {
JDBCException jdbcEx = (JDBCException) ex;
DataAccessException dae = this.jdbcExceptionTranslator.translate(
"Hibernate operation: " + jdbcEx.getMessage(), jdbcEx.getSQL(), jdbcEx.getSQLException());
if (dae != null) {
throw dae;
}
}
return SessionFactoryUtils.convertHibernateAccessException(ex);
}
protected JDBCException convertSqlException(String message, SQLException e) {
// if JdbcServices#getSqlExceptionHelper is available, use it...
final JdbcServices jdbcServices = serviceRegistry.getService( JdbcServices.class );
if ( jdbcServices != null && jdbcServices.getSqlExceptionHelper() != null ) {
return jdbcServices.getSqlExceptionHelper().convert( e, message, null );
}
// likely we are still in the process of initializing the ServiceRegistry, so use the simplified
// SQLException conversion
return simpleConverterAccess.getValue().convert( e, message, null );
}
@Override
public Blob createBlob(byte[] bytes) {
try {
final Blob blob = createBlob();
blob.setBytes( 1, bytes );
return blob;
}
catch ( SQLException e ) {
throw new JDBCException( "Unable to set BLOB bytes after creation", e );
}
}
@Override
public NClob createNClob(String string) {
try {
final NClob nclob = createNClob();
nclob.setString( 1, string );
return nclob;
}
catch ( SQLException e ) {
throw new JDBCException( "Unable to set NCLOB string after creation", e );
}
}
/**
* Builds a minimal converter. The instance returned here just always converts to
* {@link org.hibernate.exception.GenericJDBCException}.
*
* @return The minimal converter.
*/
public static SQLExceptionConverter buildMinimalSQLExceptionConverter() {
return new SQLExceptionConverter() {
public JDBCException convert(SQLException sqlException, String message, String sql) {
return new GenericJDBCException( message, sqlException, sql );
}
};
}
@Override
public JDBCException convert(SQLException sqlException, String message, String sql) {
for ( SQLExceptionConversionDelegate delegate : delegates ) {
final JDBCException jdbcException = delegate.convert( sqlException, message, sql );
if ( jdbcException != null ) {
return jdbcException;
}
}
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
}
public void close() throws JDBCException {
if (ps!=null) {
try {
log.debug("closing iterator");
nextResult = null;
session.getBatcher().closeQueryStatement(ps, rs);
ps = null;
rs = null;
hasNext = false;
}
catch (SQLException e) {
log.info( "Unable to close iterator", e );
throw JDBCExceptionHelper.convert(
session.getFactory().getSQLExceptionConverter(),
e,
"Unable to close iterator"
);
}
finally {
try {
session.getPersistenceContext().getLoadContexts().cleanup( rs );
}
catch( Throwable ignore ) {
// ignore this error for now
log.trace( "exception trying to cleanup load context : " + ignore.getMessage() );
}
}
}
}
@Override
public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
return new SQLExceptionConversionDelegate() {
@Override
public JDBCException convert(SQLException sqlException, String message, String sql) {
final String sqlState = JdbcExceptionHelper.extractSqlState( sqlException );
final int errorCode = JdbcExceptionHelper.extractErrorCode( sqlException );
if( -952 == errorCode && "57014".equals( sqlState )){
throw new LockTimeoutException( message, sqlException, sql );
}
return null;
}
};
}
/**
* 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.
*/
public JDBCException convert(SQLException sqlException, String message, String sql) {
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 );
}
@Override
public SQLExceptionConverter buildSQLExceptionConverter() {
return new SQLExceptionConverter() {
@Override
public JDBCException convert(SQLException sqlException,
String message, String sql) {
final String sqlState = JdbcExceptionHelper
.extractSqlState(sqlException);
if (sqlState != null) {
if (SQL_GRAMMAR_CATEGORIES.contains(sqlState)) {
return new SQLGrammarException(message, sqlException,
sql);
} else if (DATA_CATEGORIES.contains(sqlState)) {
return new DataException(message, sqlException, sql);
} else if (LOCK_ACQUISITION_CATEGORIES.contains(sqlState)) {
return new LockAcquisitionException(message,
sqlException, sql);
}
}
return null;
}
};
}
public HibernateJdbcException(JDBCException ex) {
super("JDBC exception on Hibernate data access: SQLException for SQL [" + ex.getSQL() + "]; SQL state [" +
ex.getSQLState() + "]; error code [" + ex.getErrorCode() + "]; " + ex.getMessage(), ex);
}
/**
* Return the underlying SQLException.
*/
public SQLException getSQLException() {
return ((JDBCException) getCause()).getSQLException();
}
/**
* Return the SQL that led to the problem.
*/
public String getSql() {
return ((JDBCException) getCause()).getSQL();
}