21
21
import org .checkerframework .checker .nullness .qual .Nullable ;
22
22
import org .hibernate .LockMode ;
23
23
import org .hibernate .LockOptions ;
24
- import org .hibernate .PessimisticLockException ;
25
24
import org .hibernate .QueryTimeoutException ;
26
25
import org .hibernate .boot .model .FunctionContributions ;
27
26
import org .hibernate .boot .model .TypeContributions ;
43
42
import org .hibernate .engine .jdbc .env .spi .NameQualifierSupport ;
44
43
import org .hibernate .engine .spi .SessionFactoryImplementor ;
45
44
import org .hibernate .exception .LockAcquisitionException ;
45
+ import org .hibernate .exception .LockTimeoutException ;
46
46
import org .hibernate .exception .TransactionSerializationException ;
47
47
import org .hibernate .exception .spi .SQLExceptionConversionDelegate ;
48
48
import org .hibernate .exception .spi .TemplatedViolatedConstraintNameExtractor ;
@@ -1063,21 +1063,13 @@ public ViolatedConstraintNameExtractor getViolatedConstraintNameExtractor() {
1063
1063
private static final ViolatedConstraintNameExtractor EXTRACTOR =
1064
1064
new TemplatedViolatedConstraintNameExtractor ( sqle -> {
1065
1065
final String sqlState = extractSqlState ( sqle );
1066
- if ( sqlState == null ) {
1067
- return null ;
1068
- }
1069
- return switch ( parseInt ( sqlState ) ) {
1070
- // CHECK VIOLATION
1071
- case 23514 -> extractUsingTemplate ( "violates check constraint \" " , "\" " , sqle .getMessage () );
1072
- // UNIQUE VIOLATION
1073
- case 23505 -> extractUsingTemplate (" violates unique constraint \" " , "\" " , sqle .getMessage () );
1074
- // FOREIGN KEY VIOLATION
1075
- case 23503 -> extractUsingTemplate ( "violates foreign key constraint \" " , "\" " , sqle .getMessage () );
1076
- // NOT NULL VIOLATION
1077
- case 23502 -> extractUsingTemplate ( "null value in column \" " , "\" violates not-null constraint" , sqle .getMessage () );
1078
- // TODO: RESTRICT VIOLATION
1079
- case 23001 -> null ;
1080
- // ALL OTHER
1066
+ return sqlState == null ? null : switch ( parseInt ( sqlState ) ) {
1067
+ case 23505 , 23514 , 23503 ->
1068
+ // UNIQUE, CHECK, OR FOREIGN KEY VIOLATION
1069
+ extractUsingTemplate ( "constraint \" " , "\" " , sqle .getMessage () );
1070
+ case 23502 ->
1071
+ // NOT NULL VIOLATION
1072
+ extractUsingTemplate ( "column \" " , "\" " , sqle .getMessage () );
1081
1073
default -> null ;
1082
1074
};
1083
1075
} );
@@ -1086,19 +1078,22 @@ public ViolatedConstraintNameExtractor getViolatedConstraintNameExtractor() {
1086
1078
public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate () {
1087
1079
return (sqlException , message , sql ) -> {
1088
1080
final String sqlState = extractSqlState ( sqlException );
1089
- if ( sqlState == null ) {
1090
- return null ;
1091
- }
1092
- return switch (sqlState ) {
1093
- // Serialization Exception
1094
- case "40001" -> message .contains ("WriteTooOldError" )
1095
- ? new TransactionSerializationException ( message , sqlException , sql )
1096
- : null ;
1097
- // DEADLOCK DETECTED
1098
- case "40P01" -> new LockAcquisitionException ( message , sqlException , sql );
1099
- // LOCK NOT AVAILABLE
1100
- case "55P03" -> new PessimisticLockException ( message , sqlException , sql );
1101
- case "57014" -> new QueryTimeoutException ( message , sqlException , sql );
1081
+ return sqlState == null ? null : switch ( sqlState ) {
1082
+ case "40001" ->
1083
+ message .contains ( "WriteTooOldError" ) // Serialization Exception
1084
+ ? new TransactionSerializationException ( message , sqlException , sql )
1085
+ : null ;
1086
+ case "40P01" ->
1087
+ // DEADLOCK DETECTED
1088
+ new LockAcquisitionException ( message , sqlException , sql );
1089
+ case "55P03" ->
1090
+ // LOCK NOT AVAILABLE
1091
+ //TODO: should we check that the message is "canceling statement due to lock timeout"
1092
+ // and return LockAcquisitionException if it is not?
1093
+ new LockTimeoutException ( message , sqlException , sql );
1094
+ case "57014" ->
1095
+ // QUERY CANCELLED
1096
+ new QueryTimeoutException ( message , sqlException , sql );
1102
1097
// returning null allows other delegates to operate
1103
1098
default -> null ;
1104
1099
};
0 commit comments