From eb66fed9769cf7c70b0ab4eb7478543f53c43fd1 Mon Sep 17 00:00:00 2001 From: Bryan Varner Date: Tue, 17 Apr 2012 13:40:48 -0400 Subject: [PATCH] HHH-7251 PostgreSQL dialects now convert proper HibernateExceptions for Deadlock / lock timeout (not available with NOWAIT query). --- .../dialect/PostgreSQL81Dialect.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQL81Dialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQL81Dialect.java index 0d1102a9c8..42f8cf898d 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQL81Dialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQL81Dialect.java @@ -28,13 +28,18 @@ import java.sql.SQLException; import java.sql.Types; +import org.hibernate.JDBCException; import org.hibernate.LockOptions; +import org.hibernate.PessimisticLockException; import org.hibernate.cfg.Environment; import org.hibernate.dialect.function.NoArgSQLFunction; import org.hibernate.dialect.function.PositionSubstringFunction; import org.hibernate.dialect.function.SQLFunctionTemplate; import org.hibernate.dialect.function.StandardSQLFunction; import org.hibernate.dialect.function.VarArgsSQLFunction; +import org.hibernate.exception.LockAcquisitionException; +import org.hibernate.exception.internal.SQLStateConversionDelegate; +import org.hibernate.exception.spi.SQLExceptionConversionDelegate; import org.hibernate.exception.spi.TemplatedViolatedConstraintNameExtracter; import org.hibernate.exception.spi.ViolatedConstraintNameExtracter; import org.hibernate.id.SequenceGenerator; @@ -367,6 +372,35 @@ public String extractConstraintName(SQLException sqle) { } }; + @Override + public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() { + SQLExceptionConversionDelegate delegate = super.buildSQLExceptionConversionDelegate(); + if (delegate == null) { + delegate = new SQLStateConversionDelegate(this) { + + @Override + public JDBCException convert(SQLException sqlException, String message, String sql) { + JDBCException exception = super.convert(sqlException, message, sql); + + if (exception == null) { + String sqlState = JdbcExceptionHelper.extractSqlState(sqlException); + + if ("40P01".equals(sqlState)) { // DEADLOCK DETECTED + exception = new LockAcquisitionException(message, sqlException, sql); + } + + if ("55P03".equals(sqlState)) { // LOCK NOT AVAILABLE + exception = new PessimisticLockException(message, sqlException, sql); + } + } + + return exception; + } + }; + } + return delegate; + } + public int registerResultSetOutParameter(CallableStatement statement, int col) throws SQLException { // Register the type of the out param - PostgreSQL uses Types.OTHER statement.registerOutParameter(col++, Types.OTHER);