Search in sources :

Example 1 with DetachState

use of org.datanucleus.DetachState in project datanucleus-core by datanucleus.

the class StateManagerImpl method detachCopy.

/**
 * Method to make detached copy of this instance
 * If the object is detachable then the copy will be migrated to DETACHED state, otherwise will migrate
 * the copy to TRANSIENT. Used by "ExecutionContext.detachObjectCopy()".
 * @param state State for the detachment process
 * @return the detached Persistable instance
 */
public Persistable detachCopy(FetchPlanState state) {
    if (myLC.isDeleted()) {
        throw new NucleusUserException(Localiser.msg("026023", myPC.getClass().getName(), myID));
    }
    if (myEC.getApiAdapter().isDetached(myPC)) {
        throw new NucleusUserException(Localiser.msg("026024", myPC.getClass().getName(), myID));
    }
    if (dirty) {
        myEC.flushInternal(false);
    }
    if (isDetaching()) {
        // Object in the process of detaching (recursive) so return the object which will be the detached object
        return getReferencedPC();
    }
    // Look for an existing detached copy
    DetachState detachState = (DetachState) state;
    DetachState.Entry existingDetached = detachState.getDetachedCopyEntry(myPC);
    Persistable detachedPC;
    if (existingDetached == null) {
        // No existing detached copy - create new one
        detachedPC = myPC.dnNewInstance(this);
        detachState.setDetachedCopyEntry(myPC, detachedPC);
    } else {
        // Found one - if it's sufficient for current FetchPlanState, return it immediately
        detachedPC = (Persistable) existingDetached.getDetachedCopyObject();
        if (existingDetached.checkCurrentState()) {
            return detachedPC;
        }
    // Need to process the detached copy using current FetchPlanState
    }
    myEC.setAttachDetachReferencedObject(this, detachedPC);
    // Check if detachable ... if so then we detach a copy, otherwise we return a transient copy
    boolean detachable = myEC.getApiAdapter().isDetachable(myPC);
    // make sure a detaching PC is not read by another thread while we are detaching
    Object referencedPC = getReferencedPC();
    synchronized (referencedPC) {
        int[] detachFieldNums = getFieldsNumbersToDetach();
        if (detachable) {
            if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
                int[] fieldsToLoad = null;
                if ((myEC.getFetchPlan().getDetachmentOptions() & FetchPlan.DETACH_LOAD_FIELDS) != 0) {
                    fieldsToLoad = ClassUtils.getFlagsSetTo(loadedFields, myFP.getMemberNumbers(), false);
                }
                NucleusLogger.PERSISTENCE.debug(Localiser.msg("010010", StringUtils.toJVMIDString(myPC), "" + state.getCurrentFetchDepth(), StringUtils.toJVMIDString(detachedPC), StringUtils.intArrayToString(detachFieldNums), StringUtils.intArrayToString(fieldsToLoad)));
            }
            // Call any "pre-detach" listeners
            getCallbackHandler().preDetach(myPC);
        }
        try {
            setDetaching(true);
            // Handle any field loading/unloading before the detach
            if ((myEC.getFetchPlan().getDetachmentOptions() & FetchPlan.DETACH_LOAD_FIELDS) != 0) {
                // Load any unloaded fetch-plan fields
                loadUnloadedFieldsInFetchPlan();
            }
            if (myLC == myEC.getNucleusContext().getApiAdapter().getLifeCycleState(LifeCycleState.HOLLOW) || myLC == myEC.getNucleusContext().getApiAdapter().getLifeCycleState(LifeCycleState.P_NONTRANS)) {
                // Migrate any HOLLOW/P_NONTRANS to P_CLEAN etc
                myLC = myLC.transitionReadField(this, true);
            }
            // Create a SM for our copy object
            ObjectProvider smDetachedPC = new StateManagerImpl(myEC, cmd);
            smDetachedPC.initialiseForDetached(detachedPC, getExternalObjectId(myPC), getVersion(myPC));
            myEC.setAttachDetachReferencedObject(smDetachedPC, myPC);
            // If detached copy already existed, take note of fields previously loaded
            if (existingDetached != null) {
                smDetachedPC.retrieveDetachState(smDetachedPC);
            }
            smDetachedPC.replaceFields(detachFieldNums, new DetachFieldManager(this, cmd.getSCOMutableMemberFlags(), myFP, state, true));
            myEC.setAttachDetachReferencedObject(smDetachedPC, null);
            if (detachable) {
                // Update the object with its detached state - not to be confused with the "state" object above
                detachedPC.dnReplaceFlags();
                ((Detachable) detachedPC).dnReplaceDetachedState();
            } else {
                smDetachedPC.makeTransient(null);
            }
            // Remove its StateManager since now detached or transient
            replaceStateManager(detachedPC, null);
        } catch (Exception e) {
            // What could possibly be wrong here ? Log it and let the user provide a testcase, yeah right
            NucleusLogger.PERSISTENCE.warn("DETACH ERROR : Error thrown while detaching " + StringUtils.toJVMIDString(myPC) + " (id=" + myID + "). Provide a testcase that demonstrates this", e);
        } finally {
            setDetaching(false);
            referencedPC = null;
        }
        if (detachable && !myEC.getApiAdapter().isDetached(detachedPC)) {
            // Sanity check on the objects detached state
            throw new NucleusUserException(Localiser.msg("026025", detachedPC.getClass().getName(), myID));
        }
        if (detachable) {
            // Call any "post-detach" listeners
            getCallbackHandler().postDetach(myPC, detachedPC);
        }
    }
    return detachedPC;
}
Also used : Detachable(org.datanucleus.enhancement.Detachable) Persistable(org.datanucleus.enhancement.Persistable) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) DetachFieldManager(org.datanucleus.store.fieldmanager.DetachFieldManager) DetachState(org.datanucleus.DetachState) NucleusObjectNotFoundException(org.datanucleus.exceptions.NucleusObjectNotFoundException) NucleusException(org.datanucleus.exceptions.NucleusException) EndOfFetchPlanGraphException(org.datanucleus.store.fieldmanager.AbstractFetchDepthFieldManager.EndOfFetchPlanGraphException) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) ClassNotResolvedException(org.datanucleus.exceptions.ClassNotResolvedException) NotYetFlushedException(org.datanucleus.exceptions.NotYetFlushedException)

