2121import org .checkerframework .checker .nullness .qual .Nullable ;
2222import org .hibernate .LockMode ;
2323import org .hibernate .LockOptions ;
24- import org .hibernate .PessimisticLockException ;
2524import org .hibernate .QueryTimeoutException ;
2625import org .hibernate .boot .model .FunctionContributions ;
2726import org .hibernate .boot .model .TypeContributions ;
4342import org .hibernate .engine .jdbc .env .spi .NameQualifierSupport ;
4443import org .hibernate .engine .spi .SessionFactoryImplementor ;
4544import org .hibernate .exception .LockAcquisitionException ;
45+ import org .hibernate .exception .LockTimeoutException ;
4646import org .hibernate .exception .TransactionSerializationException ;
4747import org .hibernate .exception .spi .SQLExceptionConversionDelegate ;
4848import org .hibernate .exception .spi .TemplatedViolatedConstraintNameExtractor ;
@@ -1063,21 +1063,13 @@ public ViolatedConstraintNameExtractor getViolatedConstraintNameExtractor() {
10631063 private static final ViolatedConstraintNameExtractor EXTRACTOR =
10641064 new TemplatedViolatedConstraintNameExtractor ( sqle -> {
10651065 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 () );
10811073 default -> null ;
10821074 };
10831075 } );
@@ -1086,19 +1078,22 @@ public ViolatedConstraintNameExtractor getViolatedConstraintNameExtractor() {
10861078 public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate () {
10871079 return (sqlException , message , sql ) -> {
10881080 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 );
11021097 // returning null allows other delegates to operate
11031098 default -> null ;
11041099 };
0 commit comments