Package org.datanucleus.state

Examples of org.datanucleus.state.StateManager


                ArrayList stateManagersToEvict = new ArrayList();
                stateManagersToEvict.addAll(cache.values());
                Iterator smIter = stateManagersToEvict.iterator();
                while (smIter.hasNext())
                {
                    StateManager sm = (StateManager)smIter.next();
                    Object pc = sm.getObject();
                    boolean evict = false;
                    if (!subclasses && pc.getClass() == cls)
                    {
                        evict = true;
                    }
                    else if (subclasses && cls.isAssignableFrom(pc.getClass()))
                    {
                        evict = true;
                    }

                    if (evict)
                    {
                        sm.evict();
                        removeObjectFromCache(getApiAdapter().getIdForObject(pc));
                    }
                }
            }
        }
View Full Code Here


        {
            //post commit cannot change objects (sanity check - avoid changing avoids on detach)
            throw new NucleusException("Cannot change objects when transaction is no longer active.");
        }

        StateManager sm = (StateManager)op;
        boolean isInDirty = dirtySMs.contains(sm);
        boolean isInIndirectDirty = indirectDirtySMs.contains(sm);
        if (!isDelayDatastoreOperationsEnabled() && !isInDirty && !isInIndirectDirty &&
            dirtySMs.size() >= getNucleusContext().getPersistenceConfiguration().getIntProperty("datanucleus.datastoreTransactionFlushLimit"))
        {
            // Reached flush limit so flush
            flushInternal(false);
        }

        if (directUpdate)
        {
            if (isInIndirectDirty)
            {
                indirectDirtySMs.remove(sm);
                dirtySMs.add(sm);
            }
            else if (!isInDirty)
            {
                dirtySMs.add(sm);
                if (txCachedIds != null)
                {
                    txCachedIds.add(sm.getInternalObjectId());
                }
            }
        }
        else
        {
            if (!isInDirty && !isInIndirectDirty)
            {
                // Register as an indirect dirty
                indirectDirtySMs.add(sm);
                if (txCachedIds != null)
                {
                    txCachedIds.add(sm.getInternalObjectId());
                }
            }
        }
    }
