Package org.datanucleus.store

Examples of org.datanucleus.store.ExecutionContext$LifecycleListener


     **/
    protected int internalIndexOf(ObjectProvider sm, Object element, String stmt, ElementContainerStore ecs)
    {
        try
        {
            ExecutionContext ec = sm.getExecutionContext();
            ManagedConnection mconn = ecs.getStoreManager().getConnection(ec);
            SQLController sqlControl = storeMgr.getSQLController();
            try
            {
                PreparedStatement ps = sqlControl.getStatementForUpdate(mconn, stmt, false);
View Full Code Here


    {
        String stmt = getIndicesOfStmt(elements, ecs);
        int[] indices = new int[elements.size()];
        try
        {
            ExecutionContext ec = sm.getExecutionContext();
            ManagedConnection mconn = ecs.getStoreManager().getConnection(ec);
            SQLController sqlControl = storeMgr.getSQLController();
            try
            {
                PreparedStatement ps = sqlControl.getStatementForUpdate(mconn, stmt, false);
View Full Code Here

        else
        {
            currentListSize = size;
        }

        ExecutionContext ec = sm.getExecutionContext();
        try
        {
            ManagedConnection mconn = ecs.getStoreManager().getConnection(ec);
            SQLController sqlControl = storeMgr.getSQLController();
            try
View Full Code Here

            int amount, boolean executeNow, ElementContainerStore ecs) throws MappedDatastoreException
    {
        JavaTypeMapping orderMapping = ecs.getOrderMapping();
        JavaTypeMapping relationDiscriminatorMapping = ecs.getRelationDiscriminatorMapping();

        ExecutionContext ec = ownerSM.getExecutionContext();
        SQLController sqlControl = storeMgr.getSQLController();
        String shiftStmt = getShiftStmt(ecs);
        try
        {
            PreparedStatement ps = sqlControl.getStatementForUpdate(conn, shiftStmt, false);
View Full Code Here

    }

    protected boolean updateElementFkInternal(ObjectProvider sm, Object element, Object owner)
    {
        boolean retval;
        ExecutionContext ec = sm.getExecutionContext();
        String stmt = getUpdateFkStmt();
        try
        {
            ManagedConnection mconn = getStoreMgr().getConnection(ec);
            SQLController sqlControl = getStoreMgr().getSQLController();
View Full Code Here

     * @param ownerSM State Manager for the set.
     * @return Iterator for the set.
     */
    public Iterator iterator(ObjectProvider ownerSM)
    {
        ExecutionContext ec = ownerSM.getExecutionContext();
        if (iteratorStmtLocked == null)
        {
            synchronized (this) // Make sure this completes in case another thread needs the same info
            {
                // Generate the statement, and statement mapping/parameter information
                SQLStatement sqlStmt = getSQLStatementForIterator(ownerSM);
                iteratorStmtUnlocked = sqlStmt.getSelectStatement().toSQL();
                sqlStmt.addExtension("lock-for-update", true);
                iteratorStmtLocked = sqlStmt.getSelectStatement().toSQL();
            }
        }

        Transaction tx = ec.getTransaction();
        String stmt = (tx.lockReadObjects() ? iteratorStmtLocked : iteratorStmtUnlocked);
        try
        {
            ManagedConnection mconn = storeMgr.getConnection(ec);
            SQLController sqlControl = ((RDBMSStoreManager)storeMgr).getSQLController();
View Full Code Here

        }

        try
        {
            VersionMetaData vermd = table.getVersionMetaData();
            ExecutionContext ec = sm.getExecutionContext();
            RDBMSStoreManager storeMgr = (RDBMSStoreManager)ec.getStoreManager();
            if (vermd != null && vermd.getFieldName() != null)
            {
                // Version field - Update the version in the object
                AbstractMemberMetaData verfmd =
                    ((AbstractClassMetaData)vermd.getParent()).getMetaDataForMember(vermd.getFieldName());
                Object currentVersion = ec.getApiAdapter().getVersion(sm);
                if (currentVersion instanceof Number)
                {
                    // Cater for Integer based versions
                    currentVersion = Long.valueOf(((Number)currentVersion).longValue());
                }

                Object nextOptimisticVersion = VersionHelper.getNextVersion(table.getVersionMetaData().getVersionStrategy(), currentVersion);
                if (verfmd.getType() == Integer.class || verfmd.getType() == int.class)
                {
                    // Cater for Integer based versions TODO Generalise this
                    nextOptimisticVersion = Integer.valueOf(((Long)nextOptimisticVersion).intValue());
                }
                sm.replaceField(verfmd.getAbsoluteFieldNumber(), nextOptimisticVersion);
            }

            // Set the state to "inserting" (may already be at this state if multiple inheritance level INSERT)
            sm.changeActivityState(ActivityState.INSERTING);

            SQLController sqlControl = storeMgr.getSQLController();
            ManagedConnection mconn = storeMgr.getConnection(ec);
            try
            {
                PreparedStatement ps = sqlControl.getStatementForUpdate(mconn, insertStmt, batch);

                try
                {
                    StatementClassMapping mappingDefinition = new StatementClassMapping();
                    StatementMappingIndex[] idxs = stmtMappings;
                    for (int i=0;i<idxs.length;i++)
                    {
                        if (idxs[i] != null)
                        {
                            mappingDefinition.addMappingForMember(i, idxs[i]);
                        }
                    }

                    // Provide the primary key field(s)
                    if (table.getIdentityType() == IdentityType.DATASTORE)
                    {
                        if (!table.isObjectIdDatastoreAttributed() || !table.isBaseDatastoreClass())
                        {
                            int[] paramNumber = {IDPARAMNUMBER};
                            table.getDatastoreObjectIdMapping().setObject(ec, ps, paramNumber, sm.getInternalObjectId());
                        }
                    }
                    else if (table.getIdentityType() == IdentityType.APPLICATION)
                    {
                        sm.provideFields(pkFieldNumbers,
                            storeMgr.getFieldManagerForStatementGeneration(sm, ps, mappingDefinition, true));
                    }

                    // Provide all non-key fields needed for the insert.
                    // This provides "persistence-by-reachability" for these fields
                    int numberOfFieldsToProvide = 0;
                    for (int i = 0; i < insertFieldNumbers.length; i++)
                    {
                        if (insertFieldNumbers[i] < sm.getClassMetaData().getMemberCount())
                        {
                            numberOfFieldsToProvide++;
                        }
                    }
                    int j = 0;
                    int[] classFieldNumbers = new int[numberOfFieldsToProvide];
                    for (int i = 0; i < insertFieldNumbers.length; i++)
                    {
                        if (insertFieldNumbers[i] < sm.getClassMetaData().getMemberCount())
                        {
                            classFieldNumbers[j++] = insertFieldNumbers[i];
                        }
                    }
                    sm.provideFields(classFieldNumbers,
                        storeMgr.getFieldManagerForStatementGeneration(sm, ps, mappingDefinition, true));

                    if (table.getVersionMapping(false) != null)
                    {
                        // Surrogate version - set the new version for the object
                        Object currentVersion = ec.getApiAdapter().getVersion(sm);
                        Object nextOptimisticVersion = VersionHelper.getNextVersion(table.getVersionMetaData().getVersionStrategy(), currentVersion);
                        for (int k=0;k<versionStmtMapping.getNumberOfParameterOccurrences();k++)
                        {
                            table.getVersionMapping(false).setObject(ec, ps,
                                versionStmtMapping.getParameterPositionsForOccurrence(k), nextOptimisticVersion);
                        }
                        sm.setTransactionalVersion(nextOptimisticVersion);
                    }
                    else if (vermd != null && vermd.getFieldName() != null)
                    {
                        // Version field - set the new version for the object
                        Object currentVersion = ec.getApiAdapter().getVersion(sm);
                        Object nextOptimisticVersion = VersionHelper.getNextVersion(table.getVersionMetaData().getVersionStrategy(), currentVersion);
                        sm.setTransactionalVersion(nextOptimisticVersion);
                    }

                    // Discriminator mapping (optional)
                    if (table.getDiscriminatorMapping(false) != null)
                    {
                        DiscriminatorMetaData dismd = table.getDiscriminatorMetaData();
                        if (dismd.getStrategy() == DiscriminatorStrategy.CLASS_NAME)
                        {
                            for (int k=0;k<discriminatorStmtMapping.getNumberOfParameterOccurrences();k++)
                            {
                                table.getDiscriminatorMapping(false).setObject(ec, ps,
                                    discriminatorStmtMapping.getParameterPositionsForOccurrence(k),
                                    sm.getObject().getClass().getName());
                            }
                        }
                        else if (dismd.getStrategy() == DiscriminatorStrategy.VALUE_MAP)
                        {
                            // Use Discriminator info for the actual class
                            dismd = sm.getClassMetaData().getInheritanceMetaData().getDiscriminatorMetaData();
                            for (int k=0;k<discriminatorStmtMapping.getNumberOfParameterOccurrences();k++)
                            {
                                table.getDiscriminatorMapping(false).setObject(ec, ps,
                                    discriminatorStmtMapping.getParameterPositionsForOccurrence(k), dismd.getValue());
                            }
                        }
                    }

                    // External FK columns (optional)
                    if (externalFKStmtMappings != null)
                    {
                        for (int i=0;i<externalFKStmtMappings.length;i++)
                        {
                            Object fkValue = sm.getAssociatedValue(externalFKStmtMappings[i].getMapping());
                            if (fkValue != null)
                            {
                                // Need to provide the owner field number so PCMapping can work out if it is inserted yet
                                AbstractMemberMetaData ownerFmd =
                                    table.getMetaDataForExternalMapping(externalFKStmtMappings[i].getMapping(),
                                        MappingConsumer.MAPPING_TYPE_EXTERNAL_FK);
                                for (int k=0;k<externalFKStmtMappings[i].getNumberOfParameterOccurrences();k++)
                                {
                                    externalFKStmtMappings[i].getMapping().setObject(ec, ps,
                                        externalFKStmtMappings[i].getParameterPositionsForOccurrence(k),
                                        fkValue, null, ownerFmd.getAbsoluteFieldNumber());
                                }
                            }
                            else
                            {
                                // We're inserting a null so dont need the owner field
                                for (int k=0;k<externalFKStmtMappings[i].getNumberOfParameterOccurrences();k++)
                                {
                                    externalFKStmtMappings[i].getMapping().setObject(ec, ps,
                                        externalFKStmtMappings[i].getParameterPositionsForOccurrence(k), null);
                                }
                            }
                        }
                    }

                    // External FK discriminator columns (optional)
                    if (externalFKDiscrimStmtMappings != null)
                    {
                        for (int i=0;i<externalFKDiscrimStmtMappings.length;i++)
                        {
                            Object discrimValue = sm.getAssociatedValue(externalFKDiscrimStmtMappings[i].getMapping());
                            for (int k=0;k<externalFKDiscrimStmtMappings[i].getNumberOfParameterOccurrences();k++)
                            {
                                externalFKDiscrimStmtMappings[i].getMapping().setObject(ec, ps,
                                    externalFKDiscrimStmtMappings[i].getParameterPositionsForOccurrence(k), discrimValue);
                            }
                        }
                    }

                    // External order columns (optional)
                    if (externalOrderStmtMappings != null)
                    {
                        for (int i=0;i<externalOrderStmtMappings.length;i++)
                        {
                            Object orderValue = sm.getAssociatedValue(externalOrderStmtMappings[i].getMapping());
                            if (orderValue == null)
                            {
                                // No order value so use -1
                                orderValue = Integer.valueOf(-1);
                            }
                            for (int k=0;k<externalOrderStmtMappings[i].getNumberOfParameterOccurrences();k++)
                            {
                                externalOrderStmtMappings[i].getMapping().setObject(ec, ps,
                                    externalOrderStmtMappings[i].getParameterPositionsForOccurrence(k), orderValue);
                            }
                        }
                    }

                    sqlControl.executeStatementUpdate(mconn, insertStmt, ps, !batch);

                    if (hasIdentityColumn)
                    {
                        // Identity was set in the datastore using auto-increment/identity/serial etc
                        Object newId = getInsertedDatastoreIdentity(ec, sqlControl, sm, mconn, ps);
                        if (NucleusLogger.DATASTORE_PERSIST.isDebugEnabled())
                        {
                            NucleusLogger.DATASTORE_PERSIST.debug(LOCALISER.msg("052206",
                                sm.toPrintableID(), newId));
                        }
                        sm.setPostStoreNewObjectId(newId);
                    }

                    // Execute any mapping actions on the insert of the fields (e.g Oracle CLOBs/BLOBs)
                    for (int i = 0; i < callbacks.length; ++i)
                    {
                        if (NucleusLogger.PERSISTENCE.isDebugEnabled())
                        {
                            NucleusLogger.PERSISTENCE.debug(LOCALISER.msg("052222",
                                sm.toPrintableID(),
                                ((JavaTypeMapping)callbacks[i]).getMemberMetaData().getFullFieldName()));
                        }
                        callbacks[i].insertPostProcessing(sm);
                    }

                    // Update the insert status for this table via the StoreManager
                    storeMgr.setObjectIsInsertedToLevel(sm, table);

                    // Make sure all relation fields (1-1, N-1 with FK) we processed in the INSERT are attached.
                    // This is necessary because with a bidir relation and the other end attached we can just
                    // do the INSERT above first and THEN attach the other end here
                    // (if we did it the other way around we would get a NotYetFlushedException thrown above).
                    for (int i=0;i<relationFieldNumbers.length;i++)
                    {
                        Object value = sm.provideField(relationFieldNumbers[i]);
                        if (value != null && ec.getApiAdapter().isDetached(value))
                        {
                            Object valueAttached = ec.persistObjectInternal(value, null, -1, ObjectProvider.PC);
                            sm.replaceField(relationFieldNumbers[i], valueAttached);
                        }
                    }

                    // Perform reachability on all fields that have no datastore column (1-1 bi non-owner, N-1 bi join)
View Full Code Here

        List exceptions = new ArrayList();
        boolean batched = allowsBatching() && length > 1;

        try
        {
            ExecutionContext ec = ownerSM.getExecutionContext();
            ManagedConnection mconn = storeMgr.getConnection(ec);
            try
            {
                getSpecialization().processBatchedWrites(mconn);
View Full Code Here

     * Method to be called after any update of the owner class element.
     * @param ownerOP ObjectProvider of the owner
     */
    public void postUpdate(ObjectProvider ownerOP)
    {
        ExecutionContext ec = ownerOP.getExecutionContext();
        MappedStoreManager storeMgr = datastoreContainer.getStoreManager();
        java.util.Map value = (java.util.Map) ownerOP.provideField(mmd.getAbsoluteFieldNumber());
        if (containerIsStoredInSingleColumn())
        {
            // Do nothing when serialised since we are handled in the main request
            if (value != null)
            {
                // Make sure the keys/values are ok for proceeding
                SCOUtils.validateObjectsForWriting(ownerOP, value.keySet());
                SCOUtils.validateObjectsForWriting(ownerOP, value.values());
            }
            return;
        }

        if (value == null)
        {
            // replace null map with empty SCO wrapper
            replaceFieldWithWrapper(ownerOP, null, false, false);
            return;
        }

        if (value instanceof SCOContainer)
        {
            SCOContainer sco = (SCOContainer) value;

            if (ownerOP.getObject() == sco.getOwner() && mmd.getName().equals(sco.getFieldName()))
            {
                // Flush any outstanding updates
                sco.flush();

                return;
            }

            if (sco.getOwner() != null)
            {
                throw new NucleusException("Owned second-class object was somehow assigned to a field other than its owner's").setFatal();
            }
        }

        if (!mmd.isCascadeUpdate())
        {
            // User doesnt want to update by reachability
            if (NucleusLogger.PERSISTENCE.isDebugEnabled())
            {
                NucleusLogger.PERSISTENCE.debug(LOCALISER.msg("007008", mmd.getFullFieldName()));
            }
            return;
        }
        if (NucleusLogger.PERSISTENCE.isDebugEnabled())
        {
            NucleusLogger.PERSISTENCE.debug(LOCALISER.msg("007009", mmd.getFullFieldName()));
        }

        // Update the datastore with this value of map (clear old entries and add new ones)
        // This method could be called in two situations
        // 1). Update a map field of an object, so UpdateRequest is called, which calls here
        // 2). Persist a new object, and it needed to wait til the element was inserted so
        //     goes into dirty state and then flush() triggers UpdateRequest, which comes here
        MapStore store = ((MapStore) storeMgr.getBackingStoreForField(
            ec.getClassLoaderResolver(), mmd, value.getClass()));

        // TODO Consider making this more efficient picking the ones to remove/add
        // e.g use an update() method on the backing store like for CollectionStore
        store.clear(ownerOP);
        store.putAll(ownerOP, value);
View Full Code Here

        boolean modified = false;

        try
        {
            ExecutionContext ec = sm.getExecutionContext();
            ManagedConnection mconn = storeMgr.getConnection(ec);

            try
            {
                // Add a row to the join table
View Full Code Here

TOP

Related Classes of org.datanucleus.store.ExecutionContext$LifecycleListener

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.