HHH-4546 add JPA 2.0 locking. Introduce LockOptions as the wrapper and session.buildLockRequest() (replaces session.lock()).

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18053 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Scott Marlow 2009-11-24 23:59:50 +00:00
parent 49f7b49bdc
commit ceaea5a2a3
20 changed files with 280 additions and 208 deletions

View File

@ -29,7 +29,7 @@ package org.hibernate;
* *
* @author Scott Marlow * @author Scott Marlow
*/ */
public class LockRequest public class LockOptions
{ {
public static final int NO_WAIT = 0; public static final int NO_WAIT = 0;
@ -42,14 +42,13 @@ public class LockRequest
private boolean scope=false;// if true, cascade (pessimistic only) lock to collections and relationships private boolean scope=false;// if true, cascade (pessimistic only) lock to collections and relationships
// owned by the entity. // owned by the entity.
public LockRequest() { public LockOptions() {
} }
public LockRequest( LockMode lockMode, int timeout, boolean scope ) {
public LockOptions( LockMode lockMode) {
this.lockMode = lockMode; this.lockMode = lockMode;
this.timeout = timeout;
this.scope = scope;
} }
/** /**
@ -66,7 +65,7 @@ public class LockRequest
* @param lockMode * @param lockMode
* @return this LockRequest instance for operation chaining. * @return this LockRequest instance for operation chaining.
*/ */
public LockRequest setLockMode(LockMode lockMode) { public LockOptions setLockMode(LockMode lockMode) {
this.lockMode = lockMode; this.lockMode = lockMode;
return this; return this;
} }
@ -87,7 +86,7 @@ public class LockRequest
* @param timeout is time in milliseconds to wait for lock. -1 means wait forever and 0 means no wait. * @param timeout is time in milliseconds to wait for lock. -1 means wait forever and 0 means no wait.
* @return this LockRequest instance for operation chaining. * @return this LockRequest instance for operation chaining.
*/ */
public LockRequest setTimeOut(int timeout) { public LockOptions setTimeOut(int timeout) {
this.timeout = timeout; this.timeout = timeout;
return this; return this;
} }
@ -107,7 +106,7 @@ public class LockRequest
* @param scope * @param scope
* @return * @return
*/ */
public LockRequest setScope(boolean scope) { public LockOptions setScope(boolean scope) {
this.scope = scope; this.scope = scope;
return this; return this;
} }

View File

@ -270,7 +270,7 @@ public interface Session extends Serializable {
* @param lockMode the lock level * @param lockMode the lock level
* @return the persistent instance or proxy * @return the persistent instance or proxy
* @throws HibernateException * @throws HibernateException
* @deprecated LockMode parameter should be replaced with a LockRequest * @deprecated LockMode parameter should be replaced with LockOptions
*/ */
public Object load(Class theClass, Serializable id, LockMode lockMode) throws HibernateException; public Object load(Class theClass, Serializable id, LockMode lockMode) throws HibernateException;
@ -280,11 +280,11 @@ public interface Session extends Serializable {
* *
* @param theClass a persistent class * @param theClass a persistent class
* @param id a valid identifier of an existing persistent instance of the class * @param id a valid identifier of an existing persistent instance of the class
* @param lockRequest contains the lock level * @param lockOptions contains the lock level
* @return the persistent instance or proxy * @return the persistent instance or proxy
* @throws HibernateException * @throws HibernateException
*/ */
public Object load(Class theClass, Serializable id, LockRequest lockRequest) throws HibernateException; public Object load(Class theClass, Serializable id, LockOptions lockOptions) throws HibernateException;
/** /**
* Return the persistent instance of the given entity class with the given identifier, * Return the persistent instance of the given entity class with the given identifier,
@ -295,7 +295,7 @@ public interface Session extends Serializable {
* @param lockMode the lock level * @param lockMode the lock level
* @return the persistent instance or proxy * @return the persistent instance or proxy
* @throws HibernateException * @throws HibernateException
* @deprecated LockMode parameter should be replaced with a LockRequest * @deprecated LockMode parameter should be replaced with LockOptions
*/ */
public Object load(String entityName, Serializable id, LockMode lockMode) throws HibernateException; public Object load(String entityName, Serializable id, LockMode lockMode) throws HibernateException;
@ -305,11 +305,11 @@ public interface Session extends Serializable {
* *
* @param entityName a persistent class * @param entityName a persistent class
* @param id a valid identifier of an existing persistent instance of the class * @param id a valid identifier of an existing persistent instance of the class
* @param lockRequest contains the lock level * @param lockOptions contains the lock level
* @return the persistent instance or proxy * @return the persistent instance or proxy
* @throws HibernateException * @throws HibernateException
*/ */
public Object load(String entityName, Serializable id, LockRequest lockRequest) throws HibernateException; public Object load(String entityName, Serializable id, LockOptions lockOptions) throws HibernateException;
/** /**
* Return the persistent instance of the given entity class with the given identifier, * Return the persistent instance of the given entity class with the given identifier,
@ -533,23 +533,10 @@ public interface Session extends Serializable {
* @param object a persistent or transient instance * @param object a persistent or transient instance
* @param lockMode the lock level * @param lockMode the lock level
* @throws HibernateException * @throws HibernateException
* @deprecated LockMode parameter should be replaced with a LockRequest * @deprecated instead call buildLockRequest(LockMode).lock(object)
*/ */
public void lock(Object object, LockMode lockMode) throws HibernateException; public void lock(Object object, LockMode lockMode) throws HibernateException;
/**
* Obtain the specified lock level upon the given object. This may be used to
* perform a version check (<tt>LockMode.OPTIMISTIC</tt>), to upgrade to a pessimistic
* lock (<tt>LockMode.PESSIMISTIC_WRITE</tt>), or to simply reassociate a transient instance
* with a session (<tt>LockMode.NONE</tt>). This operation cascades to associated
* instances if the association is mapped with <tt>cascade="lock" and lockRequest.getScope() == true</tt>.
*
* @param object a persistent or transient instance
* @param lockRequest contains the lock level
* @throws HibernateException
*/
public void lock(Object object, LockRequest lockRequest) throws HibernateException;
/** /**
* Obtain the specified lock level upon the given object. This may be used to * Obtain the specified lock level upon the given object. This may be used to
* perform a version check (<tt>LockMode.OPTIMISTIC</tt>), to upgrade to a pessimistic * perform a version check (<tt>LockMode.OPTIMISTIC</tt>), to upgrade to a pessimistic
@ -560,33 +547,22 @@ public interface Session extends Serializable {
* @param object a persistent or transient instance * @param object a persistent or transient instance
* @param lockMode the lock level * @param lockMode the lock level
* @throws HibernateException * @throws HibernateException
* @deprecated LockMode parameter should be replaced with a LockRequest * @deprecated instead call buildLockRequest(LockMode).lock(entityName, object)
*/ */
public void lock(String entityName, Object object, LockMode lockMode) throws HibernateException; public void lock(String entityName, Object object, LockMode lockMode) throws HibernateException;
/**
* Obtain the specified lock level upon the given object. This may be used to
* perform a version check (<tt>LockMode.OPTIMISTIC</tt>), to upgrade to a pessimistic
* lock (<tt>LockMode.PESSIMISTIC_WRITE</tt>), or to simply reassociate a transient instance
* with a session (<tt>LockMode.NONE</tt>). This operation cascades to associated
* instances if the association is mapped with <tt>cascade="lock" and lockRequest.getScope() == true.</tt>.
*
* @param object a persistent or transient instance
* @param lockRequest contains the lock level
* @throws HibernateException
*/
public void lock(String entityName, Object object, LockRequest lockRequest) throws HibernateException;
/** /**
* Build a lockRequest that specifies the LockMode, pessimistic lock timeout and lock scope. * Build a lockRequest that specifies the LockMode, pessimistic lock timeout and lock scope.
* timeout and scope is ignored for optimistic locking. * timeout and scope is ignored for optimistic locking.
* *
* Use: LockRequest lr = session.buildLockRequest().setLockMode(LockMode.PESSIMISTIC_WRITE).setTimeOut(1000 * 60); * Use: session.buildLockRequest().
* session.lock(entity, lr); * setLockMode(LockMode.PESSIMISTIC_WRITE).setTimeOut(1000 * 60).lock(entity);
* *
* @param lockOptions contains the lock level
* @return a lockRequest that can be used to lock the passed object.
* @throws HibernateException * @throws HibernateException
*/ */
LockRequest buildLockRequest(); public LockRequest buildLockRequest(LockOptions lockOptions);
/** /**
* Re-read the state of the given instance from the underlying database. It is * Re-read the state of the given instance from the underlying database. It is
@ -613,7 +589,7 @@ public interface Session extends Serializable {
* @param object a persistent or detached instance * @param object a persistent or detached instance
* @param lockMode the lock mode to use * @param lockMode the lock mode to use
* @throws HibernateException * @throws HibernateException
* @deprecated LockMode parameter should be replaced with a LockRequest * @deprecated LockMode parameter should be replaced with LockOptions
*/ */
public void refresh(Object object, LockMode lockMode) throws HibernateException; public void refresh(Object object, LockMode lockMode) throws HibernateException;
@ -624,10 +600,10 @@ public interface Session extends Serializable {
* useful in certain special circumstances. * useful in certain special circumstances.
* *
* @param object a persistent or detached instance * @param object a persistent or detached instance
* @param lockRequest contains the lock mode to use * @param lockOptions contains the lock mode to use
* @throws HibernateException * @throws HibernateException
*/ */
public void refresh(Object object, LockRequest lockRequest) throws HibernateException; public void refresh(Object object, LockOptions lockOptions) throws HibernateException;
/** /**
* Determine the current lock mode of the given object. * Determine the current lock mode of the given object.
@ -766,7 +742,7 @@ public interface Session extends Serializable {
* @param lockMode the lock mode * @param lockMode the lock mode
* @return a persistent instance or null * @return a persistent instance or null
* @throws HibernateException * @throws HibernateException
* @deprecated LockMode parameter should be replaced with a LockRequest * @deprecated LockMode parameter should be replaced with LockOptions
*/ */
public Object get(Class clazz, Serializable id, LockMode lockMode) throws HibernateException; public Object get(Class clazz, Serializable id, LockMode lockMode) throws HibernateException;
@ -778,11 +754,11 @@ public interface Session extends Serializable {
* *
* @param clazz a persistent class * @param clazz a persistent class
* @param id an identifier * @param id an identifier
* @param lockRequest the lock mode * @param lockOptions the lock mode
* @return a persistent instance or null * @return a persistent instance or null
* @throws HibernateException * @throws HibernateException
*/ */
public Object get(Class clazz, Serializable id, LockRequest lockRequest) throws HibernateException; public Object get(Class clazz, Serializable id, LockOptions lockOptions) throws HibernateException;
/** /**
* Return the persistent instance of the given named entity with the given identifier, * Return the persistent instance of the given named entity with the given identifier,
@ -807,7 +783,7 @@ public interface Session extends Serializable {
* @param lockMode the lock mode * @param lockMode the lock mode
* @return a persistent instance or null * @return a persistent instance or null
* @throws HibernateException * @throws HibernateException
* @deprecated LockMode parameter should be replaced with a LockRequest * @deprecated LockMode parameter should be replaced with LockOptions
*/ */
public Object get(String entityName, Serializable id, LockMode lockMode) throws HibernateException; public Object get(String entityName, Serializable id, LockMode lockMode) throws HibernateException;
@ -819,11 +795,11 @@ public interface Session extends Serializable {
* *
* @param entityName the entity name * @param entityName the entity name
* @param id an identifier * @param id an identifier
* @param lockRequest contains the lock mode * @param lockOptions contains the lock mode
* @return a persistent instance or null * @return a persistent instance or null
* @throws HibernateException * @throws HibernateException
*/ */
public Object get(String entityName, Serializable id, LockRequest lockRequest) throws HibernateException; public Object get(String entityName, Serializable id, LockOptions lockOptions) throws HibernateException;
/** /**
@ -957,4 +933,68 @@ public interface Session extends Serializable {
* @see org.hibernate.engine.profile.FetchProfile for discussion of this feature * @see org.hibernate.engine.profile.FetchProfile for discussion of this feature
*/ */
public void disableFetchProfile(String name) throws UnknownProfileException; public void disableFetchProfile(String name) throws UnknownProfileException;
/**
* Contains locking details (LockMode, Timeout and Scope).
*
*/
public interface LockRequest
{
static final int PESSIMISTIC_NO_WAIT = 0;
static final int PESSIMISTIC_WAIT_FOREVER = -1;
/**
* Get the lock mode.
* @return the lock mode.
*/
LockMode getLockMode();
/**
* Specify the LockMode to be used. The default is LockMode.none.
*
* @param lockMode
* @return this 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 (check if your dialect supports this option).
* The default pessimistic lock behavior is to wait forever for the lock.
*
* @param timeout is time in milliseconds to wait for lock. -1 means wait forever and 0 means no wait.
* @return this LockRequest instance for operation chaining.
*/
LockRequest setTimeOut(int timeout);
/**
* Check if locking is cascaded to owned collections and relationships.
* @return true if locking will be extended to owned collections and relationships.
*/
boolean getScope();
/**
* Specify if LockMode should be cascaded to owned collections and relationships.
* The association must be mapped with <tt>cascade="lock" for scope=true to work.
*
* @param scope
* @return
*/
LockRequest setScope(boolean scope);
void lock(String entityName, Object object) throws HibernateException;
public void lock(Object object) throws HibernateException;
}
} }

View File

@ -34,7 +34,7 @@ import org.hibernate.HibernateException;
import org.hibernate.LockMode; import org.hibernate.LockMode;
import org.hibernate.ReplicationMode; import org.hibernate.ReplicationMode;
import org.hibernate.TransientObjectException; import org.hibernate.TransientObjectException;
import org.hibernate.LockRequest; import org.hibernate.LockOptions;
import org.hibernate.proxy.HibernateProxy; import org.hibernate.proxy.HibernateProxy;
import org.hibernate.persister.entity.EntityPersister; import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.collection.PersistentCollection; import org.hibernate.collection.PersistentCollection;
@ -167,16 +167,16 @@ public abstract class CascadingAction {
log.trace( "cascading to lock: " + entityName ); log.trace( "cascading to lock: " + entityName );
} }
LockMode lockMode = LockMode.NONE; LockMode lockMode = LockMode.NONE;
LockRequest lr = new LockRequest(); LockOptions lr = new LockOptions();
if ( anything instanceof LockRequest ) { if ( anything instanceof LockOptions) {
LockRequest lockRequest = (LockRequest)anything; LockOptions lockOptions = (LockOptions)anything;
lr.setTimeOut(lockRequest.getTimeOut()); lr.setTimeOut(lockOptions.getTimeOut());
lr.setScope( lockRequest.getScope()); lr.setScope( lockOptions.getScope());
if ( lockRequest.getScope() == true ) // cascade specified lockMode if ( lockOptions.getScope() == true ) // cascade specified lockMode
lockMode = lockRequest.getLockMode(); lockMode = lockOptions.getLockMode();
} }
lr.setLockMode(lockMode); lr.setLockMode(lockMode);
session.lock( entityName, child, lr); session.buildLockRequest(lr).lock(entityName, child);
} }
public Iterator getCascadableChildrenIterator(EventSource session, CollectionType collectionType, Object collection) { public Iterator getCascadableChildrenIterator(EventSource session, CollectionType collectionType, Object collection) {
// lock doesn't cascade to uninitialized collections // lock doesn't cascade to uninitialized collections

View File

@ -27,7 +27,7 @@ package org.hibernate.event;
import java.io.Serializable; import java.io.Serializable;
import org.hibernate.LockMode; import org.hibernate.LockMode;
import org.hibernate.LockRequest; import org.hibernate.LockOptions;
/** /**
* Defines an event class for the loading of an entity. * Defines an event class for the loading of an entity.
@ -41,24 +41,24 @@ public class LoadEvent extends AbstractEvent {
private Serializable entityId; private Serializable entityId;
private String entityClassName; private String entityClassName;
private Object instanceToLoad; private Object instanceToLoad;
private LockRequest lockRequest; private LockOptions lockOptions;
private boolean isAssociationFetch; private boolean isAssociationFetch;
private Object result; private Object result;
public LoadEvent(Serializable entityId, Object instanceToLoad, EventSource source) { public LoadEvent(Serializable entityId, Object instanceToLoad, EventSource source) {
this(entityId, null, instanceToLoad, new LockRequest(), false, source); this(entityId, null, instanceToLoad, new LockOptions(), false, source);
} }
public LoadEvent(Serializable entityId, String entityClassName, LockMode lockMode, EventSource source) { public LoadEvent(Serializable entityId, String entityClassName, LockMode lockMode, EventSource source) {
this(entityId, entityClassName, null, lockMode, false, source); this(entityId, entityClassName, null, lockMode, false, source);
} }
public LoadEvent(Serializable entityId, String entityClassName, LockRequest lockRequest, EventSource source) { public LoadEvent(Serializable entityId, String entityClassName, LockOptions lockOptions, EventSource source) {
this(entityId, entityClassName, null, lockRequest, false, source); this(entityId, entityClassName, null, lockOptions, false, source);
} }
public LoadEvent(Serializable entityId, String entityClassName, boolean isAssociationFetch, EventSource source) { public LoadEvent(Serializable entityId, String entityClassName, boolean isAssociationFetch, EventSource source) {
this(entityId, entityClassName, null, new LockRequest(), isAssociationFetch, source); this(entityId, entityClassName, null, new LockOptions(), isAssociationFetch, source);
} }
public boolean isAssociationFetch() { public boolean isAssociationFetch() {
@ -72,14 +72,14 @@ public class LoadEvent extends AbstractEvent {
LockMode lockMode, LockMode lockMode,
boolean isAssociationFetch, boolean isAssociationFetch,
EventSource source) { EventSource source) {
this(entityId, entityClassName, instanceToLoad, new LockRequest().setLockMode(lockMode), isAssociationFetch, source ); this(entityId, entityClassName, instanceToLoad, new LockOptions().setLockMode(lockMode), isAssociationFetch, source );
} }
private LoadEvent( private LoadEvent(
Serializable entityId, Serializable entityId,
String entityClassName, String entityClassName,
Object instanceToLoad, Object instanceToLoad,
LockRequest lockRequest, LockOptions lockOptions,
boolean isAssociationFetch, boolean isAssociationFetch,
EventSource source) { EventSource source) {
@ -89,17 +89,17 @@ public class LoadEvent extends AbstractEvent {
throw new IllegalArgumentException("id to load is required for loading"); throw new IllegalArgumentException("id to load is required for loading");
} }
if ( lockRequest.getLockMode() == LockMode.WRITE ) { if ( lockOptions.getLockMode() == LockMode.WRITE ) {
throw new IllegalArgumentException("Invalid lock mode for loading"); throw new IllegalArgumentException("Invalid lock mode for loading");
} }
else if ( lockRequest.getLockMode() == null ) { else if ( lockOptions.getLockMode() == null ) {
lockRequest.setLockMode(DEFAULT_LOCK_MODE); lockOptions.setLockMode(DEFAULT_LOCK_MODE);
} }
this.entityId = entityId; this.entityId = entityId;
this.entityClassName = entityClassName; this.entityClassName = entityClassName;
this.instanceToLoad = instanceToLoad; this.instanceToLoad = instanceToLoad;
this.lockRequest = lockRequest; this.lockOptions = lockOptions;
this.isAssociationFetch = isAssociationFetch; this.isAssociationFetch = isAssociationFetch;
} }
@ -127,32 +127,32 @@ public class LoadEvent extends AbstractEvent {
this.instanceToLoad = instanceToLoad; this.instanceToLoad = instanceToLoad;
} }
public LockRequest getLockRequest() { public LockOptions getLockOptions() {
return lockRequest; return lockOptions;
} }
public LockMode getLockMode() { public LockMode getLockMode() {
return lockRequest.getLockMode(); return lockOptions.getLockMode();
} }
public void setLockMode(LockMode lockMode) { public void setLockMode(LockMode lockMode) {
this.lockRequest.setLockMode(lockMode); this.lockOptions.setLockMode(lockMode);
} }
public void setLockTimeout(int timeout) { public void setLockTimeout(int timeout) {
this.lockRequest.setTimeOut(timeout); this.lockOptions.setTimeOut(timeout);
} }
public int getLockTimeout() { public int getLockTimeout() {
return this.lockRequest.getTimeOut(); return this.lockOptions.getTimeOut();
} }
public void setLockScope(boolean cascade) { public void setLockScope(boolean cascade) {
this.lockRequest.setScope(cascade); this.lockOptions.setScope(cascade);
} }
public boolean getLockScope() { public boolean getLockScope() {
return this.lockRequest.getScope(); return this.lockOptions.getScope();
} }
public Object getResult() { public Object getResult() {

View File

@ -25,7 +25,7 @@
package org.hibernate.event; package org.hibernate.event;
import org.hibernate.LockMode; import org.hibernate.LockMode;
import org.hibernate.LockRequest; import org.hibernate.LockOptions;
/** /**
* Defines an event class for the locking of an entity. * Defines an event class for the locking of an entity.
@ -35,7 +35,7 @@ import org.hibernate.LockRequest;
public class LockEvent extends AbstractEvent { public class LockEvent extends AbstractEvent {
private Object object; private Object object;
private LockRequest lockRequest; private LockOptions lockOptions;
private String entityName; private String entityName;
public LockEvent(String entityName, Object original, LockMode lockMode, EventSource source) { public LockEvent(String entityName, Object original, LockMode lockMode, EventSource source) {
@ -43,21 +43,21 @@ public class LockEvent extends AbstractEvent {
this.entityName = entityName; this.entityName = entityName;
} }
public LockEvent(String entityName, Object original, LockRequest lockRequest, EventSource source) { public LockEvent(String entityName, Object original, LockOptions lockOptions, EventSource source) {
this(original, lockRequest, source); this(original, lockOptions, source);
this.entityName = entityName; this.entityName = entityName;
} }
public LockEvent(Object object, LockMode lockMode, EventSource source) { public LockEvent(Object object, LockMode lockMode, EventSource source) {
super(source); super(source);
this.object = object; this.object = object;
this.lockRequest = new LockRequest().setLockMode(lockMode); this.lockOptions = new LockOptions().setLockMode(lockMode);
} }
public LockEvent(Object object, LockRequest lockRequest, EventSource source) { public LockEvent(Object object, LockOptions lockOptions, EventSource source) {
super(source); super(source);
this.object = object; this.object = object;
this.lockRequest = lockRequest; this.lockOptions = lockOptions;
} }
public Object getObject() { public Object getObject() {
@ -68,32 +68,32 @@ public class LockEvent extends AbstractEvent {
this.object = object; this.object = object;
} }
public LockRequest getLockRequest() { public LockOptions getLockOptions() {
return lockRequest; return lockOptions;
} }
public LockMode getLockMode() { public LockMode getLockMode() {
return lockRequest.getLockMode(); return lockOptions.getLockMode();
} }
public void setLockMode(LockMode lockMode) { public void setLockMode(LockMode lockMode) {
this.lockRequest.setLockMode(lockMode); this.lockOptions.setLockMode(lockMode);
} }
public void setLockTimeout(int timeout) { public void setLockTimeout(int timeout) {
this.lockRequest.setTimeOut(timeout); this.lockOptions.setTimeOut(timeout);
} }
public int getLockTimeout() { public int getLockTimeout() {
return this.lockRequest.getTimeOut(); return this.lockOptions.getTimeOut();
} }
public void setLockScope(boolean cascade) { public void setLockScope(boolean cascade) {
this.lockRequest.setScope(cascade); this.lockOptions.setScope(cascade);
} }
public boolean getLockScope() { public boolean getLockScope() {
return this.lockRequest.getScope(); return this.lockOptions.getScope();
} }
public String getEntityName() { public String getEntityName() {

View File

@ -25,7 +25,7 @@
package org.hibernate.event; package org.hibernate.event;
import org.hibernate.LockMode; import org.hibernate.LockMode;
import org.hibernate.LockRequest; import org.hibernate.LockOptions;
/** /**
* Defines an event class for the refreshing of an object. * Defines an event class for the refreshing of an object.
@ -35,7 +35,7 @@ import org.hibernate.LockRequest;
public class RefreshEvent extends AbstractEvent { public class RefreshEvent extends AbstractEvent {
private Object object; private Object object;
private LockRequest lockRequest = new LockRequest().setLockMode(LockMode.READ); private LockOptions lockOptions = new LockOptions().setLockMode(LockMode.READ);
public RefreshEvent(Object object, EventSource source) { public RefreshEvent(Object object, EventSource source) {
super(source); super(source);
@ -50,34 +50,34 @@ public class RefreshEvent extends AbstractEvent {
if (lockMode == null) { if (lockMode == null) {
throw new IllegalArgumentException("Attempt to generate refresh event with null lock mode"); throw new IllegalArgumentException("Attempt to generate refresh event with null lock mode");
} }
this.lockRequest.setLockMode(lockMode); this.lockOptions.setLockMode(lockMode);
} }
public RefreshEvent(Object object, LockRequest lockRequest, EventSource source) { public RefreshEvent(Object object, LockOptions lockOptions, EventSource source) {
this(object, source); this(object, source);
if (lockRequest == null) { if (lockOptions == null) {
throw new IllegalArgumentException("Attempt to generate refresh event with null lock request"); throw new IllegalArgumentException("Attempt to generate refresh event with null lock request");
} }
this.lockRequest = lockRequest; this.lockOptions = lockOptions;
} }
public Object getObject() { public Object getObject() {
return object; return object;
} }
public LockRequest getLockRequest() { public LockOptions getLockOptions() {
return lockRequest; return lockOptions;
} }
public LockMode getLockMode() { public LockMode getLockMode() {
return lockRequest.getLockMode(); return lockOptions.getLockMode();
} }
public int getLockTimeout() { public int getLockTimeout() {
return this.lockRequest.getTimeOut(); return this.lockOptions.getTimeOut();
} }
public boolean getLockScope() { public boolean getLockScope() {
return this.lockRequest.getScope(); return this.lockOptions.getScope();
} }
} }

View File

@ -29,11 +29,8 @@ import org.slf4j.LoggerFactory;
import org.hibernate.LockMode; import org.hibernate.LockMode;
import org.hibernate.ObjectDeletedException; import org.hibernate.ObjectDeletedException;
import org.hibernate.OptimisticLockException; import org.hibernate.LockOptions;
import org.hibernate.LockRequest;
import org.hibernate.event.EventSource; import org.hibernate.event.EventSource;
import org.hibernate.action.EntityIncrementVersionProcess;
import org.hibernate.action.EntityVerifyVersionProcess;
import org.hibernate.cache.CacheKey; import org.hibernate.cache.CacheKey;
import org.hibernate.cache.access.SoftLock; import org.hibernate.cache.access.SoftLock;
import org.hibernate.engine.EntityEntry; import org.hibernate.engine.EntityEntry;
@ -56,12 +53,12 @@ public class AbstractLockUpgradeEventListener extends AbstractReassociateEventLi
* *
* @param object The entity for which to upgrade the lock. * @param object The entity for which to upgrade the lock.
* @param entry The entity's EntityEntry instance. * @param entry The entity's EntityEntry instance.
* @param lockRequest contains the requested lock mode. * @param lockOptions contains the requested lock mode.
* @param source The session which is the source of the event being processed. * @param source The session which is the source of the event being processed.
*/ */
protected void upgradeLock(Object object, EntityEntry entry, LockRequest lockRequest, EventSource source) { protected void upgradeLock(Object object, EntityEntry entry, LockOptions lockOptions, EventSource source) {
LockMode requestedLockMode = lockRequest.getLockMode(); LockMode requestedLockMode = lockOptions.getLockMode();
if ( requestedLockMode.greaterThan( entry.getLockMode() ) ) { if ( requestedLockMode.greaterThan( entry.getLockMode() ) ) {
// The user requested a "greater" (i.e. more restrictive) form of // The user requested a "greater" (i.e. more restrictive) form of
// pessimistic lock // pessimistic lock

View File

@ -485,7 +485,7 @@ public class DefaultLoadEventListener extends AbstractLockUpgradeEventListener i
return INCONSISTENT_RTN_CLASS_MARKER; return INCONSISTENT_RTN_CLASS_MARKER;
} }
} }
upgradeLock( old, oldEntry, event.getLockRequest(), event.getSession() ); upgradeLock( old, oldEntry, event.getLockOptions(), event.getSession() );
} }
return old; return old;

View File

@ -83,7 +83,7 @@ public class DefaultLockEventListener extends AbstractLockUpgradeEventListener i
cascadeOnLock(event, persister, entity); cascadeOnLock(event, persister, entity);
} }
upgradeLock( entity, entry, event.getLockRequest(), event.getSession() ); upgradeLock( entity, entry, event.getLockOptions(), event.getSession() );
} }
private void cascadeOnLock(LockEvent event, EntityPersister persister, Object entity) { private void cascadeOnLock(LockEvent event, EntityPersister persister, Object entity) {
@ -91,7 +91,7 @@ public class DefaultLockEventListener extends AbstractLockUpgradeEventListener i
source.getPersistenceContext().incrementCascadeLevel(); source.getPersistenceContext().incrementCascadeLevel();
try { try {
new Cascade(CascadingAction.LOCK, Cascade.AFTER_LOCK, source) new Cascade(CascadingAction.LOCK, Cascade.AFTER_LOCK, source)
.cascade( persister, entity, event.getLockRequest() ); .cascade( persister, entity, event.getLockOptions() );
} }
finally { finally {
source.getPersistenceContext().decrementCascadeLevel(); source.getPersistenceContext().decrementCascadeLevel();

View File

@ -142,7 +142,7 @@ public class DefaultRefreshEventListener implements RefreshEventListener {
String previousFetchProfile = source.getFetchProfile(); String previousFetchProfile = source.getFetchProfile();
source.setFetchProfile("refresh"); source.setFetchProfile("refresh");
Object result = persister.load( id, object, event.getLockRequest(), source ); Object result = persister.load( id, object, event.getLockOptions(), source );
source.setFetchProfile(previousFetchProfile); source.setFetchProfile(previousFetchProfile);
UnresolvableObjectException.throwIfNull( result, id, persister.getEntityName() ); UnresolvableObjectException.throwIfNull( result, id, persister.getEntityName() );

View File

@ -69,7 +69,7 @@ import org.hibernate.TransientObjectException;
import org.hibernate.UnresolvableObjectException; import org.hibernate.UnresolvableObjectException;
import org.hibernate.UnknownProfileException; import org.hibernate.UnknownProfileException;
import org.hibernate.EntityNameResolver; import org.hibernate.EntityNameResolver;
import org.hibernate.LockRequest; import org.hibernate.LockOptions;
import org.hibernate.collection.PersistentCollection; import org.hibernate.collection.PersistentCollection;
import org.hibernate.engine.ActionQueue; import org.hibernate.engine.ActionQueue;
import org.hibernate.engine.CollectionEntry; import org.hibernate.engine.CollectionEntry;
@ -742,21 +742,20 @@ public final class SessionImpl extends AbstractSessionImpl
fireLock( new LockEvent(entityName, object, lockMode, this) ); fireLock( new LockEvent(entityName, object, lockMode, this) );
} }
public void lock(String entityName, Object object, LockRequest lockRequest) throws HibernateException { public LockRequest buildLockRequest(LockOptions lockOptions) {
fireLock( new LockEvent(entityName, object, lockRequest, this) ); return new LockRequestImpl(lockOptions);
}
public LockRequest buildLockRequest() {
return new LockRequest();
} }
public void lock(Object object, LockMode lockMode) throws HibernateException { public void lock(Object object, LockMode lockMode) throws HibernateException {
fireLock( new LockEvent(object, lockMode, this) ); fireLock( new LockEvent(object, lockMode, this) );
} }
private void fireLock(String entityName, Object object, LockOptions options) {
fireLock( new LockEvent( entityName, object, options, this) );
}
public void lock(Object object, LockRequest lockRequest) throws HibernateException { private void fireLock( Object object, LockOptions options) {
fireLock( new LockEvent(object, lockRequest, this) ); fireLock( new LockEvent( object, options, this) );
} }
private void fireLock(LockEvent lockEvent) { private void fireLock(LockEvent lockEvent) {
@ -1037,8 +1036,8 @@ public final class SessionImpl extends AbstractSessionImpl
return load( entityClass.getName(), id, lockMode ); return load( entityClass.getName(), id, lockMode );
} }
public Object load(Class entityClass, Serializable id, LockRequest lockRequest) throws HibernateException { public Object load(Class entityClass, Serializable id, LockOptions lockOptions) throws HibernateException {
return load( entityClass.getName(), id, lockRequest ); return load( entityClass.getName(), id, lockOptions);
} }
public Object load(String entityName, Serializable id, LockMode lockMode) throws HibernateException { public Object load(String entityName, Serializable id, LockMode lockMode) throws HibernateException {
@ -1047,8 +1046,8 @@ public final class SessionImpl extends AbstractSessionImpl
return event.getResult(); return event.getResult();
} }
public Object load(String entityName, Serializable id, LockRequest lockRequest) throws HibernateException { public Object load(String entityName, Serializable id, LockOptions lockOptions) throws HibernateException {
LoadEvent event = new LoadEvent(id, entityName, lockRequest, this); LoadEvent event = new LoadEvent(id, entityName, lockOptions, this);
fireLoad( event, LoadEventListener.LOAD ); fireLoad( event, LoadEventListener.LOAD );
return event.getResult(); return event.getResult();
} }
@ -1057,8 +1056,8 @@ public final class SessionImpl extends AbstractSessionImpl
return get( entityClass.getName(), id, lockMode ); return get( entityClass.getName(), id, lockMode );
} }
public Object get(Class entityClass, Serializable id, LockRequest lockRequest) throws HibernateException { public Object get(Class entityClass, Serializable id, LockOptions lockOptions) throws HibernateException {
return get( entityClass.getName(), id, lockRequest ); return get( entityClass.getName(), id, lockOptions);
} }
public Object get(String entityName, Serializable id, LockMode lockMode) throws HibernateException { public Object get(String entityName, Serializable id, LockMode lockMode) throws HibernateException {
@ -1067,8 +1066,8 @@ public final class SessionImpl extends AbstractSessionImpl
return event.getResult(); return event.getResult();
} }
public Object get(String entityName, Serializable id, LockRequest lockRequest) throws HibernateException { public Object get(String entityName, Serializable id, LockOptions lockOptions) throws HibernateException {
LoadEvent event = new LoadEvent(id, entityName, lockRequest, this); LoadEvent event = new LoadEvent(id, entityName, lockOptions, this);
fireLoad(event, LoadEventListener.GET); fireLoad(event, LoadEventListener.GET);
return event.getResult(); return event.getResult();
} }
@ -1093,8 +1092,8 @@ public final class SessionImpl extends AbstractSessionImpl
fireRefresh( new RefreshEvent(object, lockMode, this) ); fireRefresh( new RefreshEvent(object, lockMode, this) );
} }
public void refresh(Object object, LockRequest lockRequest) throws HibernateException { public void refresh(Object object, LockOptions lockOptions) throws HibernateException {
fireRefresh( new RefreshEvent(object, lockRequest, this) ); fireRefresh( new RefreshEvent(object, lockOptions, this) );
} }
public void refresh(Object object, Map refreshedAlready) throws HibernateException { public void refresh(Object object, Map refreshedAlready) throws HibernateException {
@ -2217,4 +2216,48 @@ public final class SessionImpl extends AbstractSessionImpl
return entity.getClass().getName(); return entity.getClass().getName();
} }
} }
private class LockRequestImpl implements LockRequest {
private final LockOptions lockOptions;
private LockRequestImpl(LockOptions lo) {
lockOptions = new LockOptions();
lockOptions.setLockMode(lo.getLockMode());
lockOptions.setScope(lo.getScope());
lockOptions.setTimeOut(lo.getTimeOut());
}
public LockMode getLockMode() {
return lockOptions.getLockMode();
}
public LockRequest setLockMode(LockMode lockMode) {
lockOptions.setLockMode(lockMode);
return this;
}
public int getTimeOut() {
return lockOptions.getTimeOut();
}
public LockRequest setTimeOut(int timeout) {
lockOptions.setTimeOut(timeout);
return this;
}
public boolean getScope() {
return lockOptions.getScope();
}
public LockRequest setScope(boolean scope) {
lockOptions.setScope(scope);
return this;
}
public void lock(String entityName, Object object) throws HibernateException {
fireLock( entityName, object, lockOptions );
}
public void lock(Object object) throws HibernateException {
fireLock( object, lockOptions );
}
}
} }

