HHH-15682 add ImmutableLockOptions

here we had an amazing idiom: static final instances of a mutable value class
This commit is contained in:
Gavin King 2022-11-07 10:43:01 +01:00
parent 61294250b3
commit 58ba65f529
3 changed files with 42 additions and 6 deletions

View File

@ -48,13 +48,13 @@ public class LockOptions implements Serializable {
* Represents {@link LockMode#NONE}, to which timeout and scope are
* not applicable.
*/
public static final LockOptions NONE = new LockOptions(LockMode.NONE);
public static final LockOptions NONE = new ImmutableLockOptions(LockMode.NONE);
/**
* Represents {@link LockMode#READ}, to which timeout and scope are
* not applicable.
*/
public static final LockOptions READ = new LockOptions(LockMode.READ);
public static final LockOptions READ = new ImmutableLockOptions(LockMode.READ);
/**
* Represents {@link LockMode#PESSIMISTIC_WRITE} with
@ -62,7 +62,7 @@ public class LockOptions implements Serializable {
* {@linkplain PessimisticLockScope#NORMAL no extension of the
* lock to owned collections}.
*/
public static final LockOptions UPGRADE = new LockOptions(LockMode.PESSIMISTIC_WRITE);
public static final LockOptions UPGRADE = new ImmutableLockOptions(LockMode.PESSIMISTIC_WRITE);
/**
* Indicates that the database should not wait at all to acquire
@ -478,4 +478,40 @@ public class LockOptions implements Serializable {
public int hashCode() {
return Objects.hash( lockMode, timeout, aliasSpecificLockModes, followOnLocking, scope );
}
private static class ImmutableLockOptions extends LockOptions {
private ImmutableLockOptions(LockMode lockMode) {
super(lockMode);
}
@Override
public LockOptions setLockMode(LockMode lockMode) {
throw new UnsupportedOperationException();
}
@Override
public LockOptions setLockScope(PessimisticLockScope scope) {
throw new UnsupportedOperationException();
}
@Override
public LockOptions setFollowOnLocking(Boolean followOnLocking) {
return super.setFollowOnLocking(followOnLocking);
}
@Override
public LockOptions setTimeOut(int timeout) {
throw new UnsupportedOperationException();
}
@Override
public LockOptions setAliasSpecificLockMode(String alias, LockMode lockMode) {
throw new UnsupportedOperationException();
}
@Override
public LockOptions setScope(boolean scope) {
throw new UnsupportedOperationException();
}
}
}

View File

@ -37,7 +37,7 @@ public abstract class QueryOptionsAdapter implements QueryOptions {
@Override
public LockOptions getLockOptions() {
return LockOptions.NONE;
return new LockOptions();
}
@Override

View File

@ -421,9 +421,9 @@ public class JdbcSelectExecutorStandardImpl implements JdbcSelectExecutor {
final RowReader<R> rowReader = ResultsHelper.createRowReader(
executionContext,
// If follow on locking is used, we must omit the lock options here,
// If follow-on locking is used, we must omit the lock options here,
// because these lock options are only for Initializers.
// If we wouldn't omit this, the follow on lock requests would be no-ops,
// If we wouldn't omit this, the follow-on lock requests would be no-ops,
// because the EntityEntrys would already have the desired lock mode
deferredResultSetAccess.usesFollowOnLocking()
? LockOptions.NONE