Package org.datanucleus.store.connection

Examples of org.datanucleus.store.connection.ManagedConnection


        boolean modified = false;
        ExecutionContext ec = sm.getExecutionContext();

        try
        {
            ManagedConnection mconn = storeMgr.getConnection(ec);
            try
            {
                int[] num = getSpecialization().internalAdd(sm, mconn, false, element, true, this);
                if (num[0] > 0)
                {
                    modified = true;
                }
            }
            finally
            {
                mconn.release();
            }
        }
        catch (MappedDatastoreException e)
        {
            NucleusLogger.DATASTORE.error(e);
View Full Code Here


        {
            validateElementForWriting(sm, iter.next(), null);
        }

        ExecutionContext ec = sm.getExecutionContext();
        ManagedConnection mconn = storeMgr.getConnection(ec);
        try
        {
            // Loop through all elements to be added
            iter = elements.iterator();
            Object element = null;
            int[] returnCode = null;
            while (iter.hasNext())
            {
                element = iter.next();

                try
                {
                    returnCode = getSpecialization().internalAdd(sm, mconn, batched, element, !batched || (batched && !iter.hasNext()), this);
                }
                catch (MappedDatastoreException mde)
                {
                    exceptions.add(mde);
                    NucleusLogger.DATASTORE.error(mde);
                }
            }

            if (exceptions.size() == 0)
            {
                if (returnCode == null)
                {
                    modified = false;
                }
                else
                {
                    for (int i=0;i<returnCode.length;i++)
                    {
                        if (returnCode[i] > 0)
                        {
                            modified = true;
                        }
                    }
                }
            }
        }
        finally
        {
            mconn.release();
        }

        if (!exceptions.isEmpty())
        {
            // Throw all exceptions received as the cause of a NucleusDataStoreException so the user can see which record(s) didn't persist
View Full Code Here

        }

        try
        {
            ExecutionContext ec = sm.getExecutionContext();
            ManagedConnection mconn = storeMgr.getConnection(ec);
            try
            {
                Object element = null;
                iter = elements.iterator();
                getSpecialization().preInternalRemove(mconn);

                while (iter.hasNext())
                {
                    element = iter.next();
                    try
                    {
                        // Process the remove
                        int[] rc = getSpecialization().internalRemove(
                            sm, mconn, batched, element, !batched || (batched && !iter.hasNext()), this);
                        if (rc != null)
                        {
                            for (int i=0;i<rc.length;i++)
                            {
                                if (rc[i] > 0)
                                {
                                    // At least one record was inserted
                                    modified = true;
                                }
                            }
                        }
                    }
                    catch (MappedDatastoreException mde)
                    {
                        mde.printStackTrace();
                        exceptions.add(mde);
                        NucleusLogger.DATASTORE.error(mde);
                    }
                }
            }
            finally
            {
                mconn.release();
            }
        }
        catch (MappedDatastoreException e)
        {
            e.printStackTrace();
View Full Code Here

            boolean batch = false;
            // TODO Set the batch flag based on whether we have no other SQL being invoked in here just our UPDATE
            try
            {
                ManagedConnection mconn = storeMgr.getConnection(ec);
                SQLController sqlControl = storeMgr.getSQLController();

                try
                {
                    // Perform the update
                    PreparedStatement ps = sqlControl.getStatementForUpdate(mconn, stmt, batch);
                    try
                    {
                        Object currentVersion = sm.getTransactionalVersion();
                        Object nextVersion = null;
                        if (versionMetaData != null)
                        {
                            // Set the next version in the object
                            if (versionMetaData.getFieldName() != null)
                            {
                                // Version field
                                AbstractMemberMetaData verfmd = cmd.getMetaDataForMember(table.getVersionMetaData().getFieldName());
                                if (currentVersion instanceof Number)
                                {
                                    // Cater for Integer-based versions
                                    currentVersion = Long.valueOf(((Number)currentVersion).longValue());
                                }
                                nextVersion = VersionHelper.getNextVersion(versionMetaData.getVersionStrategy(), currentVersion);
                                if (verfmd.getType() == Integer.class || verfmd.getType() == int.class)
                                {
                                    // Cater for Integer-based versions TODO Generalise this
                                    nextVersion = Integer.valueOf(((Long)nextVersion).intValue());
                                }
                                sm.replaceField(verfmd.getAbsoluteFieldNumber(), nextVersion);
                            }
                            else
                            {
                                // Surrogate version column
                                nextVersion = VersionHelper.getNextVersion(versionMetaData.getVersionStrategy(), currentVersion);
                            }
                            sm.setTransactionalVersion(nextVersion);
                        }

                        // SELECT clause - set the required fields to be updated
                        if (updateFieldNumbers != null)
                        {
                            StatementClassMapping mappingDefinition = new StatementClassMapping();
                            StatementMappingIndex[] idxs = stmtMappingDefinition.getFields();
                            for (int i=0;i<idxs.length;i++)
                            {
                                if (idxs[i] != null)
                                {
                                    mappingDefinition.addMappingForMember(i, idxs[i]);
                                }
                            }
                            sm.provideFields(updateFieldNumbers,
                                storeMgr.getFieldManagerForStatementGeneration(sm, ps, mappingDefinition, true));
                        }

                        if (versionMetaData != null && versionMetaData.getFieldName() == null)
                        {
                            // SELECT clause - set the surrogate version column to the new version
                            StatementMappingIndex mapIdx = stmtMappingDefinition.getVersion();
                            for (int i=0;i<mapIdx.getNumberOfParameterOccurrences();i++)
                            {
                                table.getVersionMapping(false).setObject(ec, ps,
                                    mapIdx.getParameterPositionsForOccurrence(i), nextVersion);
                            }
                        }

                        // WHERE clause - primary key fields
                        if (table.getIdentityType() == IdentityType.DATASTORE)
                        {
                            // a). datastore identity
                            StatementMappingIndex mapIdx = stmtMappingDefinition.getDatastoreId();
                            for (int i=0;i<mapIdx.getNumberOfParameterOccurrences();i++)
                            {
                                table.getDatastoreObjectIdMapping().setObject(ec, ps,
                                    mapIdx.getParameterPositionsForOccurrence(i), sm.getInternalObjectId());
                            }
                        }
                        else if (table.getIdentityType() == IdentityType.APPLICATION)
                        {
                            // b). application identity
                            StatementClassMapping mappingDefinition = new StatementClassMapping();
                            StatementMappingIndex[] idxs = stmtMappingDefinition.getPrimaryKeys();
                            for (int i=0;i<idxs.length;i++)
                            {
                                if (idxs[i] != null)
                                {
                                    mappingDefinition.addMappingForMember(i, idxs[i]);
                                }
                            }
                            sm.provideFields(pkFieldNumbers,
                                storeMgr.getFieldManagerForStatementGeneration(sm, ps, mappingDefinition, true));
                        }

                        if (optimisticChecks)
                        {
                            if (currentVersion == null)
                            {
                                // Somehow the version is not set on this object (not read in ?) so report the bug
                                String msg = LOCALISER.msg("052201",
                                    sm.getInternalObjectId(), table);
                                NucleusLogger.PERSISTENCE.error(msg);
                                throw new NucleusException(msg);
                            }
                            // WHERE clause - current version discriminator
                            StatementMappingIndex mapIdx = stmtMappingDefinition.getVersion2();
                            for (int i=0;i<mapIdx.getNumberOfParameterOccurrences();i++)
                            {
                                mapIdx.getMapping().setObject(ec, ps,
                                    mapIdx.getParameterPositionsForOccurrence(i), currentVersion);
                            }
                        }

                        int[] rcs = sqlControl.executeStatementUpdate(mconn, stmt, ps, !batch);
                        if (rcs[0] == 0 && optimisticChecks)
                        {
                            // No object updated so either object disappeared or failed optimistic version checks
                            // TODO Batching : when we use batching here we need to process these somehow
                            String msg = LOCALISER.msg("052203",
                                sm.toPrintableID(), sm.getInternalObjectId(),
                                "" + currentVersion);
                            NucleusLogger.PERSISTENCE.error(msg);
                            throw new NucleusOptimisticException(msg, sm.getObject());
                        }
                    }
                    finally
                    {
                        sqlControl.closeStatement(mconn, ps);
                    }
                }
                finally
                {
                    mconn.release();
                }
            }
            catch (SQLException e)
            {
                String msg = LOCALISER.msg("052215",
View Full Code Here

                // Calculate the amount we need to shift any existing elements by
                // This is used where inserting between existing elements and have to shift down all elements after the start point
                int shift = c.size();

                ExecutionContext ec = sm.getExecutionContext();
                ManagedConnection mconn = storeMgr.getConnection(ec);
                try
                {
                    // shift up existing elements after start position by "shift"
                    for (int i=currentListSize-1; i>=startAt; i--)
                    {
                        // Shift the index of this row by "shift"
                        getSpecialization().internalShift(sm, mconn, false, i, shift, true, this
                        );
                    }
                }
                finally
                {
                    mconn.release();
                }
            }
            catch (MappedDatastoreException e)
            {
                // An error was encountered during the shift process so abort here
View Full Code Here

        }
        else
        {
            // Ordered List - just remove the list item since no indexing present
            ExecutionContext ec = ownerSM.getExecutionContext();
            ManagedConnection mconn = storeMgr.getConnection(ec);
            try
            {
                int[] rcs = getSpecialization().internalRemove(ownerSM, mconn, false, element, true, this);
                if (rcs != null)
                {
                    if (rcs[0] > 0)
                    {
                        modified = true;
                    }
                }
            }
            catch (MappedDatastoreException sqe)
            {
                String msg = LOCALISER.msg("056012", sqe.getMessage());
                NucleusLogger.DATASTORE.error(msg, sqe.getCause());
                throw new NucleusDataStoreException(msg, sqe, ownerSM.getObject());
            }
            finally
            {
                mconn.release();
            }
        }

        return modified;
    }
View Full Code Here

   * @param ec ExecutionContext
   * @return The KeyRegistry
   */
  public static KeyRegistry getKeyRegistry(ExecutionContext ec) {
    StoreManager storeManager = ec.getStoreManager();
    ManagedConnection mconn = storeManager.getConnection(ec);
    return ((EmulatedXAResource) mconn.getXAResource()).getKeyRegistry();
  }
View Full Code Here

        return EntityUtils.entityToPojo(from, acmd, clr, ec, true, ec.getFetchPlan().getCopy());
      }
    };

    AbstractJavaQuery query = new DummyQuery(ec.getStoreManager(), ec);
    final ManagedConnection mconn = ec.getStoreManager().getConnection(ec);
    try {
      List<T> results = (List<T>) DatastoreQuery.newStreamingQueryResultForEntities(
          queryResultIterable, func, endCursor, query);

      if (results instanceof AbstractQueryResult) {
        // Lazy loading results : add listener to the connection so we can get a callback when the connection is flushed.
        final AbstractQueryResult qr = (AbstractQueryResult)results;
        ManagedConnectionResourceListener listener = new ManagedConnectionResourceListener() {
          public void managedConnectionPreClose() {
            // Disconnect the query from this ManagedConnection (read in unread rows etc)
            qr.disconnect();
          }
          public void managedConnectionPostClose() {}
          public void resourcePostClose() {
            mconn.removeListener(this);
          }
          public void transactionFlushed() {}
          public void transactionPreClose() {
            // Disconnect the query from this ManagedConnection (read in unread rows etc)
            qr.disconnect();
          }
        };
        mconn.addListener(listener);
        qr.addConnectionListener(listener);
      }
      return results;
    } finally {
      mconn.release();
    }
  }
