Package org.datanucleus
Class ExecutionContextThreadedImpl
- java.lang.Object
-
- org.datanucleus.ExecutionContextImpl
-
- org.datanucleus.ExecutionContextThreadedImpl
-
- All Implemented Interfaces:
ExecutionContextReference
,ExecutionContext
,TransactionEventListener
public class ExecutionContextThreadedImpl extends ExecutionContextImpl
ExecutionContext to attempt to handle multi-threaded PM/EM cases. Intercepts various methods in an attempt to prevent conflicting thread updates by locking the current thread. Note we could have just put this code in ExecutionContextImpl but better to split it out since the majority use-case is to have a non-thread-safe PM/EM. Note also that having thread-safe ExecutionContext usage depends on much more than having this class, since SCO wrappers would need to coordinate with such locks, as would the Transaction for the ExecutionContext. TODO Evaluate all of the places we currently lock (when multithreaded) to find corner cases not caught. TODO We currently try to lock the ExecutionContext and not individual objects. To lock individual objects we would need a Lock object on the DNStateManagerThis class *tries to be* thread-safe, but there is no guarantee. You are better advised to design your application to use PM/EM for a single thread.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.datanucleus.ExecutionContextImpl
ExecutionContextImpl.NullCallbackHandler
-
Nested classes/interfaces inherited from interface org.datanucleus.ExecutionContext
ExecutionContext.EmbeddedOwnerRelation, ExecutionContext.LifecycleListener
-
-
Field Summary
Fields Modifier and Type Field Description protected Lock
lock
Lock object to lock to the current thread, and then release when the operation is complete.-
Fields inherited from class org.datanucleus.ExecutionContextImpl
cache, stateManagerAssociatedValuesMap
-
Fields inherited from interface org.datanucleus.ExecutionContext
OPTION_JTA_AUTOJOIN, OPTION_PASSWORD, OPTION_USERNAME
-
-
Constructor Summary
Constructors Constructor Description ExecutionContextThreadedImpl(PersistenceNucleusContext ctx, Object owner, Map<String,Object> options)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addStateManagerToCache(DNStateManager sm)
Method to add the object managed by the specified StateManager to the (L1) cache.void
attachObject(DNStateManager ownerSM, Object pc, boolean sco)
Method to attach a persistent detached object.Object
attachObjectCopy(DNStateManager ownerSM, Object pc, boolean sco)
Method to attach a persistent detached object returning an attached copy of the object.void
clearDirty()
Method to clear all objects marked as dirty (whether directly or indirectly).void
clearDirty(DNStateManager sm)
Method to clear an object from the list of dirty objects.void
close()
Method to close the context.void
deleteObject(Object obj)
Method to delete an object from the datastore.void
deleteObjects(Object... objs)
Method to delete an array of objects from the datastore.void
detachObject(FetchPlanState state, Object obj)
Method to detach a persistent object without making a copy.Object
detachObjectCopy(Object pc, FetchPlanState state)
void
detachObjects(FetchPlanState state, Object... objs)
Method to detach the passed object(s).void
enlistInTransaction(DNStateManager sm)
Method to enlist the specified StateManager in the current transaction.void
evictAllObjects()
Method to evict all current objects from L1 cache.void
evictFromTransaction(DNStateManager sm)
Method to evict the specified StateManager from the current transaction.void
evictObject(Object obj)
Internal method to evict an object from L1 cache.void
evictObjects(Class cls, boolean subclasses)
Method to evict all objects of the specified type (and optionally its subclasses) that are present in the L1 cache.DNStateManager
findStateManager(Object pc)
Method to return StateManager for an object (if managed).void
flush()
Method callable from external APIs for user-management of flushing.void
flushInternal(boolean flushToDatastore)
This method flushes all dirty, new, and deleted instances to the datastore.Extent
getExtent(Class pcClass, boolean subclasses)
Extents are collections of datastore objects managed by the datastore, not by explicit user operations on collections.boolean
getMultithreaded()
Accessor for whether the usage is multi-threaded.List<DNStateManager>
getObjectsToBeFlushed()
Convenience method to inspect the list of objects with outstanding changes to flush.void
makeObjectTransactional(Object obj)
Method to make an object transactional.void
makeObjectTransient(Object obj, FetchPlanState state)
Method to migrate an object to transient state.void
markDirty(DNStateManager sm, boolean directUpdate)
Method to mark an object (StateManager) as dirty.Object
persistObject(Object obj, boolean merging)
Method to make an object persistent.Object[]
persistObjects(Object... objs)
Method to persist an array of objects to the datastore.void
postBegin()
Method to perform any post-begin checks.void
postCommit()
Commit any changes made to objects managed by the object manager to the database.void
postRollback()
Callback invoked after the actual datastore rollback.void
preCommit()
Method to perform any pre-commit checks.void
preRollback()
Rollback any changes made to objects managed by the object manager to the database.void
processNontransactionalUpdate()
Method called when a non-tx update has been performed (via setter call on the persistable object, or via use of mutator methods of a field).void
refreshAllObjects()
Method to do a refresh of all objects.void
refreshObject(Object obj)
Method to do a refresh of an object, updating it from its datastore representation.void
removeStateManagerFromCache(DNStateManager sm)
Method to remove the object managed by the specified StateManager from the cache.void
replaceObjectId(Persistable pc, Object oldID, Object newID)
Replace the previous object id for a persistable object with a new one.void
retrieveObjects(boolean useFetchPlan, Object... pcs)
Method to retrieve the (fields of the) passed object(s).void
threadLock()
Method to lock this ExecutionContext for threadingvoid
threadUnlock()
Method to unlock this ExecutionContext for threading-
Methods inherited from class org.datanucleus.ExecutionContextImpl
acquireThreadContextInfo, addInternalFetchGroup, addOperationToQueue, assertActiveTransaction, assertClassPersistable, assertDetachable, assertIsOpen, assertNotDetached, closeCallbackHandler, containsStateManagerAssociatedValue, deleteObjectInternal, deregisterEmbeddedRelation, deregisterExecutionContextListener, detachAll, detachObjectCopy, exists, findObject, findObject, findObject, findObject, findObjectByUnique, findObjects, findObjectsById, findStateManager, findStateManagerForEmbedded, findStateManagerOfOwnerForAttachingObject, flushOperationsForBackingStore, getAttachDetachReferencedObject, getAttachedObjectForId, getBooleanProperty, getCallbackHandler, getClassLoaderResolver, getCurrentUser, getEmbeddedInformationForOwner, getFetchGroupManager, getFetchGroupsWithName, getFetchPlan, getFlushMode, getInternalFetchGroup, getIntProperty, getL2CacheableObject, getLevel1Cache, getLevel2CacheRetrieveMode, getLevel2CacheStoreMode, getLockManager, getManagedObjects, getManagedObjects, getManagedObjects, getManagedObjects, getManageRelations, getNucleusContext, getObjectFromCache, getObjectFromLevel1Cache, getObjectFromLevel2Cache, getObjectFromLevel2CacheForUnique, getObjectsFromCache, getObjectsFromLevel2Cache, getOperationQueue, getOwner, getOwnerForEmbeddedStateManager, getOwnerInformationForEmbedded, getProperties, getProperty, getRelationshipManager, getSerializeReadForClass, getStateManagerAssociatedValue, getStatistics, getStringProperty, getSupportedProperties, getTenantId, getThreadContextInfo, getTransaction, hasIdentityInCache, hasPersistenceInformationForClass, initialise, initialiseLevel1Cache, isClosed, isDelayDatastoreOperationsEnabled, isEnlistedInTransaction, isFlushing, isInserting, isManagingRelations, isObjectModifiedInTransaction, isRunningDetachAllOnCommit, makeObjectNontransactional, markFieldsForUpdateInLevel2Cache, newInstance, newObjectId, newObjectId, operationQueueIsActive, persistObjectInternal, processNontransactionalAtomicChanges, putObjectIntoLevel1Cache, putObjectIntoLevel2Cache, putObjectIntoLevel2CacheInternal, putObjectsIntoLevel2Cache, registerEmbeddedRelation, registerExecutionContextListener, releaseThreadContextInfo, removeEmbeddedOwnerRelation, removeInternalFetchGroup, removeObjectFromLevel1Cache, removeObjectFromLevel2Cache, removeStateManagerAssociatedValue, setAttachDetachReferencedObject, setLevel2Cache, setProperties, setProperty, setStateManagerAssociatedValue, transactionCommitted, transactionEnded, transactionFlushed, transactionPreCommit, transactionPreFlush, transactionPreRollBack, transactionReleaseSavepoint, transactionRollbackToSavepoint, transactionRolledBack, transactionSetSavepoint, transactionStarted
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.datanucleus.ExecutionContext
getApiAdapter, getMetaDataManager, getStoreManager, getTypeManager, persistObjectInternal
-
-
-
-
Field Detail
-
lock
protected Lock lock
Lock object to lock to the current thread, and then release when the operation is complete.
-
-
Constructor Detail
-
ExecutionContextThreadedImpl
public ExecutionContextThreadedImpl(PersistenceNucleusContext ctx, Object owner, Map<String,Object> options)
- Parameters:
ctx
- NucleusContextowner
- Owner object (PM, EM)options
- Any options affecting startup
-
-
Method Detail
-
threadLock
public void threadLock()
Description copied from interface:ExecutionContext
Method to lock this ExecutionContext for threading
-
threadUnlock
public void threadUnlock()
Description copied from interface:ExecutionContext
Method to unlock this ExecutionContext for threading
-
getMultithreaded
public boolean getMultithreaded()
Accessor for whether the usage is multi-threaded.- Specified by:
getMultithreaded
in interfaceExecutionContext
- Overrides:
getMultithreaded
in classExecutionContextImpl
- Returns:
- True
-
processNontransactionalUpdate
public void processNontransactionalUpdate()
Description copied from class:ExecutionContextImpl
Method called when a non-tx update has been performed (via setter call on the persistable object, or via use of mutator methods of a field). Only hands the update across to be "committed" if not part of an owning persist/delete call.- Specified by:
processNontransactionalUpdate
in interfaceExecutionContext
- Overrides:
processNontransactionalUpdate
in classExecutionContextImpl
-
enlistInTransaction
public void enlistInTransaction(DNStateManager sm)
Description copied from interface:ExecutionContext
Method to enlist the specified StateManager in the current transaction.- Specified by:
enlistInTransaction
in interfaceExecutionContext
- Overrides:
enlistInTransaction
in classExecutionContextImpl
- Parameters:
sm
- StateManager
-
evictFromTransaction
public void evictFromTransaction(DNStateManager sm)
Description copied from interface:ExecutionContext
Method to evict the specified StateManager from the current transaction.- Specified by:
evictFromTransaction
in interfaceExecutionContext
- Overrides:
evictFromTransaction
in classExecutionContextImpl
- Parameters:
sm
- StateManager
-
addStateManagerToCache
public void addStateManagerToCache(DNStateManager sm)
Description copied from class:ExecutionContextImpl
Method to add the object managed by the specified StateManager to the (L1) cache.- Specified by:
addStateManagerToCache
in interfaceExecutionContext
- Overrides:
addStateManagerToCache
in classExecutionContextImpl
- Parameters:
sm
- StateManager
-
removeStateManagerFromCache
public void removeStateManagerFromCache(DNStateManager sm)
Description copied from class:ExecutionContextImpl
Method to remove the object managed by the specified StateManager from the cache.- Specified by:
removeStateManagerFromCache
in interfaceExecutionContext
- Overrides:
removeStateManagerFromCache
in classExecutionContextImpl
- Parameters:
sm
- StateManager
-
findStateManager
public DNStateManager findStateManager(Object pc)
Description copied from class:ExecutionContextImpl
Method to return StateManager for an object (if managed).- Specified by:
findStateManager
in interfaceExecutionContext
- Overrides:
findStateManager
in classExecutionContextImpl
- Parameters:
pc
- The object we are checking- Returns:
- StateManager, null if not found.
-
close
public void close()
Description copied from class:ExecutionContextImpl
Method to close the context.- Specified by:
close
in interfaceExecutionContext
- Overrides:
close
in classExecutionContextImpl
-
evictObject
public void evictObject(Object obj)
Description copied from class:ExecutionContextImpl
Internal method to evict an object from L1 cache.- Specified by:
evictObject
in interfaceExecutionContext
- Overrides:
evictObject
in classExecutionContextImpl
- Parameters:
obj
- The object
-
refreshObject
public void refreshObject(Object obj)
Description copied from interface:ExecutionContext
Method to do a refresh of an object, updating it from its datastore representation. Also updates the object in the L1/L2 caches.- Specified by:
refreshObject
in interfaceExecutionContext
- Overrides:
refreshObject
in classExecutionContextImpl
- Parameters:
obj
- The object
-
retrieveObjects
public void retrieveObjects(boolean useFetchPlan, Object... pcs)
Description copied from interface:ExecutionContext
Method to retrieve the (fields of the) passed object(s).- Specified by:
retrieveObjects
in interfaceExecutionContext
- Overrides:
retrieveObjects
in classExecutionContextImpl
- Parameters:
useFetchPlan
- Whether to retrieve the current fetch planpcs
- The objects
-
persistObject
public Object persistObject(Object obj, boolean merging)
Description copied from interface:ExecutionContext
Method to make an object persistent. Should be called by EXTERNAL APIs (JDO/JPA) only. All INTERNAL (DataNucleus) calls should go via persistObjectInternal(...).- Specified by:
persistObject
in interfaceExecutionContext
- Overrides:
persistObject
in classExecutionContextImpl
- Parameters:
obj
- The objectmerging
- Whether this object (and dependents) is being merged- Returns:
- The persisted object
-
persistObjects
public Object[] persistObjects(Object... objs)
Description copied from interface:ExecutionContext
Method to persist an array of objects to the datastore. Should be called by EXTERNAL APIs (JDO/JPA) only.- Specified by:
persistObjects
in interfaceExecutionContext
- Overrides:
persistObjects
in classExecutionContextImpl
- Parameters:
objs
- The objects to persist- Returns:
- The persisted objects
-
deleteObject
public void deleteObject(Object obj)
Description copied from interface:ExecutionContext
Method to delete an object from the datastore. NOT to be called by internal methods. Only callable by external APIs (JDO/JPA).- Specified by:
deleteObject
in interfaceExecutionContext
- Overrides:
deleteObject
in classExecutionContextImpl
- Parameters:
obj
- The object
-
deleteObjects
public void deleteObjects(Object... objs)
Description copied from interface:ExecutionContext
Method to delete an array of objects from the datastore.- Specified by:
deleteObjects
in interfaceExecutionContext
- Overrides:
deleteObjects
in classExecutionContextImpl
- Parameters:
objs
- The objects to delete
-
makeObjectTransient
public void makeObjectTransient(Object obj, FetchPlanState state)
Description copied from interface:ExecutionContext
Method to migrate an object to transient state.- Specified by:
makeObjectTransient
in interfaceExecutionContext
- Overrides:
makeObjectTransient
in classExecutionContextImpl
- Parameters:
obj
- The objectstate
- Object containing the state of the fetch plan process (if any)
-
makeObjectTransactional
public void makeObjectTransactional(Object obj)
Description copied from interface:ExecutionContext
Method to make an object transactional.- Specified by:
makeObjectTransactional
in interfaceExecutionContext
- Overrides:
makeObjectTransactional
in classExecutionContextImpl
- Parameters:
obj
- The object
-
attachObject
public void attachObject(DNStateManager ownerSM, Object pc, boolean sco)
Description copied from interface:ExecutionContext
Method to attach a persistent detached object. If a different object with the same identity as this object exists in the L1 cache then an exception will be thrown.- Specified by:
attachObject
in interfaceExecutionContext
- Overrides:
attachObject
in classExecutionContextImpl
- Parameters:
ownerSM
- StateManager of the owner object that has this in a field that causes this attachpc
- The persistable objectsco
- Whether the PC object is stored without an identity (embedded/serialised)
-
attachObjectCopy
public Object attachObjectCopy(DNStateManager ownerSM, Object pc, boolean sco)
Description copied from interface:ExecutionContext
Method to attach a persistent detached object returning an attached copy of the object. If the object is of class that is not detachable, a ClassNotDetachableException will be thrown.- Specified by:
attachObjectCopy
in interfaceExecutionContext
- Overrides:
attachObjectCopy
in classExecutionContextImpl
- Parameters:
ownerSM
- StateManager of the owner object that has this in a field that causes this attachpc
- The objectsco
- Whether it has no identity (second-class object)- Returns:
- The attached object
-
detachObject
public void detachObject(FetchPlanState state, Object obj)
Description copied from interface:ExecutionContext
Method to detach a persistent object without making a copy. Note that also all the objects which are refered to from this object are detached. If the object is of class that is not detachable a ClassNotDetachableException will be thrown. If the object is not persistent a NucleusUserException is thrown.- Specified by:
detachObject
in interfaceExecutionContext
- Overrides:
detachObject
in classExecutionContextImpl
- Parameters:
state
- State for the detachment processobj
- The object
-
detachObjects
public void detachObjects(FetchPlanState state, Object... objs)
Description copied from interface:ExecutionContext
Method to detach the passed object(s).- Specified by:
detachObjects
in interfaceExecutionContext
- Overrides:
detachObjects
in classExecutionContextImpl
- Parameters:
state
- State for the detachment process.objs
- The object(s) to detach
-
detachObjectCopy
public Object detachObjectCopy(Object pc, FetchPlanState state)
-
clearDirty
public void clearDirty(DNStateManager sm)
Description copied from class:ExecutionContextImpl
Method to clear an object from the list of dirty objects.- Specified by:
clearDirty
in interfaceExecutionContext
- Overrides:
clearDirty
in classExecutionContextImpl
- Parameters:
sm
- StateManager
-
clearDirty
public void clearDirty()
Description copied from class:ExecutionContextImpl
Method to clear all objects marked as dirty (whether directly or indirectly).- Specified by:
clearDirty
in interfaceExecutionContext
- Overrides:
clearDirty
in classExecutionContextImpl
-
evictAllObjects
public void evictAllObjects()
Method to evict all current objects from L1 cache.- Specified by:
evictAllObjects
in interfaceExecutionContext
- Overrides:
evictAllObjects
in classExecutionContextImpl
-
markDirty
public void markDirty(DNStateManager sm, boolean directUpdate)
Description copied from class:ExecutionContextImpl
Method to mark an object (StateManager) as dirty.- Specified by:
markDirty
in interfaceExecutionContext
- Overrides:
markDirty
in classExecutionContextImpl
- Parameters:
sm
- StateManagerdirectUpdate
- Whether the object has had a direct update made on it (if known)
-
flush
public void flush()
Description copied from class:ExecutionContextImpl
Method callable from external APIs for user-management of flushing. Called by JDO PM.flush, or JPA EM.flush(). Performs management of relations, prior to performing internal flush of all dirty/new/deleted instances to the datastore.- Specified by:
flush
in interfaceExecutionContext
- Overrides:
flush
in classExecutionContextImpl
-
flushInternal
public void flushInternal(boolean flushToDatastore)
Description copied from class:ExecutionContextImpl
This method flushes all dirty, new, and deleted instances to the datastore.- Specified by:
flushInternal
in interfaceExecutionContext
- Overrides:
flushInternal
in classExecutionContextImpl
- Parameters:
flushToDatastore
- Whether to ensure any changes reach the datastore Otherwise they will be flushed to the datastore manager and leave it to decide the opportune moment to actually flush them to the datastore
-
replaceObjectId
public void replaceObjectId(Persistable pc, Object oldID, Object newID)
Description copied from class:ExecutionContextImpl
Replace the previous object id for a persistable object with a new one. This is used where we have already added the object to the cache(s) and/or enlisted it in the txn before its real identity was fixed (attributed in the datastore).- Specified by:
replaceObjectId
in interfaceExecutionContext
- Overrides:
replaceObjectId
in classExecutionContextImpl
- Parameters:
pc
- The Persistable objectoldID
- the old id it was known bynewID
- the new id
-
getExtent
public Extent getExtent(Class pcClass, boolean subclasses)
Description copied from class:ExecutionContextImpl
Extents are collections of datastore objects managed by the datastore, not by explicit user operations on collections. Extent capability is a boolean property of classes that are persistence capable. If an instance of a class that has a managed extent is made persistent via reachability, the instance is put into the extent implicitly.- Specified by:
getExtent
in interfaceExecutionContext
- Overrides:
getExtent
in classExecutionContextImpl
- Parameters:
pcClass
- The class to querysubclasses
- Whether to include subclasses in the query.- Returns:
- returns an Extent that contains all of the instances in the parameter class, and if the subclasses flag is true, all of the instances of the parameter class and its subclasses.
-
evictObjects
public void evictObjects(Class cls, boolean subclasses)
Description copied from class:ExecutionContextImpl
Method to evict all objects of the specified type (and optionally its subclasses) that are present in the L1 cache.- Specified by:
evictObjects
in interfaceExecutionContext
- Overrides:
evictObjects
in classExecutionContextImpl
- Parameters:
cls
- Type of persistable objectsubclasses
- Whether to include subclasses
-
refreshAllObjects
public void refreshAllObjects()
Description copied from interface:ExecutionContext
Method to do a refresh of all objects.- Specified by:
refreshAllObjects
in interfaceExecutionContext
- Overrides:
refreshAllObjects
in classExecutionContextImpl
-
getObjectsToBeFlushed
public List<DNStateManager> getObjectsToBeFlushed()
Description copied from class:ExecutionContextImpl
Convenience method to inspect the list of objects with outstanding changes to flush.- Specified by:
getObjectsToBeFlushed
in interfaceExecutionContext
- Overrides:
getObjectsToBeFlushed
in classExecutionContextImpl
- Returns:
- StateManagers for the objects to be flushed.
-
postBegin
public void postBegin()
Description copied from class:ExecutionContextImpl
Method to perform any post-begin checks.- Overrides:
postBegin
in classExecutionContextImpl
-
preCommit
public void preCommit()
Description copied from class:ExecutionContextImpl
Method to perform any pre-commit checks.- Overrides:
preCommit
in classExecutionContextImpl
-
postCommit
public void postCommit()
Description copied from class:ExecutionContextImpl
Commit any changes made to objects managed by the object manager to the database.- Overrides:
postCommit
in classExecutionContextImpl
-
preRollback
public void preRollback()
Description copied from class:ExecutionContextImpl
Rollback any changes made to objects managed by the object manager to the database.- Overrides:
preRollback
in classExecutionContextImpl
-
postRollback
public void postRollback()
Description copied from class:ExecutionContextImpl
Callback invoked after the actual datastore rollback.- Overrides:
postRollback
in classExecutionContextImpl
-
-