View File

@ -31,7 +31,7 @@ import java.util.Iterator;
import org.hibernate.FetchMode; import org.hibernate.FetchMode;
import org.hibernate.LockMode; import org.hibernate.LockMode;
import org.hibernate.MappingException; import org.hibernate.MappingException;
import org.hibernate.LockRequest; import org.hibernate.LockOptions;
import org.hibernate.engine.CascadeStyle; import org.hibernate.engine.CascadeStyle;
import org.hibernate.engine.SessionFactoryImplementor; import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.engine.LoadQueryInfluencers; import org.hibernate.engine.LoadQueryInfluencers;
@ -98,7 +98,7 @@ public abstract class AbstractEntityJoinWalker extends JoinWalker {
protected final void initAll( protected final void initAll(
final String whereString, final String whereString,
final String orderByString, final String orderByString,
final LockRequest lockRequest) throws MappingException { final LockOptions lockOptions) throws MappingException {
walkEntityTree( persister, getAlias() ); walkEntityTree( persister, getAlias() );
List allAssociations = new ArrayList(); List allAssociations = new ArrayList();
allAssociations.addAll(associations); allAssociations.addAll(associations);
@ -114,8 +114,8 @@ public abstract class AbstractEntityJoinWalker extends JoinWalker {
CollectionHelper.EMPTY_MAP CollectionHelper.EMPTY_MAP
) )
); );
initPersisters(allAssociations, lockRequest.getLockMode()); initPersisters(allAssociations, lockOptions.getLockMode());
initStatementString( whereString, orderByString, lockRequest.getLockMode()); initStatementString( whereString, orderByString, lockOptions.getLockMode());
} }
protected final void initProjection( protected final void initProjection(

View File

@ -27,13 +27,11 @@ package org.hibernate.loader.entity;
import java.io.Serializable; import java.io.Serializable;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Set;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
import org.hibernate.LockMode; import org.hibernate.LockMode;
import org.hibernate.MappingException; import org.hibernate.MappingException;
import org.hibernate.LockRequest; import org.hibernate.LockOptions;
import org.hibernate.engine.SessionFactoryImplementor; import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.engine.SessionImplementor; import org.hibernate.engine.SessionImplementor;
import org.hibernate.engine.LoadQueryInfluencers; import org.hibernate.engine.LoadQueryInfluencers;
@ -132,7 +130,7 @@ public class BatchingEntityLoader implements UniqueEntityLoader {
public static UniqueEntityLoader createBatchingEntityLoader( public static UniqueEntityLoader createBatchingEntityLoader(
final OuterJoinLoadable persister, final OuterJoinLoadable persister,
final int maxBatchSize, final int maxBatchSize,
final LockRequest lockRequest, final LockOptions lockOptions,
final SessionFactoryImplementor factory, final SessionFactoryImplementor factory,
final LoadQueryInfluencers loadQueryInfluencers) throws MappingException { final LoadQueryInfluencers loadQueryInfluencers) throws MappingException {
@ -140,12 +138,12 @@ public class BatchingEntityLoader implements UniqueEntityLoader {
int[] batchSizesToCreate = ArrayHelper.getBatchSizes(maxBatchSize); int[] batchSizesToCreate = ArrayHelper.getBatchSizes(maxBatchSize);
Loader[] loadersToCreate = new Loader[ batchSizesToCreate.length ]; Loader[] loadersToCreate = new Loader[ batchSizesToCreate.length ];
for ( int i=0; i<batchSizesToCreate.length; i++ ) { for ( int i=0; i<batchSizesToCreate.length; i++ ) {
loadersToCreate[i] = new EntityLoader(persister, batchSizesToCreate[i], lockRequest, factory, loadQueryInfluencers); loadersToCreate[i] = new EntityLoader(persister, batchSizesToCreate[i], lockOptions, factory, loadQueryInfluencers);
} }
return new BatchingEntityLoader(persister, batchSizesToCreate, loadersToCreate); return new BatchingEntityLoader(persister, batchSizesToCreate, loadersToCreate);
} }
else { else {
return new EntityLoader(persister, lockRequest, factory, loadQueryInfluencers); return new EntityLoader(persister, lockOptions, factory, loadQueryInfluencers);
} }
} }

View File

@ -25,17 +25,14 @@
package org.hibernate.loader.entity; package org.hibernate.loader.entity;
import java.util.Collections; import java.util.Collections;
import java.util.Iterator;
import org.hibernate.FetchMode; import org.hibernate.FetchMode;
import org.hibernate.LockMode; import org.hibernate.LockMode;
import org.hibernate.MappingException; import org.hibernate.MappingException;
import org.hibernate.LockRequest; import org.hibernate.LockOptions;
import org.hibernate.engine.CascadeStyle; import org.hibernate.engine.CascadeStyle;
import org.hibernate.engine.SessionFactoryImplementor; import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.engine.LoadQueryInfluencers; import org.hibernate.engine.LoadQueryInfluencers;
import org.hibernate.engine.profile.FetchProfile;
import org.hibernate.engine.profile.Fetch;
import org.hibernate.loader.AbstractEntityJoinWalker; import org.hibernate.loader.AbstractEntityJoinWalker;
import org.hibernate.persister.entity.OuterJoinLoadable; import org.hibernate.persister.entity.OuterJoinLoadable;
import org.hibernate.type.AssociationType; import org.hibernate.type.AssociationType;
@ -48,7 +45,7 @@ import org.hibernate.type.AssociationType;
*/ */
public class EntityJoinWalker extends AbstractEntityJoinWalker { public class EntityJoinWalker extends AbstractEntityJoinWalker {
private final LockRequest lockRequest = new LockRequest(); private final LockOptions lockOptions = new LockOptions();
public EntityJoinWalker( public EntityJoinWalker(
OuterJoinLoadable persister, OuterJoinLoadable persister,
@ -59,33 +56,33 @@ public class EntityJoinWalker extends AbstractEntityJoinWalker {
LoadQueryInfluencers loadQueryInfluencers) throws MappingException { LoadQueryInfluencers loadQueryInfluencers) throws MappingException {
super( persister, factory, loadQueryInfluencers ); super( persister, factory, loadQueryInfluencers );
this.lockRequest.setLockMode(lockMode); this.lockOptions.setLockMode(lockMode);
StringBuffer whereCondition = whereString( getAlias(), uniqueKey, batchSize ) StringBuffer whereCondition = whereString( getAlias(), uniqueKey, batchSize )
//include the discriminator and class-level where, but not filters //include the discriminator and class-level where, but not filters
.append( persister.filterFragment( getAlias(), Collections.EMPTY_MAP ) ); .append( persister.filterFragment( getAlias(), Collections.EMPTY_MAP ) );
initAll( whereCondition.toString(), "", lockRequest ); initAll( whereCondition.toString(), "", lockOptions);
} }
public EntityJoinWalker( public EntityJoinWalker(
OuterJoinLoadable persister, OuterJoinLoadable persister,
String[] uniqueKey, String[] uniqueKey,
int batchSize, int batchSize,
LockRequest lockRequest, LockOptions lockOptions,
SessionFactoryImplementor factory, SessionFactoryImplementor factory,
LoadQueryInfluencers loadQueryInfluencers) throws MappingException { LoadQueryInfluencers loadQueryInfluencers) throws MappingException {
super( persister, factory, loadQueryInfluencers ); super( persister, factory, loadQueryInfluencers );
this.lockRequest.setLockMode(lockRequest.getLockMode()); this.lockOptions.setLockMode(lockOptions.getLockMode());
this.lockRequest.setTimeOut(lockRequest.getTimeOut()); this.lockOptions.setTimeOut(lockOptions.getTimeOut());
this.lockRequest.setScope(lockRequest.getScope()); this.lockOptions.setScope(lockOptions.getScope());
StringBuffer whereCondition = whereString( getAlias(), uniqueKey, batchSize ) StringBuffer whereCondition = whereString( getAlias(), uniqueKey, batchSize )
//include the discriminator and class-level where, but not filters //include the discriminator and class-level where, but not filters
.append( persister.filterFragment( getAlias(), Collections.EMPTY_MAP ) ); .append( persister.filterFragment( getAlias(), Collections.EMPTY_MAP ) );
initAll( whereCondition.toString(), "", lockRequest); initAll( whereCondition.toString(), "", lockOptions);
} }
protected int getJoinType( protected int getJoinType(
@ -102,7 +99,7 @@ public class EntityJoinWalker extends AbstractEntityJoinWalker {
// NOTE : we override this form here specifically to account for // NOTE : we override this form here specifically to account for
// fetch profiles. // fetch profiles.
// TODO : how to best handle criteria queries? // TODO : how to best handle criteria queries?
if ( lockRequest.getLockMode().greaterThan( LockMode.READ ) ) { if ( lockOptions.getLockMode().greaterThan( LockMode.READ ) ) {
return -1; return -1;
} }
if ( isTooDeep( currentDepth ) if ( isTooDeep( currentDepth )

View File

@ -27,7 +27,7 @@ package org.hibernate.loader.entity;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
import org.hibernate.LockMode; import org.hibernate.LockMode;
import org.hibernate.MappingException; import org.hibernate.MappingException;
import org.hibernate.LockRequest; import org.hibernate.LockOptions;
import org.hibernate.engine.SessionFactoryImplementor; import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.engine.SessionImplementor; import org.hibernate.engine.SessionImplementor;
import org.hibernate.engine.LoadQueryInfluencers; import org.hibernate.engine.LoadQueryInfluencers;
@ -57,10 +57,10 @@ public class EntityLoader extends AbstractEntityLoader {
public EntityLoader( public EntityLoader(
OuterJoinLoadable persister, OuterJoinLoadable persister,
LockRequest lockRequest, LockOptions lockOptions,
SessionFactoryImplementor factory, SessionFactoryImplementor factory,
LoadQueryInfluencers loadQueryInfluencers) throws MappingException { LoadQueryInfluencers loadQueryInfluencers) throws MappingException {
this( persister, 1, lockRequest, factory, loadQueryInfluencers ); this( persister, 1, lockOptions, factory, loadQueryInfluencers );
} }
public EntityLoader( public EntityLoader(
@ -83,7 +83,7 @@ public class EntityLoader extends AbstractEntityLoader {
public EntityLoader( public EntityLoader(
OuterJoinLoadable persister, OuterJoinLoadable persister,
int batchSize, int batchSize,
LockRequest lockRequest, LockOptions lockOptions,
SessionFactoryImplementor factory, SessionFactoryImplementor factory,
LoadQueryInfluencers loadQueryInfluencers) throws MappingException { LoadQueryInfluencers loadQueryInfluencers) throws MappingException {
this( this(
@ -91,7 +91,7 @@ public class EntityLoader extends AbstractEntityLoader {
persister.getIdentifierColumnNames(), persister.getIdentifierColumnNames(),
persister.getIdentifierType(), persister.getIdentifierType(),
batchSize, batchSize,
lockRequest, lockOptions,
factory, factory,
loadQueryInfluencers loadQueryInfluencers
); );
@ -130,7 +130,7 @@ public class EntityLoader extends AbstractEntityLoader {
String[] uniqueKey, String[] uniqueKey,
Type uniqueKeyType, Type uniqueKeyType,
int batchSize, int batchSize,
LockRequest lockRequest, LockOptions lockOptions,
SessionFactoryImplementor factory, SessionFactoryImplementor factory,
LoadQueryInfluencers loadQueryInfluencers) throws MappingException { LoadQueryInfluencers loadQueryInfluencers) throws MappingException {
super( persister, uniqueKeyType, factory, loadQueryInfluencers ); super( persister, uniqueKeyType, factory, loadQueryInfluencers );
@ -139,7 +139,7 @@ public class EntityLoader extends AbstractEntityLoader {
persister, persister,
uniqueKey, uniqueKey,
batchSize, batchSize,
lockRequest, lockOptions,
factory, factory,
loadQueryInfluencers loadQueryInfluencers
); );

View File

@ -46,7 +46,7 @@ import org.hibernate.MappingException;
import org.hibernate.QueryException; import org.hibernate.QueryException;
import org.hibernate.StaleObjectStateException; import org.hibernate.StaleObjectStateException;
import org.hibernate.StaleStateException; import org.hibernate.StaleStateException;
import org.hibernate.LockRequest; import org.hibernate.LockOptions;
import org.hibernate.cache.CacheKey; import org.hibernate.cache.CacheKey;
import org.hibernate.cache.access.EntityRegionAccessStrategy; import org.hibernate.cache.access.EntityRegionAccessStrategy;
import org.hibernate.cache.entry.CacheEntry; import org.hibernate.cache.entry.CacheEntry;
@ -1413,16 +1413,16 @@ public abstract class AbstractEntityPersister
Object object, Object object,
LockMode lockMode, LockMode lockMode,
SessionImplementor session) throws HibernateException { SessionImplementor session) throws HibernateException {
getLocker( lockMode ).lock( id, version, object, LockRequest.WAIT_FOREVER, session ); getLocker( lockMode ).lock( id, version, object, LockOptions.WAIT_FOREVER, session );
} }
public void lock( public void lock(
Serializable id, Serializable id,
Object version, Object version,
Object object, Object object,
LockRequest lockRequest, LockOptions lockOptions,
SessionImplementor session) throws HibernateException { SessionImplementor session) throws HibernateException {
getLocker( lockRequest.getLockMode() ).lock( id, version, object, lockRequest.getTimeOut(), session ); getLocker( lockOptions.getLockMode() ).lock( id, version, object, lockOptions.getTimeOut(), session );
} }
public String getRootTableName() { public String getRootTableName() {
@ -1882,13 +1882,13 @@ public abstract class AbstractEntityPersister
} }
protected UniqueEntityLoader createEntityLoader( protected UniqueEntityLoader createEntityLoader(
LockRequest lockRequest, LockOptions lockOptions,
LoadQueryInfluencers loadQueryInfluencers) throws MappingException { LoadQueryInfluencers loadQueryInfluencers) throws MappingException {
//TODO: disable batch loading if lockMode > READ? //TODO: disable batch loading if lockMode > READ?
return BatchingEntityLoader.createBatchingEntityLoader( return BatchingEntityLoader.createBatchingEntityLoader(
this, this,
batchSize, batchSize,
lockRequest, lockOptions,
getFactory(), getFactory(),
loadQueryInfluencers loadQueryInfluencers
); );
@ -3208,7 +3208,7 @@ public abstract class AbstractEntityPersister
); );
} }
final UniqueEntityLoader loader = getAppropriateLoader( new LockRequest().setLockMode(lockMode), session ); final UniqueEntityLoader loader = getAppropriateLoader( new LockOptions().setLockMode(lockMode), session );
return loader.load( id, optionalObject, session ); return loader.load( id, optionalObject, session );
} }
@ -3216,7 +3216,7 @@ public abstract class AbstractEntityPersister
* Load an instance using either the <tt>forUpdateLoader</tt> or the outer joining <tt>loader</tt>, * Load an instance using either the <tt>forUpdateLoader</tt> or the outer joining <tt>loader</tt>,
* depending upon the value of the <tt>lock</tt> parameter * depending upon the value of the <tt>lock</tt> parameter
*/ */
public Object load(Serializable id, Object optionalObject, LockRequest lockRequest, SessionImplementor session) public Object load(Serializable id, Object optionalObject, LockOptions lockOptions, SessionImplementor session)
throws HibernateException { throws HibernateException {
if ( log.isTraceEnabled() ) { if ( log.isTraceEnabled() ) {
@ -3226,7 +3226,7 @@ public abstract class AbstractEntityPersister
); );
} }
final UniqueEntityLoader loader = getAppropriateLoader( lockRequest, session ); final UniqueEntityLoader loader = getAppropriateLoader(lockOptions, session );
return loader.load( id, optionalObject, session ); return loader.load( id, optionalObject, session );
} }
@ -3249,7 +3249,7 @@ public abstract class AbstractEntityPersister
&& filterHelper.isAffectedBy( session.getLoadQueryInfluencers().getEnabledFilters() ); && filterHelper.isAffectedBy( session.getLoadQueryInfluencers().getEnabledFilters() );
} }
private UniqueEntityLoader getAppropriateLoader(LockRequest lockRequest, SessionImplementor session) { private UniqueEntityLoader getAppropriateLoader(LockOptions lockOptions, SessionImplementor session) {
if ( queryLoader != null ) { if ( queryLoader != null ) {
// if the user specified a custom query loader we need to that // if the user specified a custom query loader we need to that
// regardless of any other consideration // regardless of any other consideration
@ -3258,9 +3258,9 @@ public abstract class AbstractEntityPersister
else if ( isAffectedByEnabledFilters( session ) ) { else if ( isAffectedByEnabledFilters( session ) ) {
// because filters affect the rows returned (because they add // because filters affect the rows returned (because they add
// restirctions) these need to be next in precendence // restirctions) these need to be next in precendence
return createEntityLoader( lockRequest, session.getLoadQueryInfluencers() ); return createEntityLoader(lockOptions, session.getLoadQueryInfluencers() );
} }
else if ( session.getLoadQueryInfluencers().getInternalFetchProfile() != null && LockMode.UPGRADE.greaterThan( lockRequest.getLockMode() ) ) { else if ( session.getLoadQueryInfluencers().getInternalFetchProfile() != null && LockMode.UPGRADE.greaterThan( lockOptions.getLockMode() ) ) {
// Next, we consider whether an 'internal' fetch profile has been set. // Next, we consider whether an 'internal' fetch profile has been set.
// This indicates a special fetch profile Hibernate needs applied // This indicates a special fetch profile Hibernate needs applied
// (for its merge loading process e.g.). // (for its merge loading process e.g.).
@ -3269,10 +3269,10 @@ public abstract class AbstractEntityPersister
else if ( isAffectedByEnabledFetchProfiles( session ) ) { else if ( isAffectedByEnabledFetchProfiles( session ) ) {
// If the session has associated influencers we need to adjust the // If the session has associated influencers we need to adjust the
// SQL query used for loading based on those influencers // SQL query used for loading based on those influencers
return createEntityLoader( lockRequest, session.getLoadQueryInfluencers() ); return createEntityLoader(lockOptions, session.getLoadQueryInfluencers() );
} }
else { else {
return ( UniqueEntityLoader ) loaders.get( lockRequest.getLockMode() ); return ( UniqueEntityLoader ) loaders.get( lockOptions.getLockMode() );
} }
} }

View File

@ -31,7 +31,7 @@ import org.hibernate.HibernateException;
import org.hibernate.LockMode; import org.hibernate.LockMode;
import org.hibernate.MappingException; import org.hibernate.MappingException;
import org.hibernate.EntityMode; import org.hibernate.EntityMode;
import org.hibernate.LockRequest; import org.hibernate.LockOptions;
import org.hibernate.tuple.entity.EntityMetamodel; import org.hibernate.tuple.entity.EntityMetamodel;
import org.hibernate.cache.OptimisticCacheSource; import org.hibernate.cache.OptimisticCacheSource;
import org.hibernate.cache.access.EntityRegionAccessStrategy; import org.hibernate.cache.access.EntityRegionAccessStrategy;
@ -330,7 +330,7 @@ public interface EntityPersister extends OptimisticCacheSource {
/** /**
* Load an instance of the persistent class. * Load an instance of the persistent class.
*/ */
public Object load(Serializable id, Object optionalObject, LockRequest lockRequest, SessionImplementor session) public Object load(Serializable id, Object optionalObject, LockOptions lockOptions, SessionImplementor session)
throws HibernateException; throws HibernateException;
/** /**
@ -342,7 +342,7 @@ public interface EntityPersister extends OptimisticCacheSource {
/** /**
* Do a version check (optional operation) * Do a version check (optional operation)
*/ */
public void lock(Serializable id, Object version, Object object, LockRequest lockRequest, SessionImplementor session) public void lock(Serializable id, Object version, Object object, LockOptions lockOptions, SessionImplementor session)
throws HibernateException; throws HibernateException;
/** /**

View File

@ -32,7 +32,7 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import org.hibernate.LockMode; import org.hibernate.LockMode;
import org.hibernate.LockRequest; import org.hibernate.LockOptions;
import org.hibernate.type.Type; import org.hibernate.type.Type;
public final class ArrayHelper { public final class ArrayHelper {
@ -84,9 +84,9 @@ public final class ArrayHelper {
return array; return array;
} }
public static LockMode[] fillArray(LockRequest lockRequest, int length) { public static LockMode[] fillArray(LockOptions lockOptions, int length) {
LockMode[] array = new LockMode[length]; LockMode[] array = new LockMode[length];
Arrays.fill(array, lockRequest); Arrays.fill(array, lockOptions);
return array; return array;
} }

View File

@ -29,7 +29,6 @@ import java.io.ObjectOutputStream;
import java.io.Serializable; import java.io.Serializable;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.HashMap;
import javax.persistence.EntityNotFoundException; import javax.persistence.EntityNotFoundException;
import javax.persistence.EntityTransaction; import javax.persistence.EntityTransaction;
import javax.persistence.FlushModeType; import javax.persistence.FlushModeType;
@ -521,7 +520,7 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
if ( !contains( entity ) ) { if ( !contains( entity ) ) {
throw new IllegalArgumentException( "entity not in the persistence context" ); throw new IllegalArgumentException( "entity not in the persistence context" );
} }
getSession().lock( entity, getLockRequest(lockModeType, properties) ); getSession().buildLockRequest(getLockRequest(lockModeType, properties)).lock( entity );
} }
catch ( HibernateException he ) { catch ( HibernateException he ) {
throw convert( he ); throw convert( he );
@ -529,31 +528,31 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
} }
private LockRequest getLockRequest(LockModeType lockModeType, Map<String, Object> properties) { private LockOptions getLockRequest(LockModeType lockModeType, Map<String, Object> properties) {
LockRequest lockRequest = new LockRequest(); LockOptions lockOptions = new LockOptions();
lockRequest.setLockMode(getLockMode(lockModeType)); lockOptions.setLockMode(getLockMode(lockModeType));
if ( properties != null ) { if ( properties != null ) {
// lockRequest scope will default to false (PessimisticLockScope.NORMAL) // lockOptions scope will default to false (PessimisticLockScope.NORMAL)
Object value = properties.get(PESSIMISTICLOCKSCOPE); Object value = properties.get(PESSIMISTICLOCKSCOPE);
if ( value instanceof String && PessimisticLockScope.valueOf((String) value) == PessimisticLockScope.EXTENDED) { if ( value instanceof String && PessimisticLockScope.valueOf((String) value) == PessimisticLockScope.EXTENDED) {
lockRequest.setScope(true); lockOptions.setScope(true);
} }
// lockRequest timeout will default to LockRequest.FOREVER_WAIT // lockOptions timeout will default to LockOptions.FOREVER_WAIT
value = properties.get(PESSIMISTICLOCKTIMEOUT); value = properties.get(PESSIMISTICLOCKTIMEOUT);
if ( value instanceof String ) { if ( value instanceof String ) {
int timeout = Integer.parseInt((String) value); int timeout = Integer.parseInt((String) value);
if ( timeout < 0 ) { if ( timeout < 0 ) {
lockRequest.setTimeOut(LockRequest.WAIT_FOREVER); lockOptions.setTimeOut(LockOptions.WAIT_FOREVER);
} }
else if( timeout == 0 ) { else if( timeout == 0 ) {
lockRequest.setTimeOut(LockRequest.NO_WAIT); lockOptions.setTimeOut(LockOptions.NO_WAIT);
} }
else { else {
lockRequest.setTimeOut(timeout); lockOptions.setTimeOut(timeout);
} }
} }
} }
return lockRequest; return lockOptions;
} }
private LockModeType getLockModeType(LockMode lockMode) { private LockModeType getLockModeType(LockMode lockMode) {

View File

@ -11,9 +11,8 @@ import org.hibernate.Hibernate;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
import org.hibernate.LockMode; import org.hibernate.LockMode;
import org.hibernate.MappingException; import org.hibernate.MappingException;
import org.hibernate.LockRequest; import org.hibernate.LockOptions;
import org.hibernate.tuple.entity.EntityMetamodel; import org.hibernate.tuple.entity.EntityMetamodel;
import org.hibernate.cache.CacheConcurrencyStrategy;
import org.hibernate.cache.access.EntityRegionAccessStrategy; import org.hibernate.cache.access.EntityRegionAccessStrategy;
import org.hibernate.cache.entry.CacheEntryStructure; import org.hibernate.cache.entry.CacheEntryStructure;
import org.hibernate.cache.entry.UnstructuredCacheEntry; import org.hibernate.cache.entry.UnstructuredCacheEntry;
@ -281,15 +280,15 @@ public class CustomPersister implements EntityPersister {
} }
/** /**
* @see EntityPersister#load(Serializable, Object, LockRequest, SessionImplementor) * @see EntityPersister#load(Serializable, Object, org.hibernate.LockOptions , SessionImplementor)
*/ */
public Object load( public Object load(
Serializable id, Serializable id,
Object optionalObject, Object optionalObject,
LockRequest lockRequest, LockOptions lockOptions,
SessionImplementor session SessionImplementor session
) throws HibernateException { ) throws HibernateException {
return load(id, optionalObject, lockRequest.getLockMode(), session); return load(id, optionalObject, lockOptions.getLockMode(), session);
} }
/** /**
@ -343,7 +342,7 @@ public class CustomPersister implements EntityPersister {
Serializable id, Serializable id,
Object version, Object version,
Object object, Object object,
LockRequest lockRequest, LockOptions lockOptions,
SessionImplementor session SessionImplementor session
) throws HibernateException { ) throws HibernateException {