Skip to content

Commit b7cfe00

Browse files
committed
use LockTimeoutException instead of PessimisticLockException on Postgres and h2
this is perhaps not *perfectly* correct for Postgres, but I believe it's good enough for our purposes (this error code can occur in some other situations, but they are very unlikely to affect us, it seems to me)
1 parent b31505f commit b7cfe00

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

hibernate-core/src/main/java/org/hibernate/dialect/H2Dialect.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import java.util.TimeZone;
1616

1717
import org.checkerframework.checker.nullness.qual.Nullable;
18-
import org.hibernate.PessimisticLockException;
1918
import org.hibernate.QueryTimeoutException;
2019
import org.hibernate.boot.model.FunctionContributions;
2120
import org.hibernate.boot.model.TypeContributions;
@@ -38,6 +37,7 @@
3837
import org.hibernate.exception.ConstraintViolationException;
3938
import org.hibernate.exception.ConstraintViolationException.ConstraintKind;
4039
import org.hibernate.exception.LockAcquisitionException;
40+
import org.hibernate.exception.LockTimeoutException;
4141
import org.hibernate.exception.spi.SQLExceptionConversionDelegate;
4242
import org.hibernate.exception.spi.TemplatedViolatedConstraintNameExtractor;
4343
import org.hibernate.exception.spi.ViolatedConstraintNameExtractor;
@@ -810,7 +810,7 @@ public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
810810
new LockAcquisitionException(message, sqlException, sql);
811811
case 50200 ->
812812
// LOCK NOT AVAILABLE
813-
new PessimisticLockException(message, sqlException, sql);
813+
new LockTimeoutException(message, sqlException, sql);
814814
case 23505 ->
815815
// Unique index or primary key violation
816816
new ConstraintViolationException( message, sqlException, sql, ConstraintKind.UNIQUE,
@@ -828,6 +828,7 @@ public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
828828
new ConstraintViolationException( message, sqlException, sql, ConstraintKind.CHECK,
829829
getViolatedConstraintNameExtractor().extractConstraintName( sqlException ) );
830830
case 57014 ->
831+
// QUERY CANCELLED
831832
new QueryTimeoutException( message, sqlException, sql );
832833
default -> null;
833834
};

hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQLDialect.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.hibernate.Length;
2222
import org.hibernate.LockMode;
2323
import org.hibernate.LockOptions;
24-
import org.hibernate.PessimisticLockException;
2524
import org.hibernate.QueryTimeoutException;
2625
import org.hibernate.boot.model.FunctionContributions;
2726
import org.hibernate.boot.model.TypeContributions;
@@ -46,6 +45,7 @@
4645
import org.hibernate.engine.jdbc.env.spi.NameQualifierSupport;
4746
import org.hibernate.engine.spi.SessionFactoryImplementor;
4847
import org.hibernate.exception.LockAcquisitionException;
48+
import org.hibernate.exception.LockTimeoutException;
4949
import org.hibernate.exception.spi.SQLExceptionConversionDelegate;
5050
import org.hibernate.exception.spi.TemplatedViolatedConstraintNameExtractor;
5151
import org.hibernate.exception.spi.ViolatedConstraintNameExtractor;
@@ -1041,13 +1041,13 @@ public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
10411041
final String sqlState = extractSqlState( sqlException );
10421042
if ( sqlState != null ) {
10431043
switch ( sqlState ) {
1044-
case "40P01":
1045-
// DEADLOCK DETECTED
1044+
case "40P01": // DEADLOCK DETECTED
10461045
return new LockAcquisitionException( message, sqlException, sql );
1047-
case "55P03":
1048-
// LOCK NOT AVAILABLE
1049-
return new PessimisticLockException( message, sqlException, sql );
1050-
case "57014":
1046+
case "55P03": // LOCK NOT AVAILABLE
1047+
//TODO: should we check that the message is "canceling statement due to lock timeout"
1048+
// and return LockAcquisitionException if it is not?
1049+
return new LockTimeoutException( message, sqlException, sql );
1050+
case "57014": // QUERY CANCELLED
10511051
return new QueryTimeoutException( message, sqlException, sql );
10521052
}
10531053
}

0 commit comments

Comments
 (0)