org.datanucleus.ExecutionContext Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of datanucleus-core Show documentation
Show all versions of datanucleus-core Show documentation
DataNucleus Core provides the primary components of a heterogenous Java persistence solution.
It supports persistence API's being layered on top of the core functionality.
/**********************************************************************
Copyright (c) 2009 Erik Bengtson and others. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Contributors:
...
**********************************************************************/
package org.datanucleus;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.datanucleus.api.ApiAdapter;
import org.datanucleus.cache.Level1Cache;
import org.datanucleus.enhancement.ExecutionContextReference;
import org.datanucleus.enhancement.Persistable;
import org.datanucleus.exceptions.ClassNotPersistableException;
import org.datanucleus.exceptions.NoPersistenceInformationException;
import org.datanucleus.exceptions.NucleusException;
import org.datanucleus.exceptions.NucleusObjectNotFoundException;
import org.datanucleus.exceptions.NucleusOptimisticException;
import org.datanucleus.exceptions.NucleusUserException;
import org.datanucleus.flush.FlushMode;
import org.datanucleus.flush.Operation;
import org.datanucleus.flush.OperationQueue;
import org.datanucleus.management.ManagerStatistics;
import org.datanucleus.metadata.AbstractMemberMetaData;
import org.datanucleus.metadata.MetaDataManager;
import org.datanucleus.state.CallbackHandler;
import org.datanucleus.state.LockManager;
import org.datanucleus.state.DNStateManager;
import org.datanucleus.state.RelationshipManager;
import org.datanucleus.store.FieldValues;
import org.datanucleus.store.StoreManager;
import org.datanucleus.store.query.Extent;
import org.datanucleus.store.types.TypeManager;
import org.datanucleus.store.types.scostore.Store;
import org.datanucleus.transaction.Transaction;
/**
* Context of execution for persistence operations.
* This equates to the work of a PersistenceManager/EntityManager.
* An ExecutionContext is responsible for
*
* - persist, merge, find and delete of persistable objects from the defined StoreManager
* - have a set of properties defining behaviour over and above the default configuration of the
* parent NucleusContext
* - provide an interface to querying of the persistable objects in the StoreManager
* - provide a way of managing persistable objects using StateManagers
* - have a cache of currently managed objects (the "Level 1" cache), and make use of the
* cache of the parent NucleusContext when not available in its cache
* - have a single "current" transaction. This transaction can be local, or JTA
*
*
* An ExecutionContext can be started with a series of options that affect its behaviour thereafter. These
* are defined by the "OPTION_{YYY}" static Strings.
*
*/
public interface ExecutionContext extends ExecutionContextReference
{
/** Startup option overriding the default (PMF/EMF) username for the connectionURL. */
public static final String OPTION_USERNAME = "user";
/** Startup option overriding the default (PMF/EMF) password for the connectionURL. */
public static final String OPTION_PASSWORD = "password";
/** Startup option setting whether, when using JTA, to do auto-join of transactions. */
public static final String OPTION_JTA_AUTOJOIN = "jta_autojoin";
void initialise(Object owner, Map options);
Level1Cache getLevel1Cache();
/**
* Accessor for the current transaction for this execution context.
* @return The current transaction
*/
Transaction getTransaction();
/**
* Accessor for the Store Manager.
* @return Store Manager
*/
default StoreManager getStoreManager()
{
return getNucleusContext().getStoreManager();
}
/**
* Accessor for the MetaData Manager.
* @return The MetaData Manager
*/
default MetaDataManager getMetaDataManager()
{
return getNucleusContext().getMetaDataManager();
}
/**
* Accessor for the context in which this execution context is running.
* @return Returns the context.
*/
PersistenceNucleusContext getNucleusContext();
/**
* Accessor for the API adapter.
* @return API adapter.
*/
default ApiAdapter getApiAdapter()
{
return getNucleusContext().getApiAdapter();
}
/**
* Acessor for the current FetchPlan
* @return FetchPlan
*/
FetchPlan getFetchPlan();
/**
* Accessor for the ClassLoader resolver to use in class loading issues.
* @return The ClassLoader resolver
*/
ClassLoaderResolver getClassLoaderResolver();
/**
* Accessor for the lock manager for objects in this execution context.
* @return The lock manager
*/
LockManager getLockManager();
/**
* Accessor for any statistics-gathering object.
* @return The statistics for this manager
*/
ManagerStatistics getStatistics();
/**
* Method to set properties on the execution context.
* @param props The properties
*/
void setProperties(Map props);
/**
* Method to set a property on the execution context
* @param name Name of the property
* @param value Value to set
*/
void setProperty(String name, Object value);
/**
* Accessor for a property.
* @param name Name of the property
* @return The value
*/
Object getProperty(String name);
/**
* Accessor for a boolean property value.
* @param name Name of the property
* @return the value
*/
Boolean getBooleanProperty(String name);
/**
* Accessor for an int property value.
* @param name Name of the property
* @return the value
*/
Integer getIntProperty(String name);
/**
* Accessor for a String property value.
* @param name Name of the property
* @return The value
*/
String getStringProperty(String name);
/**
* Accessor for the defined properties.
* @return Properties for this execution context
*/
Map getProperties();
/**
* Accessor for the supported property names.
* @return Set of names
*/
Set getSupportedProperties();
/**
* Convenience accessor for the type manager for this persistence context (from NucleusContext).
* @return The type manager
*/
default TypeManager getTypeManager()
{
return getNucleusContext().getTypeManager();
}
/**
* Method to close the execution context.
*/
void close();
/**
* Accessor for whether this execution context is closed.
* @return Whether this manager is closed.
*/
boolean isClosed();
/**
* Method to find StateManager for the passed persistable object when it is managed by this manager. TODO Change the signature to use generics
* @param pc The persistable object
* @return StateManager
*/
DNStateManager findStateManager(Object pc);
/**
* Method to find StateManager for the passed persistable object when it is managed by this manager,
* and if not yet persistent to persist it and return the assigned StateManager.
* @param pc The persistable object
* @param persist Whether to persist if not yet persistent
* @return StateManager
*/
DNStateManager findStateManager(Object pc, boolean persist);
/**
* Method to find StateManager for the passed embedded persistable object.
* Will create one if not already registered, and tie it to the specified owner.
* @param value The embedded object
* @param owner The owner StateManager (if known).
* @param mmd Metadata for the field of the owner
* @param objectType Type of persistable object being stored
* @return StateManager for the embedded object
*/
DNStateManager findStateManagerForEmbedded(Object value, DNStateManager owner, AbstractMemberMetaData mmd, PersistableObjectType objectType);
DNStateManager findStateManagerOfOwnerForAttachingObject(Object pc);
/**
* Method to add the object managed by the specified StateManager to the cache.
* @param sm StateManager
*/
void addStateManagerToCache(DNStateManager sm);
/**
* Method to remove the object managed by the specified StateManager from the cache.
* @param sm StateManager
*/
void removeStateManagerFromCache(DNStateManager sm);
/**
* Method to evict the passed object.
* @param pc The object
*/
void evictObject(Object pc);
/**
* Method to evict all objects of the specified type (and optionaly its subclasses).
* @param cls Type of persistable object
* @param subclasses Whether to include subclasses
*/
void evictObjects(Class> cls, boolean subclasses);
/**
* Method to evict all L1 cache objects
*/
void evictAllObjects();
/**
* Method to retrieve the (fields of the) passed object(s).
* @param useFetchPlan Whether to retrieve the current fetch plan
* @param pcs The objects
*/
void retrieveObjects(boolean useFetchPlan, Object... pcs);
/**
* Method to make an object persistent.
* Should be called by EXTERNAL APIs (JDO/JPA) only.
* All INTERNAL (DataNucleus) calls should go via persistObjectInternal(...).
* @param pc The object
* @param merging Whether this object (and dependents) is being merged
* @param Type of the persistable object
* @return The persisted object
* @throws NucleusUserException if the object is managed by a different manager
*/
T persistObject(T pc, boolean merging);
/**
* Method to persist an array of objects to the datastore.
* Should be called by EXTERNAL APIs (JDO/JPA) only.
* @param pcs The objects to persist
* @return The persisted objects
* @throws NucleusUserException Thrown if an error occurs during the persist process.
* Any exception could have several nested exceptions for each failed object persist
*/
Object[] persistObjects(Object... pcs);
/**
* Method to make an object persistent which should be called from INTERNAL (DataNucleus) calls only.
* All EXTERNAL (PM/EM) calls should go via persistObject(Object pc).
* To be used when not providing the owner object details that this is part of (embedded).
* @param pc The object
* @param preInsertChanges Changes to be made before inserting
* @param objectType Type of object
* @return The persisted object
* @param Type of the persistable object
* @throws NucleusUserException if the object is managed by a different context
*/
default T persistObjectInternal(T pc, FieldValues preInsertChanges, PersistableObjectType objectType)
{
return persistObjectInternal(pc, preInsertChanges, objectType, null, -1);
}
/**
* Method to make an object persistent which should be called from INTERNAL (DataNucleus) calls only.
* All EXTERNAL (PM/EM) calls should go via persistObject(Object pc).
* To be used when providing the owner object details that this is part of (embedded).
* @param pc The object
* @param preInsertChanges Any changes to make before inserting
* @param objectType Type of object
* @param ownerSM StateManager of the owner when embedded (or attached?) (null if not embedded)
* @param ownerFieldNum Field number in the owner where this is embedded (or attached?) (-1 if not embedded)
* @return The persisted object
* @param Type of the persistable object
* @throws NucleusUserException if the object is managed by a different context
*/
T persistObjectInternal(T pc, FieldValues preInsertChanges, PersistableObjectType objectType, DNStateManager ownerSM, int ownerFieldNum);
/**
* Method to migrate an object to transient state.
* @param pc The object
* @param state Object containing the state of the fetch plan process (if any)
* @throws NucleusException When an error occurs in making the object transient
*/
void makeObjectTransient(Object pc, FetchPlanState state);
/**
* Method to make an object transactional.
* @param pc The object
* @throws NucleusException Thrown when an error occurs
*/
void makeObjectTransactional(Object pc);
/**
* Method to make the passed object nontransactional.
* @param pc The object
* @throws NucleusException Thrown when an error occurs
*/
void makeObjectNontransactional(Object pc);
/**
* Method to return if the specified object exists in the datastore.
* @param obj The (persistable) object
* @return Whether it exists
*/
boolean exists(Object obj);
/**
* Accessor for the currently managed objects for the current transaction.
* If the transaction is not active this returns null.
* @return Collection of managed objects enlisted in the current transaction
*/
Set getManagedObjects();
/**
* Accessor for the currently managed objects for the current transaction.
* If the transaction is not active this returns null.
* @param classes Classes that we want the objects for
* @return Collection of managed objects enlisted in the current transaction
*/
Set getManagedObjects(Class[] classes);
/**
* Accessor for the currently managed objects for the current transaction.
* If the transaction is not active this returns null.
* @param states States that we want the objects for
* @return Collection of managed objects enlisted in the current transaction
*/
Set getManagedObjects(String[] states);
/**
* Accessor for the currently managed objects for the current transaction.
* If the transaction is not active this returns null.
* @param states States that we want the objects for
* @param classes Classes that we want the objects for
* @return Collection of managed objects enlisted in the current transaction
*/
Set getManagedObjects(String[] states, Class[] classes);
/**
* Method to delete an object from the datastore.
* NOT to be called by internal methods. Only callable by external APIs (JDO/JPA).
* @param obj The object
*/
void deleteObject(Object obj);
/**
* Method to delete an array of objects from the datastore.
* @param objs The objects to delete
* @throws NucleusUserException Thrown if an error occurs during the deletion process. Any exception could have several nested exceptions for each failed object deletion
*/
void deleteObjects(Object... objs);
/**
* Method to delete an object from persistence which should be called from internal calls only.
* All PM/EM calls should go via deleteObject(Object obj).
* @param pc Object to delete
*/
void deleteObjectInternal(Object pc);
/**
* 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.
* @param state State for the detachment process
* @param pc The object
* @param Type of the persistable object
*/
void detachObject(FetchPlanState state, T pc);
/**
* Method to detach the passed object(s).
* @param state State for the detachment process.
* @param pcs The object(s) to detach
*/
void detachObjects(FetchPlanState state, Object... pcs);
/**
* Detach a copy of the passed persistent object using the provided detach state.
* If the object is of class that is not detachable it will be detached as transient.
* If it is not yet persistent it will be first persisted.
* @param state State for the detachment process
* @param pc The object
* @param Type of the persistable object
* @return The detached object
*/
T detachObjectCopy(FetchPlanState state, T pc);
/**
* Method to detach all objects in the context.
* Detaches all objects enlisted as well as all objects in the L1 cache.
* Of particular use with JPA when doing a clear of the persistence context.
*/
void detachAll();
/**
* 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.
* @param ownerSM StateManager of the owner object that has this in a field that causes this attach
* @param pc The persistable object
* @param sco Whether the PC object is stored without an identity (embedded/serialised)
* @param Type of the persistable object
*/
void attachObject(DNStateManager ownerSM, T pc, boolean sco);
/**
* 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.
* @param ownerSM StateManager of the owner object that has this in a field that causes this attach
* @param pc The object
* @param sco Whether it has no identity (second-class object)
* @param Type of the persistable object
* @return The attached object
*/
T attachObjectCopy(DNStateManager ownerSM, T pc, boolean sco);
/**
* Convenience method to return the attached object for the specified id if one exists.
* Returns null if there is no currently enlisted/cached object with the specified id.
* @param id The id
* @return The attached object
*/
Object getAttachedObjectForId(Object id);
/**
* Method to do a refresh of an object, updating it from its datastore representation.
* Also updates the object in the L1/L2 caches.
* @param pc The object
*/
void refreshObject(Object pc);
/**
* Method to do a refresh of all objects.
* @throws NucleusUserException thrown if instances could not be refreshed.
*/
void refreshAllObjects();
/**
* Method to enlist the specified StateManager in the current transaction.
* @param sm StateManager
*/
void enlistInTransaction(DNStateManager sm);
/**
* Method to evict the specified StateManager from the current transaction.
* @param sm StateManager
*/
void evictFromTransaction(DNStateManager sm);
/**
* Method to return if an object is enlisted in the current transaction.
* @param id Identity for the object
* @return Whether it is enlisted in the current transaction
*/
boolean isEnlistedInTransaction(Object id);
/**
* Mark the specified StateManager as dirty
* @param sm StateManager
* @param directUpdate Whether the object has had a direct update made on it (if known)
*/
void markDirty(DNStateManager sm, boolean directUpdate);
/**
* Mark the specified StateManager as clean.
* @param sm StateManager
*/
void clearDirty(DNStateManager sm);
/**
* Method to mark as clean all StateManagers of dirty objects.
*/
void clearDirty();
/**
* Method to process any outstanding non-transactional updates that are queued.
* If "datanucleus.nontx.atomic" is false, or currently in a transaction then returns immediately.
* Otherwise will flush any updates that are outstanding (updates to an object), will perform detachAllOnCommit
* if enabled (so user always has detached objects), update objects in any L2 cache, and migrates any
* objects through lifecycle changes.
* Is similar in content to "flush"+"preCommit"+"postCommit"
*/
void processNontransactionalUpdate();
/**
* Accessor for an object of the specified type with the provided id "key".
* With datastore id or single-field id the "key" is the key of the id, and with composite ids the "key" is the toString() of the id.
* @param cls Class of the persistable
* @param key Value of the key field for SingleFieldIdentity, or the string value of the key otherwise
* @return The object for this id.
* @param Type of the persistable
*/
T findObject(Class cls, Object key);
/**
* Accessor for objects of the specified type, with the provided id "key"s.
* With datastore id or single-field id the "key" is the key of the id, and with composite ids the "key" is the toString() of the id.
* @param cls Class of the persistable
* @param keys Values of the key field for SingleFieldIdentity, or the string value of the keys otherwise
* @return The objects meeting this requirement
* @param Type of the persistable
*/
List findObjects(Class cls, List