Example 2 with DetachState

use of org.datanucleus.DetachState in project datanucleus-core by datanucleus.

the class DetachFieldManager method endOfGraphOperation.

/**
 * Method to throw and EndOfFetchPlanGraphException since we're at the end of a branch in the tree.
 * @param fieldNumber Number of the field
 * @return Object to return
 */
protected Object endOfGraphOperation(int fieldNumber) {
    // check if the object here is PC and is in the detached cache anyway
    SingleValueFieldManager sfv = new SingleValueFieldManager();
    op.provideFields(new int[] { fieldNumber }, sfv);
    Object value = sfv.fetchObjectField(fieldNumber);
    ApiAdapter api = op.getExecutionContext().getApiAdapter();
    if (api.isPersistable(value)) {
        if (copy) {
            DetachState.Entry entry = ((DetachState) state).getDetachedCopyEntry(value);
            if (entry != null) {
                // While we are at the end of a branch and this would go beyond the depth limits, the object here *is* already detached so just return it
                return entry.getDetachedCopyObject();
            }
        } else if (op.getExecutionContext().getApiAdapter().isDetached(value)) {
            return value;
        }
    }
    // we reached a leaf of the object graph to detach
    throw new EndOfFetchPlanGraphException();
}
Also used : ApiAdapter(org.datanucleus.api.ApiAdapter) DetachState(org.datanucleus.DetachState)

Example 3 with DetachState

use of org.datanucleus.DetachState in project datanucleus-api-jdo by datanucleus.

the class JDOPersistenceManager method detachCopy.

/**
 * JDO method to detach a persistent object.
 * If the object is of class that is not detachable a transient copy will be returned.
 * If the object is not persistent it will be persisted first before detaching a copy.
 * @param pc The object
 * @return The detached object
 */
public <T> T detachCopy(T pc) {
    assertIsOpen();
    if (pc == null) {
        return null;
    }
    try {
        ec.assertClassPersistable(pc.getClass());
        assertReadable("detachCopy");
        return jdoDetachCopy(pc, new DetachState(ec.getApiAdapter()));
    } catch (NucleusException ne) {
        throw NucleusJDOHelper.getJDOExceptionForNucleusException(ne);
    }
}
Also used : DetachState(org.datanucleus.DetachState) NucleusException(org.datanucleus.exceptions.NucleusException)

Example 4 with DetachState

use of org.datanucleus.DetachState in project datanucleus-api-jdo by datanucleus.

the class JDOPersistenceManager method detachCopyAll.

/**
 * Detach the specified objects from the <code>PersistenceManager</code>.
 * @param pcs the instances to detach
 * @return the detached instances
 * @see #detachCopyAll(Object[])
 */
public <T> Collection<T> detachCopyAll(Collection<T> pcs) {
    assertIsOpen();
    assertReadable("detachCopyAll");
    // Detach the objects
    FetchPlanState state = new DetachState(ec.getApiAdapter());
    List detacheds = new ArrayList();
    for (T pc : pcs) {
        if (pc == null) {
            detacheds.add(null);
        } else {
            detacheds.add(jdoDetachCopy(pc, state));
        }
    }
    return detacheds;
}
Also used : DetachState(org.datanucleus.DetachState) ArrayList(java.util.ArrayList) FetchPlanState(org.datanucleus.FetchPlanState) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

DetachState (org.datanucleus.DetachState)4 NucleusException (org.datanucleus.exceptions.NucleusException)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 FetchPlanState (org.datanucleus.FetchPlanState)1 ApiAdapter (org.datanucleus.api.ApiAdapter)1 Detachable (org.datanucleus.enhancement.Detachable)1 Persistable (org.datanucleus.enhancement.Persistable)1 ClassNotResolvedException (org.datanucleus.exceptions.ClassNotResolvedException)1 NotYetFlushedException (org.datanucleus.exceptions.NotYetFlushedException)1 NucleusObjectNotFoundException (org.datanucleus.exceptions.NucleusObjectNotFoundException)1 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)1 EndOfFetchPlanGraphException (org.datanucleus.store.fieldmanager.AbstractFetchDepthFieldManager.EndOfFetchPlanGraphException)1 DetachFieldManager (org.datanucleus.store.fieldmanager.DetachFieldManager)1