Package org.apache.openjpa.kernel

Examples of org.apache.openjpa.kernel.OpenJPAStateManager


        boolean ret;
        FieldMetaData[] pks = meta.getPrimaryKeyFields();
        if (meta.isOpenJPAIdentity()
            && pks[0].getDeclaredTypeCode() == JavaTypes.OID) {
            OpenJPAStateManager oidsm = new ObjectIdStateManager
                (sm.fetchObjectField(pks[0].getIndex()), sm, pks[0]);
            ret = assign(oidsm, store, pks[0].getEmbeddedMetaData().
                getFields(), preFlush);
            sm.storeObjectField(pks[0].getIndex(),
                oidsm.getManagedInstance());
        } else
            ret = assign(sm, store, meta.getPrimaryKeyFields(), preFlush);
        if (!ret)
            return false;
View Full Code Here


            un = em.find(getUnenhancedClass(), un.getId());
            assertTrue(getUnenhancedClass() != un.getClass());
        }

        em.evict(un);
        OpenJPAStateManager sm = (OpenJPAStateManager)
            ImplHelper.toPersistenceCapable(un, null).pcGetStateManager();

        // we only expect lazy loading to work when we can redefine classes
        // or when accessing a property-access record that OpenJPA created.
        if (ClassRedefiner.canRedefineClasses()
            || (!userDefined && sm.getMetaData().getAccessType()
                != ClassMetaData.ACCESS_FIELD)) {

            assertFalse(sm.getLoaded()
                .get(sm.getMetaData().getField("stringField").getIndex()));

            // make sure that the value was cleared...
            Field field = getUnenhancedClass().getDeclaredField(
                isFieldAccessTest() ? "stringField" : "sf");
            field.setAccessible(true);
            assertEquals(null, field.get(un));

            // ... and that it gets reloaded properly
            assertEquals("foo", un.getStringField());
            assertTrue(sm.getLoaded()
                .get(sm.getMetaData().getField("stringField").getIndex()));
        } else {
            // unredefined properties with user-defined instance, or any
            // unredefined field access
            assertTrue(sm.getLoaded()
                .get(sm.getMetaData().getField("stringField").getIndex()));

            // make sure that the value was not cleared
            Field field = getUnenhancedClass().getDeclaredField(
                isFieldAccessTest() ? "stringField" : "sf");
            field.setAccessible(true);
View Full Code Here

        em.close();

        em = emf.createEntityManager();
        un = em.find(getUnenhancedClass(), un.getId());
        assertTrue(getUnenhancedClass() != un.getClass());
        OpenJPAStateManager sm = (OpenJPAStateManager)
            ImplHelper.toPersistenceCapable(un, null).pcGetStateManager();

        // we only expect lazy loading to work when we can redefine classes
        // or when accessing a property-access record that OpenJPA created.
        if (ClassRedefiner.canRedefineClasses()
            || (sm.getMetaData().getAccessType() != ClassMetaData.ACCESS_FIELD))
        {
            assertFalse(sm.getLoaded()
                .get(sm.getMetaData().getField("lazyField").getIndex()));

            // make sure that the value was cleared
            Field field = getUnenhancedClass().getDeclaredField("lazyField");
            field.setAccessible(true);
            assertEquals(null, field.get(un));
        } else {
            // unredefined field access
            assertTrue(sm.getLoaded()
                .get(sm.getMetaData().getField("lazyField").getIndex()));

            // make sure that the value was loaded already
            Field field = getUnenhancedClass().getDeclaredField("lazyField");
            field.setAccessible(true);
            assertEquals("lazy", field.get(un));
        }

        // make sure that the value is available, one way or another
        assertEquals("lazy", un.getLazyField());
        assertTrue(sm.getLoaded()
            .get(sm.getMetaData().getField("lazyField").getIndex()));

        em.close();
    }
