Skip to content

Commit ec75613

Browse files
committed
make LAE extend PLE and clean up SQLStateConversionDelegate
- PessimisticLockException dupes the role of LockAcquisitionException - We should not have database-specific codes on SQLStateConversionDelegate - throw LAE instead of PLE on MySQL and Maria
1 parent c0032b1 commit ec75613

File tree

6 files changed

+29
-41
lines changed

6 files changed

+29
-41
lines changed

hibernate-core/src/main/java/org/hibernate/PessimisticLockException.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@
99
/**
1010
* Thrown when a pessimistic locking conflict occurs.
1111
*
12+
* @apiNote When a conflict is detected while acquiring a database-level lock,
13+
* {@link org.hibernate.exception.LockAcquisitionException} is preferred.
14+
*
1215
* @author Scott Marlow
1316
*
1417
* @see jakarta.persistence.PessimisticLockException
18+
* @see org.hibernate.exception.LockAcquisitionException
1519
*/
1620
public class PessimisticLockException extends JDBCException {
1721
/**
@@ -24,4 +28,14 @@ public class PessimisticLockException extends JDBCException {
2428
public PessimisticLockException(String message, SQLException sqlException, String sql) {
2529
super( message, sqlException, sql );
2630
}
31+
/**
32+
* Constructs a {@code PessimisticLockException} using the specified information.
33+
*
34+
* @param message A message explaining the exception condition
35+
* @param sqlException The underlying SQL exception
36+
*/
37+
public PessimisticLockException(String message, SQLException sqlException) {
38+
super( message, sqlException );
39+
40+
}
2741
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import java.sql.Types;
1010
import java.util.Set;
1111

12-
import org.hibernate.PessimisticLockException;
1312
import org.hibernate.QueryTimeoutException;
1413
import org.hibernate.boot.model.FunctionContributions;
1514
import org.hibernate.boot.model.TypeContributions;
@@ -355,12 +354,11 @@ public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
355354
switch ( sqlException.getErrorCode() ) {
356355
case 1205: // ER_LOCK_WAIT_TIMEOUT
357356
return new LockTimeoutException( message, sqlException, sql );
358-
case 3572: // ER_LOCK_NOWAIT
359-
return new PessimisticLockException( message, sqlException, sql );
360357
case 1020:
361358
// If @@innodb_snapshot_isolation is set (default since 11.6.2),
362359
// and an attempt to acquire a lock on a record that does not exist
363360
// in the current read view is made, error DB_RECORD_CHANGED is raised.
361+
case 3572: // ER_LOCK_NOWAIT
364362
case 1207: // ER_READ_ONLY_TRANSACTION
365363
case 1206: // ER_LOCK_TABLE_FULL
366364
return new LockAcquisitionException( message, sqlException, sql );

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import org.hibernate.Length;
1919
import org.hibernate.LockOptions;
20-
import org.hibernate.PessimisticLockException;
2120
import org.hibernate.QueryTimeoutException;
2221
import org.hibernate.boot.model.FunctionContributions;
2322
import org.hibernate.boot.model.TypeContributions;
@@ -1247,11 +1246,11 @@ public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
12471246
case 1205: // ER_LOCK_WAIT_TIMEOUT
12481247
return new LockTimeoutException( message, sqlException, sql );
12491248
case 3572: // ER_LOCK_NOWAIT
1250-
return new PessimisticLockException( message, sqlException, sql );
12511249
case 1207: // ER_READ_ONLY_TRANSACTION
12521250
case 1206: // ER_LOCK_TABLE_FULL
12531251
return new LockAcquisitionException( message, sqlException, sql );
12541252
case 3024: // ER_QUERY_TIMEOUT
1253+
case 1317: // ER_QUERY_INTERRUPTED
12551254
return new QueryTimeoutException( message, sqlException, sql );
12561255
case 1062:
12571256
// Unique constraint violation

hibernate-core/src/main/java/org/hibernate/exception/LockAcquisitionException.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import java.sql.SQLException;
88

9+
import org.hibernate.PessimisticLockException;
910
import org.hibernate.JDBCException;
1011

1112
/**
@@ -14,7 +15,7 @@
1415
*
1516
* @author Steve Ebersole
1617
*/
17-
public class LockAcquisitionException extends JDBCException {
18+
public class LockAcquisitionException extends PessimisticLockException {
1819
public LockAcquisitionException(String string, SQLException root) {
1920
super( string, root );
2021
}

hibernate-core/src/main/java/org/hibernate/exception/internal/SQLStateConversionDelegate.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
import java.sql.SQLException;
88

99
import org.hibernate.JDBCException;
10-
import org.hibernate.PessimisticLockException;
11-
import org.hibernate.QueryTimeoutException;
1210
import org.hibernate.exception.AuthException;
1311
import org.hibernate.exception.ConstraintViolationException;
1412
import org.hibernate.exception.ConstraintViolationException.ConstraintKind;
@@ -22,7 +20,6 @@
2220
import org.checkerframework.checker.nullness.qual.Nullable;
2321

2422
import static org.hibernate.internal.util.JdbcExceptionHelper.determineSqlStateClassCode;
25-
import static org.hibernate.internal.util.JdbcExceptionHelper.extractErrorCode;
2623
import static org.hibernate.internal.util.JdbcExceptionHelper.extractSqlState;
2724

2825
/**
@@ -53,17 +50,6 @@ public SQLStateConversionDelegate(ConversionContext conversionContext) {
5350
return new AuthException( message, sqlException, sql );
5451
case "40001":
5552
return new LockAcquisitionException( message, sqlException, sql );
56-
case "40XL1", "40XL2":
57-
// Derby "A lock could not be obtained within the time requested."
58-
return new PessimisticLockException( message, sqlException, sql );
59-
case "70100":
60-
// MySQL Query execution was interrupted
61-
return new QueryTimeoutException( message, sqlException, sql );
62-
case "72000":
63-
if ( extractErrorCode( sqlException ) == 1013 ) {
64-
// Oracle user requested cancel of current operation
65-
return new QueryTimeoutException( message, sqlException, sql );
66-
}
6753
}
6854
switch ( determineSqlStateClassCode( sqlState ) ) {
6955
case

hibernate-core/src/main/java/org/hibernate/internal/ExceptionConverterImpl.java

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.hibernate.dialect.lock.PessimisticEntityLockException;
2323
import org.hibernate.engine.spi.ExceptionConverter;
2424
import org.hibernate.engine.spi.SharedSessionContractImplementor;
25-
import org.hibernate.exception.LockAcquisitionException;
2625
import org.hibernate.exception.TransactionSerializationException;
2726
import org.hibernate.loader.MultipleBagFetchException;
2827
import org.hibernate.query.IllegalQueryOperationException;
@@ -81,8 +80,8 @@ public RuntimeException convert(HibernateException exception, LockOptions lockOp
8180
rollbackIfNecessary( converted );
8281
return converted;
8382
}
84-
else if ( exception instanceof LockAcquisitionException lockAcquisitionException ) {
85-
final PersistenceException converted = wrapLockException( lockAcquisitionException, lockOptions );
83+
else if ( exception instanceof org.hibernate.PessimisticLockException pessimisticLockException ) {
84+
final PersistenceException converted = wrapLockException( pessimisticLockException, lockOptions );
8685
rollbackIfNecessary( converted );
8786
return converted;
8887
}
@@ -91,11 +90,6 @@ else if ( exception instanceof LockingStrategyException lockingStrategyException
9190
rollbackIfNecessary( converted );
9291
return converted;
9392
}
94-
else if ( exception instanceof org.hibernate.PessimisticLockException pessimisticLockException ) {
95-
final PersistenceException converted = wrapLockException( pessimisticLockException, lockOptions );
96-
rollbackIfNecessary( converted );
97-
return converted;
98-
}
9993
else if ( exception instanceof org.hibernate.QueryTimeoutException ) {
10094
final QueryTimeoutException converted = new QueryTimeoutException( exception.getMessage(), exception );
10195
rollbackIfNecessary( converted );
@@ -203,15 +197,6 @@ protected PersistenceException wrapStaleStateException(StaleStateException excep
203197
return new OptimisticLockException( exception.getMessage(), exception );
204198
}
205199

206-
protected PersistenceException wrapLockException(LockAcquisitionException exception, LockOptions lockOptions) {
207-
if ( exception instanceof org.hibernate.exception.LockTimeoutException ) {
208-
return new LockTimeoutException( exception.getMessage(), exception );
209-
}
210-
else {
211-
throw new PessimisticLockException( exception.getMessage(), exception );
212-
}
213-
}
214-
215200
protected PersistenceException wrapLockException(LockingStrategyException exception, LockOptions lockOptions) {
216201
if ( exception instanceof OptimisticEntityLockException lockException ) {
217202
return new OptimisticLockException( lockException.getMessage(), lockException, lockException.getEntity() );
@@ -228,10 +213,15 @@ else if ( exception instanceof PessimisticEntityLockException lockException ) {
228213
}
229214

230215
protected PersistenceException wrapLockException(org.hibernate.PessimisticLockException exception, LockOptions lockOptions) {
231-
// assume lock timeout occurred if a timeout or NO WAIT was specified
232-
return lockOptions != null && lockOptions.getTimeOut() > -1
233-
? new LockTimeoutException( exception.getMessage(), exception )
234-
: new PessimisticLockException( exception.getMessage(), exception );
216+
if ( exception instanceof org.hibernate.exception.LockTimeoutException ) {
217+
return new LockTimeoutException( exception.getMessage(), exception );
218+
}
219+
else {
220+
// assume lock timeout occurred if a timeout or NO WAIT was specified
221+
return lockOptions != null && lockOptions.getTimeOut() > -1
222+
? new LockTimeoutException( exception.getMessage(), exception )
223+
: new PessimisticLockException( exception.getMessage(), exception );
224+
}
235225
}
236226

237227
private void rollbackIfNecessary(PersistenceException persistenceException) {

0 commit comments

Comments
 (0)