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. Locks various methods in an attempt to prevent conflicting thread updates. 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.This class *tries to be* thread-safe, but there is no guarantee. You are better advised to design your application to use ExecutionContextImpl for a single thread.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.datanucleus.ExecutionContext
ExecutionContext.EmbeddedOwnerRelation, ExecutionContext.LifecycleListener
-
-
Field Summary
-
Fields inherited from class org.datanucleus.ExecutionContextImpl
cache, opAssociatedValuesMapByOP
-
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
addObjectProviderToCache(ObjectProvider op)
Method to add the object managed by the specified ObjectProvider to the (L1) cache.void
attachObject(ObjectProvider ownerOP, Object pc, boolean sco)
Method to attach a persistent detached object.Object
attachObjectCopy(ObjectProvider ownerOP, 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(ObjectProvider op)
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(ObjectProvider sm)
Method to enlist the specified ObjectProvider in the current transaction.void
evictAllObjects()
Method to evict all current objects from L1 cache.void
evictFromTransaction(ObjectProvider sm)
Method to evict the specified ObjectProvider 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 optionaly its subclasses) that are present in the L1 cache.ObjectProvider
findObjectProvider(Object pc)
Method to return the ObjectProvider 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.Lock
getLock()
Accessor for the context lock object.boolean
getMultithreaded()
Accessor for whether the usage is multi-threaded.List<ObjectProvider>
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(ObjectProvider op, boolean directUpdate)
Method to mark an object (ObjectProvider) 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
removeObjectProviderFromCache(ObjectProvider op)
Method to remove the object managed by the specified ObjectProvider from the cache.void
replaceObjectId(Object pc, Object oldID, Object newID)
Replace the previous object id for a persistable object with a new one.void
retrieveObject(Object obj, boolean fgOnly)
Method to retrieve an object.-
Methods inherited from class org.datanucleus.ExecutionContextImpl
acquireThreadContextInfo, addInternalFetchGroup, addOperationToQueue, assertActiveTransaction, assertClassPersistable, assertDetachable, assertIsOpen, assertNotDetached, closeCallbackHandler, containsObjectProviderAssociatedValue, deleteObjectInternal, deregisterEmbeddedRelation, deregisterExecutionContextListener, detachAll, detachObjectCopy, exists, findObject, findObject, findObject, findObject, findObjectByUnique, findObjectProvider, findObjectProviderForEmbedded, findObjectProviderOfOwnerForAttachingObject, findObjects, findObjectsById, flushOperationsForBackingStore, getAttachDetachReferencedObject, getAttachedObjectForId, getBooleanProperty, getCallbackHandler, getClassLoaderResolver, getEmbeddedInformationForOwner, getFetchGroupManager, getFetchGroupsWithName, getFetchPlan, getFlushMode, getInternalFetchGroup, getIntProperty, getL2CacheableObject, getLevel1Cache, getLevel2CacheRetrieveMode, getLevel2CacheStoreMode, getLockManager, getManagedObjects, getManagedObjects, getManagedObjects, getManagedObjects, getManageRelations, getNucleusContext, getObjectFromCache, getObjectFromLevel1Cache, getObjectFromLevel2Cache, getObjectFromLevel2CacheForUnique, getObjectProviderAssociatedValue, getObjectsFromCache, getObjectsFromLevel2Cache, getOperationQueue, getOwner, getOwnerInformationForEmbedded, getOwnersForEmbeddedObjectProvider, getProperties, getProperty, getRelationshipManager, getSerializeReadForClass, getStatistics, getStringProperty, getSupportedProperties, getThreadContextInfo, getTransaction, hasIdentityInCache, hasPersistenceInformationForClass, initialise, initialiseLevel1Cache, isClosed, isDelayDatastoreOperationsEnabled, isEnlistedInTransaction, isFlushing, isInserting, isManagingRelations, isObjectModifiedInTransaction, isRunningDetachAllOnCommit, makeObjectNontransactional, markFieldsForUpdateInLevel2Cache, newInstance, newObjectId, newObjectId, operationQueueIsActive, persistObjectInternal, persistObjectInternal, processNontransactionalAtomicChanges, putObjectIntoLevel1Cache, putObjectIntoLevel2Cache, putObjectIntoLevel2CacheInternal, putObjectsIntoLevel2Cache, registerEmbeddedRelation, registerExecutionContextListener, releaseThreadContextInfo, removeEmbeddedOwnerRelation, removeInternalFetchGroup, removeObjectFromLevel1Cache, removeObjectFromLevel2Cache, removeObjectProviderAssociatedValue, setAttachDetachReferencedObject, setLevel2Cache, setObjectProviderAssociatedValue, setProperties, setProperty, 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
-
-
-
-
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
-
getLock
public final Lock getLock()
Accessor for the context lock object.- Returns:
- The lock object
-
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(ObjectProvider sm)
Description copied from class:ExecutionContextImpl
Method to enlist the specified ObjectProvider in the current transaction.- Specified by:
enlistInTransaction
in interfaceExecutionContext
- Overrides:
enlistInTransaction
in classExecutionContextImpl
- Parameters:
sm
- The ObjectProvider
-
evictFromTransaction
public void evictFromTransaction(ObjectProvider sm)
Description copied from class:ExecutionContextImpl
Method to evict the specified ObjectProvider from the current transaction.- Specified by:
evictFromTransaction
in interfaceExecutionContext
- Overrides:
evictFromTransaction
in classExecutionContextImpl
- Parameters:
sm
- The ObjectProvider
-
addObjectProviderToCache
public void addObjectProviderToCache(ObjectProvider op)
Description copied from class:ExecutionContextImpl
Method to add the object managed by the specified ObjectProvider to the (L1) cache.- Specified by:
addObjectProviderToCache
in interfaceExecutionContext
- Overrides:
addObjectProviderToCache
in classExecutionContextImpl
- Parameters:
op
- The ObjectProvider
-
removeObjectProviderFromCache
public void removeObjectProviderFromCache(ObjectProvider op)
Description copied from class:ExecutionContextImpl
Method to remove the object managed by the specified ObjectProvider from the cache.- Specified by:
removeObjectProviderFromCache
in interfaceExecutionContext
- Overrides:
removeObjectProviderFromCache
in classExecutionContextImpl
- Parameters:
op
- The ObjectProvider
-
findObjectProvider
public ObjectProvider findObjectProvider(Object pc)
Description copied from class:ExecutionContextImpl
Method to return the ObjectProvider for an object (if managed).- Specified by:
findObjectProvider
in interfaceExecutionContext
- Overrides:
findObjectProvider
in classExecutionContextImpl
- Parameters:
pc
- The object we are checking- Returns:
- The ObjectProvider, 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 class:ExecutionContextImpl
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
-
retrieveObject
public void retrieveObject(Object obj, boolean fgOnly)
Description copied from class:ExecutionContextImpl
Method to retrieve an object.- Specified by:
retrieveObject
in interfaceExecutionContext
- Overrides:
retrieveObject
in classExecutionContextImpl
- Parameters:
obj
- The objectfgOnly
- Whether to retrieve the current fetch group fields only
-
persistObject
public Object persistObject(Object obj, boolean merging)
Description copied from class:ExecutionContextImpl
Method to make an object persistent. NOT to be called by internal DataNucleus methods. Only callable by external APIs (JDO/JPA).- 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 class:ExecutionContextImpl
Method to persist an array of objects to the datastore.- 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 class:ExecutionContextImpl
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 class:ExecutionContextImpl
Method to delete an array of objects from the datastore.- Specified by:
deleteObjects
in interfaceExecutionContext
- Overrides:
deleteObjects
in classExecutionContextImpl
- Parameters:
objs
- The objects
-
makeObjectTransient
public void makeObjectTransient(Object obj, FetchPlanState state)
Description copied from class:ExecutionContextImpl
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 class:ExecutionContextImpl
Method to make an object transactional.- Specified by:
makeObjectTransactional
in interfaceExecutionContext
- Overrides:
makeObjectTransactional
in classExecutionContextImpl
- Parameters:
obj
- The object
-
attachObject
public void attachObject(ObjectProvider ownerOP, Object pc, boolean sco)
Description copied from class:ExecutionContextImpl
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:
ownerOP
- ObjectProvider 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(ObjectProvider ownerOP, Object pc, boolean sco)
Description copied from class:ExecutionContextImpl
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:
ownerOP
- ObjectProvider 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 class:ExecutionContextImpl
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(ObjectProvider op)
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:
op
- The ObjectProvider
-
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(ObjectProvider op, boolean directUpdate)
Description copied from class:ExecutionContextImpl
Method to mark an object (ObjectProvider) as dirty.- Specified by:
markDirty
in interfaceExecutionContext
- Overrides:
markDirty
in classExecutionContextImpl
- Parameters:
op
- ObjectProviderdirectUpdate
- 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(Object 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 optionaly 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 class:ExecutionContextImpl
Method to do a refresh of all objects.- Specified by:
refreshAllObjects
in interfaceExecutionContext
- Overrides:
refreshAllObjects
in classExecutionContextImpl
-
getObjectsToBeFlushed
public List<ObjectProvider> 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:
- ObjectProviders 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
-
-