View Full Code Here

            return super.loadAll(sms, state, load, fetch, edata);

        Map unloaded = null;
        List smList = null;
        Map caches = new HashMap();
        OpenJPAStateManager sm;
        DataCache cache;
        DataCachePCData data;
        BitSet fields;

        for (Iterator itr = sms.iterator(); itr.hasNext();) {
            sm = (OpenJPAStateManager) itr.next();
            cache = sm.getMetaData().getDataCache();
            if (cache == null || sm.isEmbedded()) {
                unloaded = addUnloaded(sm, null, unloaded);
                continue;
            }

            if (sm.getManagedInstance() == null
                || load != FORCE_LOAD_NONE
                || sm.getPCState() == PCState.HOLLOW) {
                smList = (List) caches.get(cache);
                if (smList == null) {
                    smList = new ArrayList();
                    caches.put(cache, smList);
                }
                smList.add(sm);
            } else if (!cache.contains(sm.getObjectId()))
                unloaded = addUnloaded(sm, null, unloaded);
        }
       
        for (Iterator itr = caches.keySet().iterator(); itr.hasNext();) {
            cache = (DataCache) itr.next();
            smList = (List) caches.get(cache);
            List oidList = new ArrayList(smList.size());

            for (itr=smList.iterator();itr.hasNext();) {
                sm = (OpenJPAStateManager) itr.next();
                oidList.add((OpenJPAId) sm.getObjectId());
            }
           
            Map dataMap = cache.getAll(oidList);

            for (itr=smList.iterator();itr.hasNext();) {
                sm = (OpenJPAStateManager) itr.next();
                data = (DataCachePCData) dataMap.get(
                        (OpenJPAId) sm.getObjectId());

                if (sm.getManagedInstance() == null) {
                    if (data != null) {
                        //### the 'data.type' access here probably needs
                        //### to be addressed for bug 511
                        sm.initialize(data.getType(), state);
                        data.load(sm, fetch, edata);
                    } else
                        unloaded = addUnloaded(sm, null, unloaded);
                } else if (load != FORCE_LOAD_NONE
                        || sm.getPCState() == PCState.HOLLOW) {
                    data = cache.get(sm.getObjectId());
                    if (data != null) {
                        // load unloaded fields
                        fields = sm.getUnloaded(fetch);
                        data.load(sm, fields, fetch, edata);
                        if (fields.length() > 0)
                            unloaded = addUnloaded(sm, fields, unloaded);
                    } else
                        unloaded = addUnloaded(sm, null, unloaded);
                }
            }
        }

        if (unloaded == null)
            return Collections.EMPTY_LIST;

        // load with delegate
        Collection failed = super.loadAll(unloaded.keySet(), state, load,
            fetch, edata);
        if (!_ctx.getPopulateDataCache())
            return failed;

        // for each loaded instance, merge loaded state into cached data
        Map.Entry entry;
        boolean isNew;
        for (Iterator itr = unloaded.entrySet().iterator(); itr.hasNext();) {
            entry = (Map.Entry) itr.next();
            sm = (OpenJPAStateManager) entry.getKey();
            fields = (BitSet) entry.getValue();

            cache = sm.getMetaData().getDataCache();
            if (cache == null || sm.isEmbedded() || (failed != null
                && failed.contains(sm.getId())))
                continue;

            // make sure that we're not trying to cache an old version
            cache.writeLock();
            try {
                data = cache.get(sm.getObjectId());
                if (data != null && compareVersion(sm, sm.getVersion(),
                    data.getVersion()) == VERSION_EARLIER)
                    continue;

                isNew = data == null;
                if (isNew)
View Full Code Here

     * @since 0.4.0
     */
    public static Collection loadAll(Collection sms, StoreManager store,
        PCState state, int load, FetchConfiguration fetch, Object context) {
        Collection failed = null;
        OpenJPAStateManager sm;
        LockManager lm;
        for (Iterator itr = sms.iterator(); itr.hasNext();) {
            sm = (OpenJPAStateManager) itr.next();
            if (sm.getManagedInstance() == null) {
                if (!store.initialize(sm, state, fetch, context))
                    failed = addFailedId(sm, failed);
            } else if (load != StoreManager.FORCE_LOAD_NONE
                || sm.getPCState() == PCState.HOLLOW) {
                lm = sm.getContext().getLockManager();
                if (!store.load(sm, sm.getUnloaded(fetch), fetch,
                    lm.getLockLevel(sm), context))
                    failed = addFailedId(sm, failed);
            } else if (!store.exists(sm, context))
                failed = addFailedId(sm, failed);
        }
View Full Code Here

        // if large transaction mode don't record individual changes
        if (_ctx.isTrackChangesByType())
            return exceps;

        OpenJPAStateManager sm;
        for (Iterator itr = states.iterator(); itr.hasNext();) {
            sm = (OpenJPAStateManager) itr.next();

            if (sm.getPCState() == PCState.PNEW && !sm.isFlushed()) {
                if (_inserts == null)
                    _inserts = new ArrayList();
                _inserts.add(sm);

                // may have been re-persisted
                if (_deletes != null)
                    _deletes.remove(sm);
            } else if (_inserts != null
                && (sm.getPCState() == PCState.PNEWDELETED
                || sm.getPCState() == PCState.PNEWFLUSHEDDELETED))
                _inserts.remove(sm);
            else if (sm.getPCState() == PCState.PDIRTY) {
                if (_updates == null)
                    _updates = new HashMap();
                _updates.put(sm, sm.getDirty());
            } else if (sm.getPCState() == PCState.PDELETED) {
                if (_deletes == null)
                    _deletes = new HashSet();
                _deletes.add(sm);
            }
        }
View Full Code Here

     * somewhat tolerant of such behavior, in that the cache will be cleaned
     * up as failures occur.
     */
    private void notifyOptimisticLockFailure(OptimisticException e) {
        Object o = e.getFailedObject();
        OpenJPAStateManager sm = _ctx.getStateManager(o);
        if (sm == null)
            return;
        Object oid = sm.getId();
        boolean remove;

        // this logic could be more efficient -- we could aggregate
        // all the cache->oid changes, and then use DataCache.removeAll()
        // and less write locks to do the mutation.
        ClassMetaData meta = sm.getMetaData();
        DataCache cache = meta.getDataCache();
        if (cache == null)
            return;

        cache.writeLock();
        try {
            DataCachePCData data = cache.get(oid);
            if (data == null)
                return;

            switch (compareVersion(sm, sm.getVersion(), data.getVersion())) {
                case StoreManager.VERSION_LATER:
                case StoreManager.VERSION_SAME:
                    // This tx's current version is later than or the same as
                    // the data cache version. In this case, the commit should
                    // have succeeded from the standpoint of the cache. Remove
                    // the instance from cache in the hopes that the cache is
                    // out of sync.
                    remove = true;
                    break;
                case StoreManager.VERSION_EARLIER:
                    // This tx's current version is earlier than the data
                    // cache version. This is a normal optimistic lock failure.
                    // Do not clean up the cache; it probably already has the
                    // right values, and if not, it'll get cleaned up by a tx
                    // that fails in one of the other case statements.
                    remove = false;
                    break;
                case StoreManager.VERSION_DIFFERENT:
                    // The version strategy for the failed object does not
                    // store enough information to optimize for expected
                    // failures. Clean up the cache.
                    remove = true;
                    break;
                default:
                    // Unexpected return value. Remove to be future-proof.
                    remove = true;
                    break;
            }
            if (remove)
                // remove directly instead of via the RemoteCommitListener
                // since we have a write lock here already, so this is more
                // efficient than read-locking and then write-locking later.
                cache.remove(sm.getId());
        } finally {
            cache.writeUnlock();
        }

        // fire off a remote commit stalenesss detection event.
View Full Code Here

     */
    private void updateCaches() {
        // map each data cache to the modifications we need to perform
        Map modMap = null;
        Modifications mods;
        OpenJPAStateManager sm;
        DataCachePCData data;
        DataCache cache;

        // create pc datas for inserts
        if (_ctx.getPopulateDataCache() && _inserts != null) {
            for (Iterator itr = _inserts.iterator(); itr.hasNext();) {
                sm = (OpenJPAStateManager) itr.next();
                cache = sm.getMetaData().getDataCache();
                if (cache == null)
                    continue;

                if (modMap == null)
                    modMap = new HashMap();
                mods = getModifications(modMap, cache);
                data = newPCData(sm);
                data.store(sm);
                mods.additions.add(new PCDataHolder(data, sm));
            }
        }

        // update pcdatas for updates
        Map.Entry entry;
        if (_updates != null) {
            BitSet fields;
            for (Iterator itr = _updates.entrySet().iterator();
                itr.hasNext();) {
                entry = (Map.Entry) itr.next();
                sm = (OpenJPAStateManager) entry.getKey();
                fields = (BitSet) entry.getValue();

                cache = sm.getMetaData().getDataCache();
                if (cache == null)
                    continue;

                // it's ok not to clone the object that we get from the cache,
                // since we're inside the commit() method, so any modifications
                // to the underlying cache are valid. If the commit had not
                // already succeeded, then we'd want to clone the retrieved
                // object.
                if (modMap == null)
                    modMap = new HashMap();
                data = cache.get(sm.getObjectId());
                mods = getModifications(modMap, cache);

                // data should always be non-null, since the object is
                // dirty, but maybe it got dropped from the cache in the
                // interim
                if (data == null) {
                    data = newPCData(sm);
                    data.store(sm);
                    mods.newUpdates.add(new PCDataHolder(data, sm));
                } else {
                    data.store(sm, fields);
                    mods.existingUpdates.add(new PCDataHolder(data, sm));
                }
            }
        }

        // remove pcdatas for deletes
        if (_deletes != null) {
            for (Iterator itr = _deletes.iterator(); itr.hasNext();) {
                sm = (OpenJPAStateManager) itr.next();
                cache = sm.getMetaData().getDataCache();
                if (cache == null)
                    continue;

                if (modMap == null)
                    modMap = new HashMap();
                mods = getModifications(modMap, cache);
                mods.deletes.add(sm.getObjectId());
            }
        }

        // notify the caches of the changes
        if (modMap != null) {
View Full Code Here

    public void update(OpenJPAStateManager sm, JDBCStore store, RowManager rm)
        throws SQLException {
        if (field.getMappedBy() != null)
            return;

        OpenJPAStateManager rel = RelationStrategies.getStateManager
            (sm.fetchObjectField(field.getIndex()), store.getContext());

        if (field.getJoinDirection() == field.JOIN_INVERSE) {
            nullInverse(sm, rm);
            updateInverse(sm, rel, store, rm);
View Full Code Here

        if (field.getMappedBy() != null)
            return;

        if (field.getJoinDirection() == field.JOIN_INVERSE) {
            if (sm.getLoaded().get(field.getIndex())) {
                OpenJPAStateManager rel = RelationStrategies.getStateManager(sm.
                    fetchObjectField(field.getIndex()), store.getContext());
                updateInverse(sm, rel, store, rm);
            } else
                nullInverse(sm, rm);
        } else {
            field.deleteRow(sm, store, rm);

            // if our foreign key has a delete action, we need to set the
            // related object so constraints can be evaluated
            OpenJPAStateManager rel = RelationStrategies.getStateManager
                (sm.fetchObjectField(field.getIndex()), store.getContext());
            if (rel != null) {
                ForeignKey fk = field.getForeignKey((ClassMapping)
                    rel.getMetaData());
                if (fk.getDeleteAction() == ForeignKey.ACTION_RESTRICT) {
                    Row row = field.getRow(sm, store, rm, Row.ACTION_DELETE);
                    row.setForeignKey(fk, null, rel);
                }
            }
View Full Code Here

TOP

Related Classes of org.apache.openjpa.kernel.OpenJPAStateManager

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.