Skip to content

Commit c0032b1

Browse files
committed
convert our LockAcquisitionException to a JPA PessimisticLockException
1 parent 1b56e3d commit c0032b1

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

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

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.io.Serializable;
88
import java.sql.SQLException;
99

10+
import org.hibernate.AssertionFailure;
1011
import org.hibernate.HibernateException;
1112
import org.hibernate.JDBCException;
1213
import org.hibernate.LockOptions;
@@ -80,18 +81,18 @@ public RuntimeException convert(HibernateException exception, LockOptions lockOp
8081
rollbackIfNecessary( converted );
8182
return converted;
8283
}
83-
else if ( exception instanceof LockAcquisitionException ) {
84-
final PersistenceException converted = wrapLockException( exception, lockOptions );
84+
else if ( exception instanceof LockAcquisitionException lockAcquisitionException ) {
85+
final PersistenceException converted = wrapLockException( lockAcquisitionException, lockOptions );
8586
rollbackIfNecessary( converted );
8687
return converted;
8788
}
88-
else if ( exception instanceof LockingStrategyException ) {
89-
final PersistenceException converted = wrapLockException( exception, lockOptions );
89+
else if ( exception instanceof LockingStrategyException lockingStrategyException ) {
90+
final PersistenceException converted = wrapLockException( lockingStrategyException, lockOptions );
9091
rollbackIfNecessary( converted );
9192
return converted;
9293
}
93-
else if ( exception instanceof org.hibernate.PessimisticLockException ) {
94-
final PersistenceException converted = wrapLockException( exception, lockOptions );
94+
else if ( exception instanceof org.hibernate.PessimisticLockException pessimisticLockException ) {
95+
final PersistenceException converted = wrapLockException( pessimisticLockException, lockOptions );
9596
rollbackIfNecessary( converted );
9697
return converted;
9798
}
@@ -202,30 +203,37 @@ protected PersistenceException wrapStaleStateException(StaleStateException excep
202203
return new OptimisticLockException( exception.getMessage(), exception );
203204
}
204205

205-
protected PersistenceException wrapLockException(HibernateException exception, LockOptions lockOptions) {
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+
215+
protected PersistenceException wrapLockException(LockingStrategyException exception, LockOptions lockOptions) {
206216
if ( exception instanceof OptimisticEntityLockException lockException ) {
207217
return new OptimisticLockException( lockException.getMessage(), lockException, lockException.getEntity() );
208218
}
209-
else if ( exception instanceof org.hibernate.exception.LockTimeoutException ) {
210-
return new LockTimeoutException( exception.getMessage(), exception, null );
211-
}
212219
else if ( exception instanceof PessimisticEntityLockException lockException ) {
213220
// assume lock timeout occurred if a timeout or NO WAIT was specified
214221
return lockOptions != null && lockOptions.getTimeOut() > -1
215222
? new LockTimeoutException( lockException.getMessage(), lockException, lockException.getEntity() )
216223
: new PessimisticLockException( lockException.getMessage(), lockException, lockException.getEntity() );
217224
}
218-
else if ( exception instanceof org.hibernate.PessimisticLockException lockException ) {
219-
// assume lock timeout occurred if a timeout or NO WAIT was specified
220-
return lockOptions != null && lockOptions.getTimeOut() > -1
221-
? new LockTimeoutException( lockException.getMessage(), lockException, null )
222-
: new PessimisticLockException( lockException.getMessage(), lockException, null );
223-
}
224225
else {
225-
return new OptimisticLockException( exception );
226+
throw new AssertionFailure( "Unrecognized exception type" );
226227
}
227228
}
228229

230+
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 );
235+
}
236+
229237
private void rollbackIfNecessary(PersistenceException persistenceException) {
230238
if ( !isNonRollbackException( persistenceException ) ) {
231239
try {

0 commit comments

Comments
 (0)