View Full Code Here

        }

        // a). direct dirty objects
        for (int i = 0; i < toFlushDirect.length; i++)
        {
            StateManager sm = (StateManager) toFlushDirect[i];
            try
            {
                sm.flush();
                if (classesToFlush != null)
                {
                    classesToFlush.add(sm.getObject().getClass());
                }
            }
            catch (NucleusOptimisticException oe)
            {
                if (optimisticFailures == null)
                {
                    optimisticFailures = new ArrayList();
                }
                optimisticFailures.add(oe);
            }
        }

        // b). indirect dirty objects
        for (int i = 0; i < toFlushIndirect.length; i++)
        {
            StateManager sm = (StateManager) toFlushIndirect[i];
            try
            {
                sm.flush();
                if (classesToFlush != null)
                {
                    classesToFlush.add(sm.getObject().getClass());
                }
            }
            catch (NucleusOptimisticException oe)
            {
                if (optimisticFailures == null)
View Full Code Here

            // Process all modified objects adding/updating/removing from L2 cache as appropriate
            Iterator txCachedIter = txCachedIds.iterator();
            while (txCachedIter.hasNext())
            {
                Object id = txCachedIter.next();
                StateManager sm = enlistedSMCache.get(id);
                if (sm == null)
                {
                    // Modified object no longer enlisted so has been GCed, so remove from L2
                    if (NucleusLogger.CACHE.isDebugEnabled())
                    {
                        NucleusLogger.CACHE.debug(LOCALISER.msg("004014", id));
                    }
                    l2Cache.evict(id);
                }
                else
                {
                    // Modified object still enlisted so cacheable
                    Object objID = getApiAdapter().getIdForObject(sm.getObject());
                    if (objID == null)
                    {
                        // Must be embedded
                    }
                    else if (getApiAdapter().isDeleted(sm.getObject()))
                    {
                        // Object has been deleted so remove from L2 cache
                        if (NucleusLogger.CACHE.isDebugEnabled())
                        {
                            NucleusLogger.CACHE.debug(LOCALISER.msg("004007",
                                StringUtils.toJVMIDString(sm.getObject()), sm.getInternalObjectId()));
                        }
                        l2Cache.evict(objID);
                    }
                    else if (!getApiAdapter().isDetached(sm.getObject()))
                    {
                        // Object has been added/modified so update in L2 cache
                        putObjectIntoLevel2CacheInternal(sm, true);
                    }
                }
View Full Code Here

                    {
                        NucleusLogger.PERSISTENCE.debug("Performing reachability algorithm on object with id \""+ids[i]+"\"");
                    }
                    try
                    {
                        StateManager sm = findStateManager(findObject(ids[i], true, true, null));
                        sm.runReachability(currentReachables);
                        if (i % 10000 == 0 || i == ids.length-1)
                        {
                            // Flush every 10000 or on the last one to make sure tx cache is empty
                            flushInternal(true);
                        }
                    }
                    catch (NucleusObjectNotFoundException ex)
                    {
                        objectNotFound.add(ids[i]);
                    }
                }
                else
                {
                    // Was deleted earlier so ignore
                }
            }

            // Remove any of the "reachable" instances that are no longer "reachable"
            txFlushedNewIds.removeAll(currentReachables);

            Object nonReachableIds[] = txFlushedNewIds.toArray();
            if (nonReachableIds != null && nonReachableIds.length > 0)
            {
                // For all of instances no longer reachable we need to delete them from the datastore
                // A). Nullify all of their fields.
                // TODO See CORE-3276 for a possible change to this
                for (int i=0; i<nonReachableIds.length; i++)
                {
                    if (NucleusLogger.PERSISTENCE.isDebugEnabled())
                    {
                        NucleusLogger.PERSISTENCE.debug(LOCALISER.msg("010033", nonReachableIds[i]));
                    }
                    try
                    {
                        if (!objectNotFound.contains(nonReachableIds[i]))
                        {
                            StateManager sm = findStateManager(findObject(nonReachableIds[i], true, true, null));
                            sm.nullifyFields();

                            if (i % 10000 == 0 || i == nonReachableIds.length-1)
                            {
                                // Flush every 10000 or on the last one to clear out dirties
                                flushInternal(true);
                            }
                        }
                    }
                    catch (NucleusObjectNotFoundException ex)
                    {
                        // just ignore if the object does not exist anymore 
                    }
                }

                // B). Remove the objects
                for (int i=0; i<nonReachableIds.length; i++)
                {
                    try
                    {
                        if (!objectNotFound.contains(nonReachableIds[i]))
                        {
                            StateManager sm = findStateManager(findObject(nonReachableIds[i], true, true, null));
                            sm.deletePersistent();
                            if (i % 10000 == 0 || i == nonReachableIds.length-1)
                            {
                                // Flush every 10000 or on the last one to clear out dirties
                                flushInternal(true);
                            }
View Full Code Here

        // Make sure that all FetchPlan fields are loaded
        Iterator smsIter = sms.iterator();
        while (smsIter.hasNext())
        {
            StateManager sm = (StateManager)smsIter.next();
            Object pc = sm.getObject();
            if (pc != null && !getApiAdapter().isDetached(pc) && !getApiAdapter().isDeleted(pc))
            {
                // Load all fields (and sub-objects) in the FetchPlan
                FetchPlanState state = new FetchPlanState();
                try
                {
                    sm.loadFieldsInFetchPlan(state);
                }
                catch (NucleusObjectNotFoundException onfe)
                {
                    // This object doesnt exist in the datastore at this point.
                    // Either the user has some other process that has deleted it or they have
                    // defined datastore based cascade delete and it has been deleted that way
                    NucleusLogger.PERSISTENCE.warn(LOCALISER.msg("010013",
                        StringUtils.toJVMIDString(pc), sm.getInternalObjectId()));
                    smsIter.remove();
                    // TODO Move the object state to P_DELETED for consistency
                }
            }
        }
View Full Code Here

    private void performDetachOnCloseWork(List<StateManager> smsToDetach)
    {
        Iterator<StateManager> iter = smsToDetach.iterator();
        while (iter.hasNext())
        {
            StateManager sm = iter.next();
            if (sm != null && sm.getObject() != null &&
                !sm.getObjectManager().getApiAdapter().isDeleted(sm.getObject()) &&
                sm.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
                {
                    sm.detach(new DetachState(getApiAdapter()));
                }
                catch (NucleusObjectNotFoundException onfe)
                {
                    // Catch exceptions for any objects that are deleted in other managers whilst having this open
                }
View Full Code Here

            {
                Collection sms = enlistedSMCache.values();
                Iterator<StateManager> smsIter = sms.iterator();
                while (smsIter.hasNext())
                {
                    StateManager sm = smsIter.next();
                    try
                    {
                        sm.preRollback(getTransaction());
                    }
                    catch (RuntimeException e)
                    {
                        if (failures == null)
                        {
View Full Code Here

     * @return Persistence Capable object (with connected StateManager).
     */
    public Object getObjectFromCache(Object id)
    {
        Object pc = null;
        StateManager sm = null;

        // Try Level 1 first
        if (cache != null)
        {
            sm = (StateManager)cache.get(id);
            if (sm != null)
            {
                pc = sm.getObject();
                if (NucleusLogger.CACHE.isDebugEnabled())
                {
                    NucleusLogger.CACHE.debug(LOCALISER.msg("003008", StringUtils.toJVMIDString(pc),
                        getIdentityAsString(id),
                        StringUtils.booleanArrayToString(sm.getLoadedFields()),
                        "" + cache.size()));
                }

                // Wipe the detach state that may have been added if the object has been serialised in the meantime
                sm.resetDetachState();

                return pc;
            }
            else
            {
                if (NucleusLogger.CACHE.isDebugEnabled())
                {
                    NucleusLogger.CACHE.debug(LOCALISER.msg("003007", getIdentityAsString(id), "" + cache.size()));
                }
            }
        }

        // Try Level 2 since not in Level 1
        if (context.hasLevel2Cache())
        {
            Level2Cache l2Cache = context.getLevel2Cache();
            CachedPC cachedPC = null;
            synchronized (l2Cache)
            {
                cachedPC = l2Cache.get(id);
            }

            // Create active version of cached object with StateManager connected and same id
            if (cachedPC != null)
            {
                if (NucleusLogger.CACHE.isDebugEnabled())
                {
                    NucleusLogger.CACHE.debug(LOCALISER.msg("004015",
                        getIdentityAsString(id),
                        StringUtils.booleanArrayToString(cachedPC.getLoadedFields()),
                        StringUtils.objectArrayToString(cachedPC.getRelationFieldNames())));
                }

                sm = (StateManager) ObjectProviderFactory.newForCachedPC(this, id, cachedPC);
                pc = sm.getObject(); // Object in P_CLEAN state
                if (NucleusLogger.CACHE.isDebugEnabled())
                {
                    NucleusLogger.CACHE.debug(LOCALISER.msg("004006",
                        getIdentityAsString(id), StringUtils.toJVMIDString(pc)));
                }
                if (tx.isActive() && tx.getOptimistic())
                {
                    // Optimistic txns, so return as P_NONTRANS (as per JDO2 spec)
                    sm.makeNontransactional();
                }
                else if (!tx.isActive() && getApiAdapter().isTransactional(pc))
                {
                    // Non-tx context, so return as P_NONTRANS (as per JDO2 spec)
                    sm.makeNontransactional();
                }

                return pc;
            }
            else
View Full Code Here

        {
            NucleusLogger.CACHE.warn(LOCALISER.msg("003006"));
            return;
        }

        StateManager sm = findStateManager(pc);

        // Update L1 cache
        if (cache != null)
        {
            Object o = cache.get(oldID); //use get() because a cache.remove operation returns a weakReference instance
View Full Code Here

TOP

Related Classes of org.datanucleus.state.StateManager

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.