Search in sources :

Example 21 with ObjectProvider

use of org.datanucleus.state.ObjectProvider in project datanucleus-rdbms by datanucleus.

the class BaseContainerStore method getObjectProviderForEmbeddedPCObject.

/**
 * Method to return the ObjectProvider for an embedded PC object (element, key, value).
 * It creates one if the element is not currently managed.
 * @param op ObjectProvider of the owner
 * @param obj The embedded PC object
 * @param ownerMmd The meta data for the owner field
 * @param pcType Object type for the embedded object (see ObjectProvider EMBEDDED_PC etc)
 * @return The ObjectProvider
 */
public ObjectProvider getObjectProviderForEmbeddedPCObject(ObjectProvider op, Object obj, AbstractMemberMetaData ownerMmd, short pcType) {
    ExecutionContext ec = op.getExecutionContext();
    ObjectProvider objOP = ec.findObjectProvider(obj);
    if (objOP == null) {
        objOP = ec.getNucleusContext().getObjectProviderFactory().newForEmbedded(ec, obj, false, op, ownerMmd.getAbsoluteFieldNumber());
    }
    objOP.setPcObjectType(pcType);
    return objOP;
}
Also used : ExecutionContext(org.datanucleus.ExecutionContext) ObjectProvider(org.datanucleus.state.ObjectProvider)

Example 22 with ObjectProvider

use of org.datanucleus.state.ObjectProvider in project datanucleus-rdbms by datanucleus.

the class ElementContainerStore method validateElementForWriting.

/**
 * Method to check if an element is already persistent, or is managed by a different ExecutionContext. If not persistent, this will persist it.
 * @param ec execution context
 * @param element The element
 * @param fieldValues any initial field values to use if persisting the element
 * @return Whether the element was persisted during this call
 */
protected boolean validateElementForWriting(ExecutionContext ec, Object element, FieldValues fieldValues) {
    // Check the element type for this collection
    if (!elementIsPersistentInterface && !validateElementType(ec.getClassLoaderResolver(), element)) {
        throw new ClassCastException(Localiser.msg("056033", element.getClass().getName(), ownerMemberMetaData.getFullFieldName(), elementType));
    }
    boolean persisted = false;
    if (elementsAreEmbedded || elementsAreSerialised) {
    // Element is embedded/serialised so has no id
    } else {
        ObjectProvider elementSM = ec.findObjectProvider(element);
        if (elementSM != null && elementSM.isEmbedded()) {
            // Element is already with ObjectProvider and is embedded in another field!
            throw new NucleusUserException(Localiser.msg("056028", ownerMemberMetaData.getFullFieldName(), element));
        }
        persisted = SCOUtils.validateObjectForWriting(ec, element, fieldValues);
    }
    return persisted;
}
Also used : NucleusUserException(org.datanucleus.exceptions.NucleusUserException) ObjectProvider(org.datanucleus.state.ObjectProvider)

Example 23 with ObjectProvider

use of org.datanucleus.state.ObjectProvider in project datanucleus-rdbms by datanucleus.

the class FlushReferential method execute.

/* (non-Javadoc)
     * @see org.datanucleus.FlushOrdered#execute(org.datanucleus.ExecutionContext, java.util.List, java.util.List, org.datanucleus.flush.OperationQueue)
     */
@Override
public List<NucleusOptimisticException> execute(ExecutionContext ec, List<ObjectProvider> primaryOPs, List<ObjectProvider> secondaryOPs, OperationQueue opQueue) {
    List<NucleusOptimisticException> flushExcps = null;
    // Phase 1 : Find all objects that have no relations or external FKs and process first
    Set<ObjectProvider> unrelatedOPs = null;
    if (primaryOPs != null) {
        Iterator<ObjectProvider> opIter = primaryOPs.iterator();
        while (opIter.hasNext()) {
            ObjectProvider op = opIter.next();
            if (!op.isEmbedded() && isClassSuitableForBatching(ec, op.getClassMetaData())) {
                if (unrelatedOPs == null) {
                    unrelatedOPs = new HashSet<>();
                }
                unrelatedOPs.add(op);
                opIter.remove();
            }
        }
    }
    if (secondaryOPs != null) {
        Iterator<ObjectProvider> opIter = secondaryOPs.iterator();
        while (opIter.hasNext()) {
            ObjectProvider op = opIter.next();
            if (!op.isEmbedded() && isClassSuitableForBatching(ec, op.getClassMetaData())) {
                if (unrelatedOPs == null) {
                    unrelatedOPs = new HashSet<>();
                }
                unrelatedOPs.add(op);
                opIter.remove();
            }
        }
    }
    if (unrelatedOPs != null) {
        // Process DELETEs, then INSERTs, then UPDATEs
        FlushNonReferential groupedFlush = new FlushNonReferential();
        flushExcps = groupedFlush.flushDeleteInsertUpdateGrouped(unrelatedOPs, ec);
    }
    // Phase 2 : Fallback to FlushOrdered handling for remaining objects
    List<NucleusOptimisticException> excps = super.execute(ec, primaryOPs, secondaryOPs, opQueue);
    // Return any exceptions
    if (excps != null) {
        if (flushExcps == null) {
            flushExcps = excps;
        } else {
            flushExcps.addAll(excps);
        }
    }
    return flushExcps;
}
Also used : FlushNonReferential(org.datanucleus.flush.FlushNonReferential) NucleusOptimisticException(org.datanucleus.exceptions.NucleusOptimisticException) ObjectProvider(org.datanucleus.state.ObjectProvider)

