44 */
55package org .hibernate .internal ;
66
7- import java .util .Map ;
8- import java .util .function .Supplier ;
97import jakarta .persistence .PersistenceException ;
108import jakarta .persistence .PessimisticLockScope ;
11-
9+ import jakarta . persistence . Timeout ;
1210import org .hibernate .LockOptions ;
11+ import org .hibernate .Locking ;
12+ import org .hibernate .internal .log .DeprecationLogger ;
13+
14+ import java .util .Map ;
15+ import java .util .function .Supplier ;
1316
14- import static jakarta .persistence .PessimisticLockScope .EXTENDED ;
15- import static jakarta .persistence .PessimisticLockScope .NORMAL ;
1617import static org .hibernate .cfg .AvailableSettings .JAKARTA_LOCK_SCOPE ;
1718import static org .hibernate .cfg .AvailableSettings .JAKARTA_LOCK_TIMEOUT ;
1819import static org .hibernate .cfg .AvailableSettings .JPA_LOCK_SCOPE ;
1920import static org .hibernate .cfg .AvailableSettings .JPA_LOCK_TIMEOUT ;
21+ import static org .hibernate .internal .util .config .ConfigurationHelper .getBoolean ;
22+ import static org .hibernate .jpa .HibernateHints .HINT_FOLLOW_ON_LOCKING ;
23+ import static org .hibernate .jpa .HibernateHints .HINT_FOLLOW_ON_STRATEGY ;
2024
2125public final class LockOptionsHelper {
2226
@@ -35,44 +39,80 @@ private LockOptionsHelper() {
3539 public static void applyPropertiesToLockOptions (Map <String , Object > props , Supplier <LockOptions > lockOptions ) {
3640 applyScope ( props , lockOptions );
3741 applyTimeout ( props , lockOptions );
42+ applyFollowOn ( props , lockOptions );
3843 }
3944
4045 private static void applyScope (Map <String , Object > props , Supplier <LockOptions > lockOptions ) {
41- String lockScopeHint = JPA_LOCK_SCOPE ;
42- Object lockScope = props .get ( lockScopeHint );
43- if ( lockScope == null ) {
44- lockScopeHint = JAKARTA_LOCK_SCOPE ;
45- lockScope = props .get ( lockScopeHint );
46+ String lockScopeHint = JAKARTA_LOCK_SCOPE ;
47+ Object value = props .get ( lockScopeHint );
48+ if ( value == null ) {
49+ lockScopeHint = JPA_LOCK_SCOPE ;
50+ value = props .get ( lockScopeHint );
4651 }
4752
48- if ( lockScope instanceof String string ) {
49- lockOptions .get ().setLockScope ( EXTENDED . name (). equalsIgnoreCase ( string ) ? EXTENDED : NORMAL );
53+ if ( value instanceof Locking . Scope scope ) {
54+ lockOptions .get ().setScope ( scope );
5055 }
51- else if ( lockScope instanceof PessimisticLockScope pessimisticLockScope ) {
56+ else if ( value instanceof PessimisticLockScope pessimisticLockScope ) {
5257 lockOptions .get ().setLockScope ( pessimisticLockScope );
5358 }
54- else if ( lockScope != null ) {
55- throw new PersistenceException ( "Unable to parse " + lockScopeHint + ": " + lockScope );
59+ else if ( value instanceof String string ) {
60+ final Locking .Scope scope = Locking .Scope .interpret ( string );
61+ if ( scope != null ) {
62+ lockOptions .get ().setScope ( scope );
63+ }
64+ }
65+ else if ( value != null ) {
66+ throw new PersistenceException ( "Unable to interpret " + lockScopeHint + ": " + value );
5667 }
5768 }
5869
5970 private static void applyTimeout (Map <String , Object > props , Supplier <LockOptions > lockOptions ) {
60- String timeoutHint = JPA_LOCK_TIMEOUT ;
71+ String timeoutHint = JAKARTA_LOCK_TIMEOUT ;
6172 Object lockTimeout = props .get ( timeoutHint );
6273 if (lockTimeout == null ) {
63- timeoutHint = JAKARTA_LOCK_TIMEOUT ;
74+ timeoutHint = JPA_LOCK_TIMEOUT ;
6475 lockTimeout = props .get ( timeoutHint );
76+ if ( lockTimeout != null ) {
77+ DeprecationLogger .DEPRECATION_LOGGER .deprecatedHint ( JPA_LOCK_TIMEOUT , JAKARTA_LOCK_TIMEOUT );
78+ }
6579 }
6680
67- if ( lockTimeout instanceof String string ) {
81+ if ( lockTimeout instanceof Timeout timeout ) {
82+ lockOptions .get ().setTimeout ( timeout );
83+ }
84+ else if ( lockTimeout instanceof String string ) {
6885 lockOptions .get ().setTimeOut ( Integer .parseInt ( string ) );
6986 }
7087 else if ( lockTimeout instanceof Number number ) {
7188 int timeout = number .intValue ();
7289 lockOptions .get ().setTimeOut ( timeout );
7390 }
7491 else if ( lockTimeout != null ) {
75- throw new PersistenceException ( "Unable to parse " + timeoutHint + ": " + lockTimeout );
92+ throw new PersistenceException ( "Unable to interpret " + timeoutHint + ": " + lockTimeout );
93+ }
94+ }
95+
96+ private static void applyFollowOn (Map <String , Object > props , Supplier <LockOptions > lockOptions ) {
97+ final Object strategyValue = props .get ( HINT_FOLLOW_ON_STRATEGY );
98+ if ( strategyValue != null ) {
99+ if ( strategyValue instanceof Locking .FollowOn strategy ) {
100+ lockOptions .get ().setFollowOnStrategy ( strategy );
101+ }
102+ else if ( strategyValue instanceof String name ) {
103+ lockOptions .get ().setFollowOnStrategy ( Locking .FollowOn .interpret ( name ) );
104+ }
105+ else {
106+ throw new PersistenceException ( "Unable to interpret " + HINT_FOLLOW_ON_STRATEGY + ": " + strategyValue );
107+ }
108+ }
109+ else {
110+ // accounts for manually specifying null...
111+ if ( props .containsKey ( HINT_FOLLOW_ON_LOCKING ) ) {
112+ final Locking .FollowOn strategyFromLegacy = Locking .FollowOn .fromLegacyValue ( getBoolean ( HINT_FOLLOW_ON_LOCKING , props ) );
113+ DeprecationLogger .DEPRECATION_LOGGER .deprecatedHint ( HINT_FOLLOW_ON_LOCKING , HINT_FOLLOW_ON_STRATEGY );
114+ lockOptions .get ().setFollowOnStrategy ( strategyFromLegacy );
115+ }
76116 }
77117 }
78118
0 commit comments