HHH-18540 remove Session.LockRequest

Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
Gavin King 2024-08-29 10:48:06 +02:00
parent bbbaf511ec
commit 6fe0553fb9
8 changed files with 5 additions and 223 deletions

View File

@ -22,7 +22,6 @@ import jakarta.persistence.EntityGraph;
import jakarta.persistence.EntityManager;
import jakarta.persistence.FlushModeType;
import jakarta.persistence.LockModeType;
import jakarta.persistence.PessimisticLockScope;
import jakarta.persistence.TypedQueryReference;
import jakarta.persistence.criteria.CriteriaDelete;
import jakarta.persistence.criteria.CriteriaQuery;
@ -635,31 +634,6 @@ public interface Session extends SharedSessionContract, EntityManager {
@Deprecated(since = "6.2")
void lock(String entityName, Object object, LockMode lockMode);
/**
* Build a new {@linkplain LockRequest lock request} that specifies:
* <ul>
* <li>the {@link LockMode} to use,
* <li>the {@linkplain LockRequest#setTimeOut(int) pessimistic lock timeout},
* and
* <li>the {@linkplain LockRequest#setLockScope(PessimisticLockScope) scope}
* that is, whether the lock extends to rows of owned collections.
* </ul>
* <p>
* Timeout and scope are ignored if the specified {@code LockMode} represents
* a flavor of {@linkplain LockMode#OPTIMISTIC optimistic} locking.
* <p>
* Call {@link LockRequest#lock(Object)} to actually obtain the requested lock
* on a managed entity instance.
*
* @param lockOptions contains the lock level
*
* @return a {@link LockRequest} that can be used to lock any given object.
*
* @deprecated use {@link #lock(Object, LockOptions)}
*/
@Deprecated(since = "6.2")
LockRequest buildLockRequest(LockOptions lockOptions);
/**
* Reread the state of the given managed instance associated with this session
* from the underlying database. This may be useful:
@ -1157,125 +1131,6 @@ public interface Session extends SharedSessionContract, EntityManager {
*/
SharedSessionBuilder sessionWithOptions();
/**
* A set of {@linkplain LockOptions locking options} attached
* to the session.
*
* @deprecated simply construct a {@link LockOptions} and pass
* it to {@link #lock(Object, LockOptions)}.
*/
@Deprecated(since = "6.2")
interface LockRequest {
/**
* A timeout value indicating that the database should not
* wait at all to acquire a pessimistic lock which is not
* immediately available. This has the same effect as
* {@link LockMode#UPGRADE_NOWAIT}.
*/
int PESSIMISTIC_NO_WAIT = LockOptions.NO_WAIT;
/**
* A timeout value indicating that attempting to acquire
* that there is no timeout for the lock acquisition.
*/
int PESSIMISTIC_WAIT_FOREVER = LockOptions.WAIT_FOREVER;
/**
* Get the lock mode.
*
* @return the lock mode.
*/
LockMode getLockMode();
/**
* Specify the {@link LockMode} to be used. The default is {@link LockMode#NONE}.
*
* @param lockMode the lock mode to use for this request
*
* @return this {@code LockRequest} instance for operation chaining.
*/
LockRequest setLockMode(LockMode lockMode);
/**
* Get the timeout setting.
*
* @return timeout in milliseconds, -1 for indefinite wait and 0 for no wait.
*/
int getTimeOut();
/**
* Specify the pessimistic lock timeout. The default pessimistic lock
* behavior is to wait forever for the lock. Lock timeout support is
* not available in every {@link org.hibernate.dialect.Dialect dialect}
* of SQL.
*
* @param timeout is time in milliseconds to wait for lock.
* -1 means wait forever and 0 means no wait.
*
* @return this {@code LockRequest} instance for operation chaining.
*/
LockRequest setTimeOut(int timeout);
/**
* Check if locking extends to owned collections and associated entities.
*
* @return true if locking will be extended to owned collections and associated entities
*
* @deprecated use {@link #getLockScope()}
*/
@Deprecated(since = "6.2")
boolean getScope();
/**
* Obtain the {@link PessimisticLockScope}, which determines if locking
* extends to owned collections and associated entities.
*/
default PessimisticLockScope getLockScope() {
return getScope() ? PessimisticLockScope.EXTENDED : PessimisticLockScope.NORMAL;
}
/**
* Specify whether the {@link LockMode} should extend to owned collections
* and associated entities. An association must be mapped with
* {@link org.hibernate.annotations.CascadeType#LOCK} for this setting to
* have any effect.
*
* @param scope {@code true} to cascade locks; {@code false} to not.
*
* @return {@code this}, for method chaining
*
* @deprecated use {@link #setLockScope(PessimisticLockScope)}
*/
@Deprecated(since = "6.2")
LockRequest setScope(boolean scope);
/**
* Set the {@link PessimisticLockScope}, which determines if locking
* extends to owned collections and associated entities.
*/
default LockRequest setLockScope(PessimisticLockScope scope) {
return setScope( scope == PessimisticLockScope.EXTENDED );
}
/**
* Perform the requested locking.
*
* @param entityName the name of the entity to lock
* @param object the instance of the entity to lock
*
* @deprecated use {@link #lock(Object)}
*/
@Deprecated(since = "6.2")
void lock(String entityName, Object object);
/**
* Perform the requested locking.
*
* @param object the instance of the entity to lock
*/
void lock(Object object);
}
/**
* Add one or more listeners to the Session
*

View File

@ -910,11 +910,6 @@ public class SessionDelegatorBaseImpl implements SessionImplementor {
delegate.lock( object, lockOptions );
}
@Override @Deprecated
public LockRequest buildLockRequest(LockOptions lockOptions) {
return delegate.buildLockRequest( lockOptions );
}
@Override
public void refresh(Object object) {
delegate.refresh( object );

View File

@ -238,12 +238,6 @@ public class SessionLazyDelegator implements Session {
this.lazySession.get().lock( object, lockOptions );
}
@Override
@Deprecated
public LockRequest buildLockRequest(LockOptions lockOptions) {
return this.lazySession.get().buildLockRequest( lockOptions );
}
@Override
public void refresh(Object object) {
this.lazySession.get().refresh( object );

View File

@ -652,20 +652,11 @@ public class SessionImpl
fireLock( new LockEvent( entityName, object, lockMode, this ) );
}
@Override @Deprecated
public LockRequest buildLockRequest(LockOptions lockOptions) {
return new LockRequestImpl( lockOptions );
}
@Override
public void lock(Object object, LockMode lockMode) throws HibernateException {
fireLock( new LockEvent( object, lockMode, this ) );
}
private void fireLock(String entityName, Object object, LockOptions options) {
fireLock( new LockEvent( entityName, object, options, this ) );
}
private void fireLock(Object object, LockOptions options) {
fireLock( new LockEvent( object, options, this ) );
}
@ -2164,59 +2155,6 @@ public class SessionImpl
}
}
@Deprecated
private class LockRequestImpl implements LockRequest {
private final LockOptions lockOptions;
private LockRequestImpl(LockOptions lockOptions) {
this.lockOptions = new LockOptions();
LockOptions.copy( lockOptions, this.lockOptions );
}
@Override
public LockMode getLockMode() {
return lockOptions.getLockMode();
}
@Override
public LockRequest setLockMode(LockMode lockMode) {
lockOptions.setLockMode( lockMode );
return this;
}
@Override
public int getTimeOut() {
return lockOptions.getTimeOut();
}
@Override
public LockRequest setTimeOut(int timeout) {
lockOptions.setTimeOut( timeout );
return this;
}
@Override @Deprecated @SuppressWarnings("deprecation")
public boolean getScope() {
return lockOptions.getScope();
}
@Override @Deprecated @SuppressWarnings("deprecation")
public LockRequest setScope(boolean scope) {
lockOptions.setScope( scope );
return this;
}
@Override @Deprecated @SuppressWarnings("deprecation")
public void lock(String entityName, Object object) throws HibernateException {
fireLock( entityName, object, lockOptions );
}
@Override
public void lock(Object object) throws HibernateException {
fireLock( object, lockOptions );
}
}
@Override
protected void addSharedSessionTransactionObserver(TransactionCoordinator transactionCoordinator) {
transactionObserver = new TransactionObserver() {
@ -2586,7 +2524,7 @@ public class SessionImpl
final LockOptions lockOptions = buildLockOptions( lockModeType, properties );
try {
buildLockRequest( lockOptions ).lock( entity );
fireLock( entity, lockOptions );
}
catch (RuntimeException e) {
throw getExceptionConverter().convert( e, lockOptions );

View File

@ -368,7 +368,7 @@ public class SQLServerDialectTest extends BaseCoreFunctionalTestCase {
long start = System.currentTimeMillis();
thread.start();
try {
s2.buildLockRequest( opt ).lock( kit2 );
s2.lock( kit2, opt );
}
catch ( PessimisticEntityLockException e ) {
assertTrue( e.getCause() instanceof LockTimeoutException );

View File

@ -148,7 +148,7 @@ public class MutableNaturalIdTest {
scope.inTransaction(
(session) -> {
try {
session.buildLockRequest( LockOptions.NONE ).lock( created );
session.lock( created, LockOptions.NONE );
name.set( created, "Gavin" );
final User loaded = session

View File

@ -295,7 +295,7 @@ public abstract class CachedMutableNaturalIdTest {
scope.inTransaction(
(session) -> {
// HHH-7513 failure during reattachment
session.buildLockRequest( LockOptions.NONE ).lock( created );
session.lock( created, LockOptions.NONE );
session.remove( created.assA );
session.remove( created );
}

View File

@ -593,7 +593,7 @@ public class ProxyTest extends BaseCoreFunctionalTestCase {
dp = s.load( DataPoint.class, dp.getId());
assertFalse( Hibernate.isInitialized( dp ) );
s.buildLockRequest( LockOptions.UPGRADE ).lock( dp );
s.lock( dp, LockOptions.UPGRADE );
assertSame( LockOptions.UPGRADE.getLockMode(), s.getCurrentLockMode( dp ) );
s.remove( dp );