Example 24 with ObjectProvider

use of org.datanucleus.state.ObjectProvider in project datanucleus-core by datanucleus.

the class ExecutionContextImpl method persistObjectWork.

/**
 * Method to make an object persistent.
 * NOT to be called by internal DataNucleus methods.
 * @param obj The object
 * @return The persisted object
 * @throws NucleusUserException if the object is managed by a different manager
 */
private Object persistObjectWork(Object obj) {
    boolean detached = getApiAdapter().isDetached(obj);
    // Persist the object
    Object persistedPc = persistObjectInternal(obj, null, null, -1, ObjectProvider.PC);
    // If using reachability at commit and appropriate save it for reachability checks when we commit
    ObjectProvider op = findObjectProvider(persistedPc);
    if (op != null) {
        // TODO If attaching (detached=true), we maybe ought not add the ObjectProvider to dirtyOPs/indirectDirtyOPs
        if (indirectDirtyOPs.contains(op)) {
            dirtyOPs.add(op);
            indirectDirtyOPs.remove(op);
        } else if (!dirtyOPs.contains(op)) {
            dirtyOPs.add(op);
            if (l2CacheTxIds != null && nucCtx.isClassCacheable(op.getClassMetaData())) {
                l2CacheTxIds.add(op.getInternalObjectId());
            }
        }
        if (pbrAtCommitHandler != null && tx.isActive()) {
            if (detached || getApiAdapter().isNew(persistedPc)) {
                pbrAtCommitHandler.addPersistedObject(op.getInternalObjectId());
            }
        }
    }
    return persistedPc;
}
Also used : ObjectProvider(org.datanucleus.state.ObjectProvider)

Example 25 with ObjectProvider

use of org.datanucleus.state.ObjectProvider in project datanucleus-core by datanucleus.

the class ExecutionContextImpl method getObjectFromLevel2Cache.

/**
 * Convenience method to access an object in the Level 2 cache.
 * @param id Id of the object
 * @return Persistable object (with connected ObjectProvider).
 */
protected Object getObjectFromLevel2Cache(Object id) {
    Object pc = null;
    if (l2CacheEnabled) {
        if (!nucCtx.isClassWithIdentityCacheable(id)) {
            return null;
        }
        String cacheRetrieveMode = getLevel2CacheRetrieveMode();
        if ("bypass".equalsIgnoreCase(cacheRetrieveMode)) {
            // Cache retrieval currently turned off
            return null;
        }
        Level2Cache l2Cache = nucCtx.getLevel2Cache();
        CachedPC cachedPC = l2Cache.get(id);
        if (cachedPC != null) {
            // Create active version of cached object with ObjectProvider connected and same id
            ObjectProvider op = nucCtx.getObjectProviderFactory().newForCachedPC(this, id, cachedPC);
            // Object in P_CLEAN state
            pc = op.getObject();
            if (NucleusLogger.CACHE.isDebugEnabled()) {
                NucleusLogger.CACHE.debug(Localiser.msg("004006", IdentityUtils.getPersistableIdentityForId(id), StringUtils.intArrayToString(cachedPC.getLoadedFieldNumbers()), cachedPC.getVersion(), StringUtils.toJVMIDString(pc)));
            }
            if (tx.isActive() && tx.getOptimistic()) {
                // Optimistic txns, so return as P_NONTRANS (as per JDO spec)
                op.makeNontransactional();
            } else if (!tx.isActive() && getApiAdapter().isTransactional(pc)) {
                // Non-tx context, so return as P_NONTRANS (as per JDO spec)
                op.makeNontransactional();
            }
            return pc;
        }
        if (NucleusLogger.CACHE.isDebugEnabled()) {
            NucleusLogger.CACHE.debug(Localiser.msg("004005", IdentityUtils.getPersistableIdentityForId(id)));
        }
    }
    return null;
}
Also used : Level2Cache(org.datanucleus.cache.Level2Cache) CachedPC(org.datanucleus.cache.CachedPC) ObjectProvider(org.datanucleus.state.ObjectProvider)

Aggregations

ObjectProvider (org.datanucleus.state.ObjectProvider)160 ExecutionContext (org.datanucleus.ExecutionContext)85 Iterator (java.util.Iterator)43 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)34 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)25 Map (java.util.Map)22 NucleusDataStoreException (org.datanucleus.exceptions.NucleusDataStoreException)22 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)21 SQLException (java.sql.SQLException)17 Collection (java.util.Collection)16 ApiAdapter (org.datanucleus.api.ApiAdapter)16 NucleusObjectNotFoundException (org.datanucleus.exceptions.NucleusObjectNotFoundException)16 SCOCollectionIterator (org.datanucleus.store.types.SCOCollectionIterator)16 ArrayList (java.util.ArrayList)14 HashSet (java.util.HashSet)14 StatementMappingIndex (org.datanucleus.store.rdbms.query.StatementMappingIndex)14 NucleusException (org.datanucleus.exceptions.NucleusException)13 ManagedConnection (org.datanucleus.store.connection.ManagedConnection)13 ListIterator (java.util.ListIterator)12 SQLController (org.datanucleus.store.rdbms.SQLController)12