|
39 | 39 | import org.hibernate.engine.jdbc.env.spi.NameQualifierSupport; |
40 | 40 | import org.hibernate.engine.spi.SessionFactoryImplementor; |
41 | 41 | import org.hibernate.exception.ConstraintViolationException; |
| 42 | +import org.hibernate.exception.ConstraintViolationException.ConstraintKind; |
42 | 43 | import org.hibernate.exception.LockAcquisitionException; |
43 | 44 | import org.hibernate.exception.LockTimeoutException; |
44 | 45 | import org.hibernate.exception.SQLGrammarException; |
45 | 46 | import org.hibernate.exception.spi.SQLExceptionConversionDelegate; |
46 | | -import org.hibernate.internal.util.JdbcExceptionHelper; |
47 | 47 | import org.hibernate.mapping.Table; |
48 | 48 | import org.hibernate.metamodel.mapping.EntityMappingType; |
49 | 49 | import org.hibernate.metamodel.spi.RuntimeModelCreationContext; |
|
126 | 126 | import java.util.regex.Pattern; |
127 | 127 |
|
128 | 128 | import static org.hibernate.dialect.HANAServerConfiguration.MAX_LOB_PREFETCH_SIZE_DEFAULT_VALUE; |
| 129 | +import static org.hibernate.internal.util.JdbcExceptionHelper.extractErrorCode; |
129 | 130 | import static org.hibernate.query.sqm.produce.function.FunctionParameterType.ANY; |
130 | 131 | import static org.hibernate.type.SqlTypes.BINARY; |
131 | 132 | import static org.hibernate.type.SqlTypes.BOOLEAN; |
@@ -587,59 +588,49 @@ public String extractPattern(TemporalUnit unit) { |
587 | 588 |
|
588 | 589 | @Override |
589 | 590 | public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() { |
590 | | - return (sqlException, message, sql) -> { |
591 | | - final int errorCode = JdbcExceptionHelper.extractErrorCode( sqlException ); |
592 | | - |
593 | | - if ( errorCode == 131 ) { |
594 | | - // 131 - Transaction rolled back by lock wait timeout |
595 | | - return new LockTimeoutException( message, sqlException, sql ); |
596 | | - } |
597 | | - |
598 | | - if ( errorCode == 146 ) { |
599 | | - // 146 - Resource busy and acquire with NOWAIT specified |
600 | | - return new LockTimeoutException( message, sqlException, sql ); |
601 | | - } |
602 | | - |
603 | | - if ( errorCode == 132 ) { |
604 | | - // 132 - Transaction rolled back due to unavailable resource |
605 | | - return new LockAcquisitionException( message, sqlException, sql ); |
606 | | - } |
607 | | - |
608 | | - if ( errorCode == 133 ) { |
609 | | - // 133 - Transaction rolled back by detected deadlock |
610 | | - return new LockAcquisitionException( message, sqlException, sql ); |
611 | | - } |
612 | | - |
613 | | - // 259 - Invalid table name |
614 | | - // 260 - Invalid column name |
615 | | - // 261 - Invalid index name |
616 | | - // 262 - Invalid query name |
617 | | - // 263 - Invalid alias name |
618 | | - if ( errorCode == 257 || ( errorCode >= 259 && errorCode <= 263 ) ) { |
619 | | - return new SQLGrammarException( message, sqlException, sql ); |
620 | | - } |
621 | | - |
622 | | - // 257 - Cannot insert NULL or update to NULL |
623 | | - // 301 - Unique constraint violated |
624 | | - // 461 - foreign key constraint violation |
625 | | - // 462 - failed on update or delete by foreign key constraint violation |
626 | | - if ( errorCode == 287 || errorCode == 301 || errorCode == 461 || errorCode == 462 ) { |
627 | | - final String constraintName = getViolatedConstraintNameExtractor() |
628 | | - .extractConstraintName( sqlException ); |
629 | | - |
630 | | - return new ConstraintViolationException( |
631 | | - message, |
632 | | - sqlException, |
633 | | - sql, |
634 | | - errorCode == 301 |
635 | | - ? ConstraintViolationException.ConstraintKind.UNIQUE |
636 | | - : ConstraintViolationException.ConstraintKind.OTHER, |
637 | | - constraintName |
638 | | - ); |
639 | | - } |
640 | | - |
641 | | - return null; |
642 | | - }; |
| 591 | + return (sqlException, message, sql) -> |
| 592 | + switch ( extractErrorCode( sqlException ) ) { |
| 593 | + case 131 -> |
| 594 | + // 131 - Transaction rolled back by lock wait timeout |
| 595 | + new LockTimeoutException( message, sqlException, sql ); |
| 596 | + case 146 -> |
| 597 | + // 146 - Resource busy and acquire with NOWAIT specified |
| 598 | + new LockTimeoutException( message, sqlException, sql ); |
| 599 | + case 132 -> |
| 600 | + // 132 - Transaction rolled back due to unavailable resource |
| 601 | + new LockAcquisitionException( message, sqlException, sql ); |
| 602 | + case 133 -> |
| 603 | + // 133 - Transaction rolled back by detected deadlock |
| 604 | + new LockAcquisitionException( message, sqlException, sql ); |
| 605 | + case 257, 259, 260, 261, 262, 263 -> |
| 606 | + // 259 - Invalid table name |
| 607 | + // 260 - Invalid column name |
| 608 | + // 261 - Invalid index name |
| 609 | + // 262 - Invalid query name |
| 610 | + // 263 - Invalid alias name |
| 611 | + new SQLGrammarException( message, sqlException, sql ); |
| 612 | + case 301 -> |
| 613 | + // 301 - Unique constraint violated |
| 614 | + new ConstraintViolationException( message, sqlException, sql, ConstraintKind.UNIQUE, |
| 615 | + getViolatedConstraintNameExtractor().extractConstraintName( sqlException ) ); |
| 616 | + case 287 -> |
| 617 | + // 287 - Cannot insert NULL or update to NULL |
| 618 | + new ConstraintViolationException( message, sqlException, sql, ConstraintKind.NOT_NULL, |
| 619 | + getViolatedConstraintNameExtractor().extractConstraintName( sqlException ) ); |
| 620 | + case 461, 462 -> |
| 621 | + // 257 - Cannot insert NULL or update to NULL |
| 622 | + // 301 - Unique constraint violated |
| 623 | + // 461 - foreign key constraint violation |
| 624 | + // 462 - failed on update or delete by foreign key constraint violation |
| 625 | + new ConstraintViolationException( message, sqlException, sql, ConstraintKind.FOREIGN_KEY, |
| 626 | + getViolatedConstraintNameExtractor().extractConstraintName( sqlException ) ); |
| 627 | + case 677 -> |
| 628 | + // 677 - Check constraint violation |
| 629 | + new ConstraintViolationException( message, sqlException, sql, ConstraintKind.CHECK, |
| 630 | + getViolatedConstraintNameExtractor().extractConstraintName( sqlException ) ); |
| 631 | + |
| 632 | + default -> null; |
| 633 | + }; |
643 | 634 | } |
644 | 635 |
|
645 | 636 | @Override |
|
0 commit comments