use of org.datanucleus.state.ObjectProvider in project datanucleus-core by datanucleus.
the class ExecutionContextImpl method evictObjects.
/**
* Method to evict all objects of the specified type (and optionaly its subclasses) that are present in the L1 cache.
* @param cls Type of persistable object
* @param subclasses Whether to include subclasses
*/
public void evictObjects(Class cls, boolean subclasses) {
if (cache != null) {
Set<ObjectProvider> opsToEvict = new HashSet<>(cache.values());
for (ObjectProvider op : opsToEvict) {
Object pc = op.getObject();
boolean evict = false;
if (!subclasses && pc.getClass() == cls) {
evict = true;
} else if (subclasses && cls.isAssignableFrom(pc.getClass())) {
evict = true;
}
if (evict) {
op.evict();
removeObjectFromLevel1Cache(getApiAdapter().getIdForObject(pc));
}
}
}
}
use of org.datanucleus.state.ObjectProvider in project datanucleus-core by datanucleus.
the class ExecutionContextImpl method makeObjectTransient.
/**
* Method to migrate an object to transient state.
* @param obj 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
*/
public void makeObjectTransient(Object obj, FetchPlanState state) {
if (obj == null) {
return;
}
try {
clr.setPrimary(obj.getClass().getClassLoader());
assertClassPersistable(obj.getClass());
assertNotDetached(obj);
if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
NucleusLogger.PERSISTENCE.debug(Localiser.msg("010022", StringUtils.toJVMIDString(obj)));
}
if (getApiAdapter().isPersistent(obj)) {
ObjectProvider op = findObjectProvider(obj);
op.makeTransient(state);
}
} finally {
clr.unsetPrimary();
}
}
use of org.datanucleus.state.ObjectProvider in project datanucleus-core by datanucleus.
the class ExecutionContextImpl method attachObject.
/**
* 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 ownerOP ObjectProvider 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)
*/
public void attachObject(ObjectProvider ownerOP, Object pc, boolean sco) {
assertClassPersistable(pc.getClass());
// Store the owner for this persistable object being attached
// For the current thread
Map attachedOwnerByObject = getThreadContextInfo().attachedOwnerByObject;
if (attachedOwnerByObject != null) {
attachedOwnerByObject.put(pc, ownerOP);
}
ApiAdapter api = getApiAdapter();
Object id = api.getIdForObject(pc);
if (id != null && isInserting(pc)) {
// Object is being inserted in this transaction so just return
return;
} else if (id == null && !sco) {
// Transient object so needs persisting
persistObjectInternal(pc, null, null, -1, ObjectProvider.PC);
return;
}
if (api.isDetached(pc)) {
// Detached, so migrate to attached
if (cache != null) {
ObjectProvider l1CachedOP = cache.get(id);
if (l1CachedOP != null && l1CachedOP.getObject() != pc) {
// attached object with the same id already present in the L1 cache so cannot attach in-situ
throw new NucleusUserException(Localiser.msg("010017", StringUtils.toJVMIDString(pc)));
}
}
if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
NucleusLogger.PERSISTENCE.debug(Localiser.msg("010016", StringUtils.toJVMIDString(pc)));
}
ObjectProvider op = nucCtx.getObjectProviderFactory().newForDetached(this, pc, id, api.getVersionForObject(pc));
op.attach(sco);
} else {
// Not detached so can't attach it. Just return
return;
}
}
use of org.datanucleus.state.ObjectProvider in project datanucleus-core by datanucleus.
the class ExecutionContextImpl method close.
/**
* Method to close the context.
*/
public void close() {
if (closed) {
throw new NucleusUserException(Localiser.msg("010002"));
}
if (tx.getIsActive()) {
String closeActionTxAction = nucCtx.getConfiguration().getStringProperty(PropertyNames.PROPERTY_EXECUTION_CONTEXT_CLOSE_ACTIVE_TX_ACTION);
if (closeActionTxAction != null) {
if (closeActionTxAction.equalsIgnoreCase("exception")) {
throw new TransactionActiveOnCloseException(this);
} else if (closeActionTxAction.equalsIgnoreCase("rollback")) {
NucleusLogger.GENERAL.warn("ExecutionContext closed with active transaction, so rolling back the active transaction");
tx.rollback();
}
}
}
// Commit any outstanding non-tx updates
if (!dirtyOPs.isEmpty() && tx.getNontransactionalWrite()) {
if (isNonTxAtomic()) {
// TODO Remove this when all mutator operations handle it atomically
// Process as nontransactional update
processNontransactionalUpdate();
} else {
// Process within a transaction
try {
tx.begin();
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
}
}
if (properties.getFrequentProperties().getDetachOnClose() && cache != null && !cache.isEmpty()) {
// "Detach-on-Close", detaching all currently cached objects
// TODO This will remove objects from the L1 cache one-by-one. Is there a possibility for optimisation? See also AttachDetachTest.testDetachOnClose
NucleusLogger.PERSISTENCE.debug(Localiser.msg("010011"));
List<ObjectProvider> toDetach = new ArrayList<>(cache.values());
try {
if (!tx.getNontransactionalRead()) {
tx.begin();
}
for (ObjectProvider op : toDetach) {
if (op != null && op.getObject() != null && !op.getExecutionContext().getApiAdapter().isDeleted(op.getObject()) && op.getExternalObjectId() != null) {
// If the object is deleted then no point detaching. An object can be in L1 cache if transient and passed in to a query as a param for example
try {
op.detach(new DetachState(getApiAdapter()));
} catch (NucleusObjectNotFoundException onfe) {
// Catch exceptions for any objects that are deleted in other managers whilst having this open
}
}
}
if (!tx.getNontransactionalRead()) {
tx.commit();
}
} finally {
if (!tx.getNontransactionalRead()) {
if (tx.isActive()) {
tx.rollback();
}
}
}
NucleusLogger.PERSISTENCE.debug(Localiser.msg("010012"));
}
// Call all listeners to do their clean up TODO Why is this here and not after "disconnect remaining resources" or before "detachOnClose"?
ExecutionContext.LifecycleListener[] listener = nucCtx.getExecutionContextListeners();
for (int i = 0; i < listener.length; i++) {
listener[i].preClose(this);
}
closing = true;
// Disconnect remaining resources
if (cache != null && !cache.isEmpty()) {
// Clear out the cache (use separate list since sm.disconnect will remove the object from "cache" so we avoid any ConcurrentModification issues)
Collection<ObjectProvider> cachedOPsClone = new HashSet<>(cache.values());
for (ObjectProvider op : cachedOPsClone) {
if (op != null) {
// Remove it from any transaction
op.disconnect();
} else {
NucleusLogger.CACHE.error(">> EC.close L1Cache op IS NULL!");
}
}
cache.clear();
if (NucleusLogger.CACHE.isDebugEnabled()) {
NucleusLogger.CACHE.debug(Localiser.msg("003011"));
}
} else {
// TODO If there is no cache we need a way for ObjectProviders to be disconnected; have ObjectProvider as listener for EC close? (ecListeners below)
}
// Clear out lifecycle listeners that were registered
closeCallbackHandler();
if (ecListeners != null) {
// Inform all interested parties that we are about to close
Set<ExecutionContextListener> listeners = new HashSet<>(ecListeners);
for (ExecutionContextListener lstr : listeners) {
lstr.executionContextClosing(this);
}
ecListeners.clear();
ecListeners = null;
}
// Reset the Fetch Plan to its DFG setting
fetchPlan.clearGroups().addGroup(FetchPlan.DEFAULT);
if (statistics != null) {
statistics.close();
statistics = null;
}
enlistedOPCache.clear();
dirtyOPs.clear();
indirectDirtyOPs.clear();
if (nontxProcessedOPs != null) {
nontxProcessedOPs.clear();
nontxProcessedOPs = null;
}
if (managedRelationsHandler != null) {
managedRelationsHandler.clear();
}
if (l2CacheTxIds != null) {
l2CacheTxIds.clear();
}
if (l2CacheTxFieldsToUpdateById != null) {
l2CacheTxFieldsToUpdateById.clear();
}
if (pbrAtCommitHandler != null) {
pbrAtCommitHandler.clear();
}
if (opEmbeddedInfoByOwner != null) {
opEmbeddedInfoByOwner.clear();
opEmbeddedInfoByOwner = null;
}
if (opEmbeddedInfoByEmbedded != null) {
opEmbeddedInfoByEmbedded.clear();
opEmbeddedInfoByEmbedded = null;
}
if (opAssociatedValuesMapByOP != null) {
opAssociatedValuesMapByOP.clear();
opAssociatedValuesMapByOP = null;
}
l2CacheObjectsToEvictUponRollback = null;
closing = false;
closed = true;
// Close the transaction
tx.close();
tx = null;
owner = null;
if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
NucleusLogger.PERSISTENCE.debug(Localiser.msg("010001", this));
}
// Hand back to the pool for reuse
nucCtx.getExecutionContextPool().checkIn(this);
}
use of org.datanucleus.state.ObjectProvider in project datanucleus-core by datanucleus.
the class ExecutionContextImpl method findObjectsById.
/**
* Accessor for objects with the specified identities.
* @param identities Ids of the object(s).
* @param validate Whether to validate the object state
* @return The Objects with these ids (same order)
* @throws NucleusObjectNotFoundException if an object doesn't exist in the datastore
*/
public Object[] findObjectsById(Object[] identities, boolean validate) {
if (identities == null) {
return null;
} else if (identities.length == 1) {
return new Object[] { findObject(identities[0], validate, validate, null) };
}
for (int i = 0; i < identities.length; i++) {
if (identities[i] == null) {
throw new NucleusUserException(Localiser.msg("010044"));
}
}
// Set the identities array
Object[] ids = new Object[identities.length];
for (int i = 0; i < identities.length; i++) {
// Translate the identity if required
if (identities[i] instanceof String) {
IdentityStringTranslator idStringTranslator = getNucleusContext().getIdentityManager().getIdentityStringTranslator();
if (idStringTranslator != null) {
// DataNucleus extension to translate input identities into valid persistent identities.
ids[i] = idStringTranslator.getIdentity(this, (String) identities[i]);
continue;
}
}
ids[i] = identities[i];
}
Map pcById = new HashMap(identities.length);
List idsToFind = new ArrayList();
ApiAdapter api = getApiAdapter();
// Check the L1 cache
for (int i = 0; i < ids.length; i++) {
Object pc = getObjectFromLevel1Cache(ids[i]);
if (pc != null) {
if (ids[i] instanceof SCOID) {
if (api.isPersistent(pc) && !api.isNew(pc) && !api.isDeleted(pc) && !api.isTransactional(pc)) {
// JDO [5.4.4] Can't return HOLLOW nondurable objects
throw new NucleusUserException(Localiser.msg("010005"));
}
}
pcById.put(ids[i], pc);
} else {
idsToFind.add(ids[i]);
}
}
if (!idsToFind.isEmpty() && l2CacheEnabled) {
// Check the L2 cache for those not found
Map pcsById = getObjectsFromLevel2Cache(idsToFind);
if (!pcsById.isEmpty()) {
// Found some so add to the values, and remove from the "toFind" list
Iterator<Map.Entry> entryIter = pcsById.entrySet().iterator();
while (entryIter.hasNext()) {
Map.Entry entry = entryIter.next();
pcById.put(entry.getKey(), entry.getValue());
idsToFind.remove(entry.getKey());
}
}
}
boolean performValidationWhenCached = nucCtx.getConfiguration().getBooleanProperty(PropertyNames.PROPERTY_FIND_OBJECT_VALIDATE_WHEN_CACHED);
List<ObjectProvider> opsToValidate = new ArrayList<>();
if (validate) {
if (performValidationWhenCached) {
// Mark all ObjectProviders for validation (performed at end)
Collection pcValues = pcById.values();
for (Object pc : pcValues) {
if (api.isTransactional(pc)) {
// This object is transactional, so no need to validate
continue;
}
// Mark this object for validation
ObjectProvider op = findObjectProvider(pc);
opsToValidate.add(op);
}
}
}
Object[] foundPcs = null;
if (!idsToFind.isEmpty()) {
// Try to find unresolved objects direct from the datastore if supported by the datastore (e.g ODBMS)
foundPcs = getStoreManager().getPersistenceHandler().findObjects(this, idsToFind.toArray());
}
int foundPcIdx = 0;
for (Object id : idsToFind) {
// Id target class could change due to inheritance level
Object idOrig = id;
Object pc = foundPcs != null ? foundPcs[foundPcIdx++] : null;
ObjectProvider op = null;
if (pc != null) {
// Object created by store plugin
op = findObjectProvider(pc);
putObjectIntoLevel1Cache(op);
} else {
// Object not found yet, so maybe class name is not correct inheritance level
ClassDetailsForId details = getClassDetailsForId(id, null, validate);
String className = details.className;
id = details.id;
if (details.pc != null) {
// Found in cache from updated id
pc = details.pc;
op = findObjectProvider(pc);
if (performValidationWhenCached && validate) {
if (!api.isTransactional(pc)) {
// Mark this object for validation
opsToValidate.add(op);
}
}
} else {
// Still not found so create a Hollow instance with the supplied field values
try {
Class pcClass = clr.classForName(className, (id instanceof DatastoreId) ? null : id.getClass().getClassLoader());
if (Modifier.isAbstract(pcClass.getModifiers())) {
// This class is abstract so impossible to have an instance of this type
throw new NucleusObjectNotFoundException(Localiser.msg("010027", IdentityUtils.getPersistableIdentityForId(id), className));
}
op = nucCtx.getObjectProviderFactory().newForHollow(this, pcClass, id);
pc = op.getObject();
if (!validate) {
// Mark the ObjectProvider as needing to validate this object before loading fields
op.markForInheritanceValidation();
}
// Cache it in case we have bidir relations
putObjectIntoLevel1Cache(op);
} catch (ClassNotResolvedException e) {
NucleusLogger.PERSISTENCE.warn(Localiser.msg("010027", IdentityUtils.getPersistableIdentityForId(id)));
throw new NucleusUserException(Localiser.msg("010027", IdentityUtils.getPersistableIdentityForId(id)), e);
}
if (validate) {
// Mark this object for validation
opsToValidate.add(op);
}
}
}
// Put in map under input id, so we find it later
pcById.put(idOrig, pc);
}
if (!opsToValidate.isEmpty()) {
// Validate the objects that need it
try {
getStoreManager().getPersistenceHandler().locateObjects(opsToValidate.toArray(new ObjectProvider[opsToValidate.size()]));
} catch (NucleusObjectNotFoundException nonfe) {
NucleusObjectNotFoundException[] nonfes = (NucleusObjectNotFoundException[]) nonfe.getNestedExceptions();
if (nonfes != null) {
for (int i = 0; i < nonfes.length; i++) {
Object missingId = nonfes[i].getFailedObject();
removeObjectFromLevel1Cache(missingId);
}
}
throw nonfe;
}
}
Object[] objs = new Object[ids.length];
for (int i = 0; i < ids.length; i++) {
Object id = ids[i];
objs[i] = pcById.get(id);
}
return objs;
}
Aggregations