View Full Code Here

      }

      if (results instanceof AbstractQueryResult) {
        // Lazy loading results : add listener to the connection so we can get a callback when the connection is flushed.
        final AbstractQueryResult qr = (AbstractQueryResult)results;
        final ManagedConnection mconn = getStoreManager().getConnection(ec);
        ManagedConnectionResourceListener listener = new ManagedConnectionResourceListener() {
          public void managedConnectionPreClose() {
            // Disconnect the query from this ManagedConnection (read in unread rows etc)
            qr.disconnect();
          }
          public void managedConnectionPostClose() {}
          public void resourcePostClose() {
            mconn.removeListener(this);
          }
          public void transactionFlushed() {}
          public void transactionPreClose() {
            // Disconnect the query from this ManagedConnection (read in unread rows etc)
            qr.disconnect();
          }
        };
        mconn.addListener(listener);
        qr.addConnectionListener(listener);
      }
    }

    if (NucleusLogger.QUERY.isDebugEnabled()) {
View Full Code Here

      }

      if (results instanceof AbstractQueryResult) {
        // Lazy loading results : add listener to the connection so we can get a callback when the connection is flushed.
        final AbstractQueryResult qr = (AbstractQueryResult)results;
        final ManagedConnection mconn = getStoreManager().getConnection(ec);
        ManagedConnectionResourceListener listener = new ManagedConnectionResourceListener() {
          public void managedConnectionPreClose() {
            // Disconnect the query from this ManagedConnection (read in unread rows etc)
            qr.disconnect();
          }
          public void managedConnectionPostClose() {}
          public void resourcePostClose() {
            mconn.removeListener(this);
          }
          public void transactionFlushed() {}
          public void transactionPreClose() {
            // Disconnect the query from this ManagedConnection (read in unread rows etc)
            qr.disconnect();
          }
        };
        mconn.addListener(listener);
        qr.addConnectionListener(listener);
      }
    }

    if (NucleusLogger.QUERY.isDebugEnabled()) {
View Full Code Here

TOP

Related Classes of org.datanucleus.store.connection.ManagedConnection

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.