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 LocklockLock 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 voidaddStateManagerToCache(DNStateManager sm)Method to add the object managed by the specified StateManager to the (L1) cache.voidattachObject(DNStateManager ownerSM, Object pc, boolean sco)Method to attach a persistent detached object.ObjectattachObjectCopy(DNStateManager ownerSM, Object pc, boolean sco)Method to attach a persistent detached object returning an attached copy of the object.voidclearDirty()Method to clear all objects marked as dirty (whether directly or indirectly).voidclearDirty(DNStateManager sm)Method to clear an object from the list of dirty objects.voidclose()Method to close the context.voiddeleteObject(Object obj)Method to delete an object from the datastore.voiddeleteObjects(Object... objs)Method to delete an array of objects from the datastore.voiddetachObject(FetchPlanState state, Object obj)Method to detach a persistent object without making a copy.ObjectdetachObjectCopy(Object pc, FetchPlanState state)voiddetachObjects(FetchPlanState state, Object... objs)Method to detach the passed object(s).voidenlistInTransaction(DNStateManager sm)Method to enlist the specified StateManager in the current transaction.voidevictAllObjects()Method to evict all current objects from L1 cache.voidevictFromTransaction(DNStateManager sm)Method to evict the specified StateManager from the current transaction.voidevictObject(Object obj)Internal method to evict an object from L1 cache.voidevictObjects(Class cls, boolean subclasses)Method to evict all objects of the specified type (and optionally its subclasses) that are present in the L1 cache.DNStateManagerfindStateManager(Object pc)Method to return StateManager for an object (if managed).voidflush()Method callable from external APIs for user-management of flushing.voidflushInternal(boolean flushToDatastore)This method flushes all dirty, new, and deleted instances to the datastore.ExtentgetExtent(Class pcClass, boolean subclasses)Extents are collections of datastore objects managed by the datastore, not by explicit user operations on collections.booleangetMultithreaded()Accessor for whether the usage is multi-threaded.List<DNStateManager>getObjectsToBeFlushed()Convenience method to inspect the list of objects with outstanding changes to flush.voidmakeObjectTransactional(Object obj)Method to make an object transactional.voidmakeObjectTransient(Object obj, FetchPlanState state)Method to migrate an object to transient state.voidmarkDirty(DNStateManager sm, boolean directUpdate)Method to mark an object (StateManager) as dirty.ObjectpersistObject(Object obj, boolean merging)Method to make an object persistent.Object[]persistObjects(Object... objs)Method to persist an array of objects to the datastore.voidpostBegin()Method to perform any post-begin checks.voidpostCommit()Commit any changes made to objects managed by the object manager to the database.voidpostRollback()Callback invoked after the actual datastore rollback.voidpreCommit()Method to perform any pre-commit checks.voidpreRollback()Rollback any changes made to objects managed by the object manager to the database.voidprocessNontransactionalUpdate()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).voidrefreshAllObjects()Method to do a refresh of all objects.voidrefreshObject(Object obj)Method to do a refresh of an object, updating it from its datastore representation.voidremoveStateManagerFromCache(DNStateManager sm)Method to remove the object managed by the specified StateManager from the cache.voidreplaceObjectId(Persistable pc, Object oldID, Object newID)Replace the previous object id for a persistable object with a new one.voidretrieveObjects(boolean useFetchPlan, Object... pcs)Method to retrieve the (fields of the) passed object(s).voidthreadLock()Method to lock this ExecutionContext for threadingvoidthreadUnlock()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:ExecutionContextMethod to lock this ExecutionContext for threading
-
threadUnlock
public void threadUnlock()
Description copied from interface:ExecutionContextMethod to unlock this ExecutionContext for threading
-
getMultithreaded
public boolean getMultithreaded()
Accessor for whether the usage is multi-threaded.- Specified by:
getMultithreadedin interfaceExecutionContext- Overrides:
getMultithreadedin classExecutionContextImpl- Returns:
- True
-
processNontransactionalUpdate
public void processNontransactionalUpdate()
Description copied from class:ExecutionContextImplMethod 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:
processNontransactionalUpdatein interfaceExecutionContext- Overrides:
processNontransactionalUpdatein classExecutionContextImpl
-
enlistInTransaction
public void enlistInTransaction(DNStateManager sm)
Description copied from interface:ExecutionContextMethod to enlist the specified StateManager in the current transaction.- Specified by:
enlistInTransactionin interfaceExecutionContext- Overrides:
enlistInTransactionin classExecutionContextImpl- Parameters:
sm- StateManager
-
evictFromTransaction
public void evictFromTransaction(DNStateManager sm)
Description copied from interface:ExecutionContextMethod to evict the specified StateManager from the current transaction.- Specified by:
evictFromTransactionin interfaceExecutionContext- Overrides:
evictFromTransactionin classExecutionContextImpl- Parameters:
sm- StateManager
-
addStateManagerToCache
public void addStateManagerToCache(DNStateManager sm)
Description copied from class:ExecutionContextImplMethod to add the object managed by the specified StateManager to the (L1) cache.- Specified by:
addStateManagerToCachein interfaceExecutionContext- Overrides:
addStateManagerToCachein classExecutionContextImpl- Parameters:
sm- StateManager
-
removeStateManagerFromCache
public void removeStateManagerFromCache(DNStateManager sm)
Description copied from class:ExecutionContextImplMethod to remove the object managed by the specified StateManager from the cache.- Specified by:
removeStateManagerFromCachein interfaceExecutionContext- Overrides:
removeStateManagerFromCachein classExecutionContextImpl- Parameters:
sm- StateManager
-
findStateManager
public DNStateManager findStateManager(Object pc)
Description copied from class:ExecutionContextImplMethod to return StateManager for an object (if managed).- Specified by:
findStateManagerin interfaceExecutionContext- Overrides:
findStateManagerin classExecutionContextImpl- Parameters:
pc- The object we are checking- Returns:
- StateManager, null if not found.
-
close
public void close()
Description copied from class:ExecutionContextImplMethod to close the context.- Specified by:
closein interfaceExecutionContext- Overrides:
closein classExecutionContextImpl
-
evictObject
public void evictObject(Object obj)
Description copied from class:ExecutionContextImplInternal method to evict an object from L1 cache.- Specified by:
evictObjectin interfaceExecutionContext- Overrides:
evictObjectin classExecutionContextImpl- Parameters:
obj- The object
-
refreshObject
public void refreshObject(Object obj)
Description copied from interface:ExecutionContextMethod to do a refresh of an object, updating it from its datastore representation. Also updates the object in the L1/L2 caches.- Specified by:
refreshObjectin interfaceExecutionContext- Overrides:
refreshObjectin classExecutionContextImpl- Parameters:
obj- The object
-
retrieveObjects
public void retrieveObjects(boolean useFetchPlan, Object... pcs)Description copied from interface:ExecutionContextMethod to retrieve the (fields of the) passed object(s).- Specified by:
retrieveObjectsin interfaceExecutionContext- Overrides:
retrieveObjectsin classExecutionContextImpl- Parameters:
useFetchPlan- Whether to retrieve the current fetch planpcs- The objects
-
persistObject
public Object persistObject(Object obj, boolean merging)
Description copied from interface:ExecutionContextMethod to make an object persistent. Should be called by EXTERNAL APIs (JDO/JPA) only. All INTERNAL (DataNucleus) calls should go via persistObjectInternal(...).- Specified by:
persistObjectin interfaceExecutionContext- Overrides:
persistObjectin 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:ExecutionContextMethod to persist an array of objects to the datastore. Should be called by EXTERNAL APIs (JDO/JPA) only.- Specified by:
persistObjectsin interfaceExecutionContext- Overrides:
persistObjectsin classExecutionContextImpl- Parameters:
objs- The objects to persist- Returns:
- The persisted objects
-
deleteObject
public void deleteObject(Object obj)
Description copied from interface:ExecutionContextMethod to delete an object from the datastore. NOT to be called by internal methods. Only callable by external APIs (JDO/JPA).- Specified by:
deleteObjectin interfaceExecutionContext- Overrides:
deleteObjectin classExecutionContextImpl- Parameters:
obj- The object
-
deleteObjects
public void deleteObjects(Object... objs)
Description copied from interface:ExecutionContextMethod to delete an array of objects from the datastore.- Specified by:
deleteObjectsin interfaceExecutionContext- Overrides:
deleteObjectsin classExecutionContextImpl- Parameters:
objs- The objects to delete
-
makeObjectTransient
public void makeObjectTransient(Object obj, FetchPlanState state)
Description copied from interface:ExecutionContextMethod to migrate an object to transient state.- Specified by:
makeObjectTransientin interfaceExecutionContext- Overrides:
makeObjectTransientin 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:ExecutionContextMethod to make an object transactional.- Specified by:
makeObjectTransactionalin interfaceExecutionContext- Overrides:
makeObjectTransactionalin classExecutionContextImpl- Parameters:
obj- The object
-
attachObject
public void attachObject(DNStateManager ownerSM, Object pc, boolean sco)
Description copied from interface:ExecutionContextMethod 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:
attachObjectin interfaceExecutionContext- Overrides:
attachObjectin 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:ExecutionContextMethod 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:
attachObjectCopyin interfaceExecutionContext- Overrides:
attachObjectCopyin 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:ExecutionContextMethod 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:
detachObjectin interfaceExecutionContext- Overrides:
detachObjectin classExecutionContextImpl- Parameters:
state- State for the detachment processobj- The object
-
detachObjects
public void detachObjects(FetchPlanState state, Object... objs)
Description copied from interface:ExecutionContextMethod to detach the passed object(s).- Specified by:
detachObjectsin interfaceExecutionContext- Overrides:
detachObjectsin 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:ExecutionContextImplMethod to clear an object from the list of dirty objects.- Specified by:
clearDirtyin interfaceExecutionContext- Overrides:
clearDirtyin classExecutionContextImpl- Parameters:
sm- StateManager
-
clearDirty
public void clearDirty()
Description copied from class:ExecutionContextImplMethod to clear all objects marked as dirty (whether directly or indirectly).- Specified by:
clearDirtyin interfaceExecutionContext- Overrides:
clearDirtyin classExecutionContextImpl
-
evictAllObjects
public void evictAllObjects()
Method to evict all current objects from L1 cache.- Specified by:
evictAllObjectsin interfaceExecutionContext- Overrides:
evictAllObjectsin classExecutionContextImpl
-
markDirty
public void markDirty(DNStateManager sm, boolean directUpdate)
Description copied from class:ExecutionContextImplMethod to mark an object (StateManager) as dirty.- Specified by:
markDirtyin interfaceExecutionContext- Overrides:
markDirtyin 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:ExecutionContextImplMethod 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:
flushin interfaceExecutionContext- Overrides:
flushin classExecutionContextImpl
-
flushInternal
public void flushInternal(boolean flushToDatastore)
Description copied from class:ExecutionContextImplThis method flushes all dirty, new, and deleted instances to the datastore.- Specified by:
flushInternalin interfaceExecutionContext- Overrides:
flushInternalin 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:ExecutionContextImplReplace 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:
replaceObjectIdin interfaceExecutionContext- Overrides:
replaceObjectIdin 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:ExecutionContextImplExtents 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:
getExtentin interfaceExecutionContext- Overrides:
getExtentin 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:ExecutionContextImplMethod to evict all objects of the specified type (and optionally its subclasses) that are present in the L1 cache.- Specified by:
evictObjectsin interfaceExecutionContext- Overrides:
evictObjectsin classExecutionContextImpl- Parameters:
cls- Type of persistable objectsubclasses- Whether to include subclasses
-
refreshAllObjects
public void refreshAllObjects()
Description copied from interface:ExecutionContextMethod to do a refresh of all objects.- Specified by:
refreshAllObjectsin interfaceExecutionContext- Overrides:
refreshAllObjectsin classExecutionContextImpl
-
getObjectsToBeFlushed
public List<DNStateManager> getObjectsToBeFlushed()
Description copied from class:ExecutionContextImplConvenience method to inspect the list of objects with outstanding changes to flush.- Specified by:
getObjectsToBeFlushedin interfaceExecutionContext- Overrides:
getObjectsToBeFlushedin classExecutionContextImpl- Returns:
- StateManagers for the objects to be flushed.
-
postBegin
public void postBegin()
Description copied from class:ExecutionContextImplMethod to perform any post-begin checks.- Overrides:
postBeginin classExecutionContextImpl
-
preCommit
public void preCommit()
Description copied from class:ExecutionContextImplMethod to perform any pre-commit checks.- Overrides:
preCommitin classExecutionContextImpl
-
postCommit
public void postCommit()
Description copied from class:ExecutionContextImplCommit any changes made to objects managed by the object manager to the database.- Overrides:
postCommitin classExecutionContextImpl
-
preRollback
public void preRollback()
Description copied from class:ExecutionContextImplRollback any changes made to objects managed by the object manager to the database.- Overrides:
preRollbackin classExecutionContextImpl
-
postRollback
public void postRollback()
Description copied from class:ExecutionContextImplCallback invoked after the actual datastore rollback.- Overrides:
postRollbackin classExecutionContextImpl
-
-