db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t...@apache.org
Subject cvs commit: jakarta-ojb/src/java/org/apache/ojb/odmg ObjectEnvelopeTable.java
Date Fri, 24 Jan 2003 22:46:30 GMT
thma        2003/01/24 14:46:30

  Modified:    src/java/org/apache/ojb/odmg ObjectEnvelopeTable.java
  Log:
  optimize commit operations
  1. implicit lockupgrade is done only in implicit lockmode
  2. reordering and commiting are performed only if there are dirty objects
  
  Revision  Changes    Path
  1.15      +144 -70   jakarta-ojb/src/java/org/apache/ojb/odmg/ObjectEnvelopeTable.java
  
  Index: ObjectEnvelopeTable.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ojb/src/java/org/apache/ojb/odmg/ObjectEnvelopeTable.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- ObjectEnvelopeTable.java	17 Jan 2003 18:03:34 -0000	1.14
  +++ ObjectEnvelopeTable.java	24 Jan 2003 22:46:30 -0000	1.15
  @@ -67,6 +67,7 @@
   import org.apache.ojb.broker.ManageableCollection;
   import org.apache.ojb.broker.PersistenceBroker;
   import org.apache.ojb.broker.OJBRuntimeException;
  +import org.apache.ojb.broker.PersistenceBrokerFactory;
   import org.apache.ojb.broker.accesslayer.ConnectionManagerIF;
   import org.apache.ojb.broker.metadata.ClassDescriptor;
   import org.apache.ojb.broker.metadata.CollectionDescriptor;
  @@ -108,6 +109,11 @@
        */
       private ArrayList mvOrderOfIds = new ArrayList();
   
  +    /**
  +     * marker used to avoid superfluous reordering and commiting
  +     */
  +    private boolean needsCommit = false;
  +
       public void close()
       {
           /**
  @@ -144,13 +150,17 @@
           PersistenceBroker broker = transaction.getBroker();
           ConnectionManagerIF connMan = broker.serviceConnectionManager();
           boolean saveBatchMode = connMan.isBatchMode();
  +        Iterator iter = null;
   
           try
           {
               if (log.isDebugEnabled())
               {
                   log.debug(
  -                    "PB is in intern tx: " + broker.isInTransaction() + "  broker was:
" + broker);
  +                    "PB is in internal tx: "
  +                        + broker.isInTransaction()
  +                        + "  broker was: "
  +                        + broker);
               }
               // all neccessary db operations are executed within a PersistenceBroker transaction:
               if (!broker.isInTransaction())
  @@ -167,47 +177,15 @@
   
               // 0. turn on the batch mode
               connMan.setBatchMode(true);
  -            // 1. upgrade locks
   
  -            /* Using iter.next() directly in the get() call causes a Concurrent Modification
  -             Exception when the process of commiting envolves object with complex relations
  -             some of which may refer to the same object through different relational paths.
  -             Iteration a temporary vector instead seams to solve the problem
  -             arminw:
  -             clone the list should solve the problem ditto??
  -             */
  -            Iterator iter = ((ArrayList) mvOrderOfIds.clone()).iterator();
  -            while (iter.hasNext())
  -            {
  -                ObjectEnvelope mod = (ObjectEnvelope) mhtObjectEnvelopes.get(iter.next());
  -                if (log.isDebugEnabled()) log.debug("commit: " + mod);
  -                // if the Object has been modified by transaction, mark object as dirty
  -                // but only if it has not been marked during tx already !! 
  -                if ( (! mod.needsDelete()) && (! mod.needsInsert()) &&
(! mod.needsUpdate())  )
  -                {
  -		            if (mod.hasChanged())
  -		            {
  -		                // implicitely acquire a write lock !
  -		                transaction.lock(mod.getObject(), Transaction.UPGRADE);
  -		                // mark object dirty
  -		                mod.setModificationState(mod.getModificationState().markDirty());
  -		            }
  -                }
  -            }
  +            // 1. upgrade implicit locks. 
  +            upgradeImplicitLocksAndCheckIfCommitIsNeeded();
   
               // 2. Reorder objects
               reorder();
   
               // 3. commit objects.
  -            iter = mvOrderOfIds.iterator();
  -            while (iter.hasNext())
  -            {
  -                ObjectEnvelope mod = (ObjectEnvelope) mhtObjectEnvelopes.get(iter.next());
  -                mod.getModificationState().commit(mod, broker);
  -
  -                // in a distributed environment all broker caches must invalidate the Object
  -                broker.invalidate(new Identity(mod.getObject(), transaction.getBroker()));
  -            }
  +            commitAllEnvelopes(broker);
   
               // 4. execute batch
               connMan.executeBatch();
  @@ -227,6 +205,66 @@
       }
   
       /**
  +     * commit all envelopes against the current broker
  +     * @param broker the PB to persist all objects
  +     */
  +    private void commitAllEnvelopes(PersistenceBroker broker)
  +    {
  +        if (needsCommit)
  +        {
  +            Iterator iter;
  +            iter = mvOrderOfIds.iterator();
  +            while (iter.hasNext())
  +            {
  +                ObjectEnvelope mod = (ObjectEnvelope) mhtObjectEnvelopes.get(iter.next());
  +                // in a distributed environment all broker caches must invalidate modified
Objects
  +                if (mod.needsDelete() || mod.needsInsert() || mod.needsUpdate())
  +                {
  +                    broker.invalidate(new Identity(mod.getObject(), transaction.getBroker()));
  +                }
  +                mod.getModificationState().commit(mod, broker);
  +            }
  +        }
  +    }
  +
  +    /**
  +     * Implicitely upgrade locks on modified objects.
  +     * Also checks if there are any operations to commit.
  +     */
  +    private void upgradeImplicitLocksAndCheckIfCommitIsNeeded()
  +    {
  +        boolean useImplicitLocking = getConfiguration().useImplicitLocking();
  +        // using clone to avoid ConcurentModificationException
  +        Iterator iter = ((ArrayList) mvOrderOfIds.clone()).iterator();
  +        while (iter.hasNext())
  +        {
  +            ObjectEnvelope mod = (ObjectEnvelope) mhtObjectEnvelopes.get(iter.next());
  +            if (log.isDebugEnabled())
  +                log.debug("commit: " + mod);
  +            // if the Object has been modified by transaction, mark object as dirty
  +            // but only if it has not been marked during tx already !! 
  +            if ((!mod.needsDelete()) && (!mod.needsInsert()) && (!mod.needsUpdate()))
  +            {
  +                if (useImplicitLocking && mod.hasChanged())
  +                {
  +                    // implicitely acquire a write lock !
  +                    transaction.lock(mod.getObject(), Transaction.UPGRADE);
  +                    // mark object dirty
  +                    mod.setModificationState(mod.getModificationState().markDirty());
  +                    // objects needs commit action, thus set needCommit to true:
  +                    needsCommit = true;
  +                }
  +            }
  +            else
  +            {
  +                // objects needs commit action, thus set needCommit to true:
  +                needsCommit = true;
  +            }
  +        }
  +
  +    }
  +
  +    /**
        * perform rollback on all tx-states
        */
       public void rollback()
  @@ -352,24 +390,27 @@
        */
       private void reorder() throws IllegalAccessException
       {
  -        ArrayList vNewVector = new ArrayList(mvOrderOfIds.size());
  -        Map htNewHashtable = new HashMap((int) (mvOrderOfIds.size() * 1.1), 1f);
  -        Map htOldVectorPosition = new HashMap((int) (mvOrderOfIds.size() * 1.1), 1f);
  -        for (int i = 0; i < mvOrderOfIds.size(); i++)
  -            htOldVectorPosition.put(mvOrderOfIds.get(i), new Integer(i));
  -        for (int i = 0; i < mvOrderOfIds.size(); i++)
  -        {
  -            Identity id = (Identity) mvOrderOfIds.get(i);
  -            if (id != null)
  -            {
  -                mvOrderOfIds.set(i, null);
  -                ObjectEnvelope o = (ObjectEnvelope) mhtObjectEnvelopes.get(id);
  -                mhtObjectEnvelopes.remove(id);
  -                reorderObject(htNewHashtable, vNewVector, o, id, htOldVectorPosition);
  +        if (needsCommit)
  +        {
  +            ArrayList vNewVector = new ArrayList(mvOrderOfIds.size());
  +            Map htNewHashtable = new HashMap((int) (mvOrderOfIds.size() * 1.1), 1f);
  +            Map htOldVectorPosition = new HashMap((int) (mvOrderOfIds.size() * 1.1), 1f);
  +            for (int i = 0; i < mvOrderOfIds.size(); i++)
  +                htOldVectorPosition.put(mvOrderOfIds.get(i), new Integer(i));
  +            for (int i = 0; i < mvOrderOfIds.size(); i++)
  +            {
  +                Identity id = (Identity) mvOrderOfIds.get(i);
  +                if (id != null)
  +                {
  +                    mvOrderOfIds.set(i, null);
  +                    ObjectEnvelope o = (ObjectEnvelope) mhtObjectEnvelopes.get(id);
  +                    mhtObjectEnvelopes.remove(id);
  +                    reorderObject(htNewHashtable, vNewVector, o, id, htOldVectorPosition);
  +                }
               }
  +            mvOrderOfIds = vNewVector;
  +            mhtObjectEnvelopes = htNewHashtable;
           }
  -        mvOrderOfIds = vNewVector;
  -        mhtObjectEnvelopes = htNewHashtable;
       }
   
       /**
  @@ -387,24 +428,44 @@
           throws IllegalAccessException
       {
           PersistenceBroker broker = transaction.getBroker();
  -		if (objectToReorder != null)
  -		{
  -			ClassDescriptor cld = broker.getClassDescriptor(objectToReorder.getObject().getClass());
  -			if (objectToReorder.needsDelete())
  -			{
  -				reorderCollection(htNewHashtable, newVector, objectToReorder, cld, htOldVectorPosition);
  -				newVector.add(id);
  -				htNewHashtable.put(id, objectToReorder);
  -				reorderReference(htNewHashtable, newVector, objectToReorder, cld, htOldVectorPosition);
  -			}
  -			else
  -			{
  -				reorderReference(htNewHashtable, newVector, objectToReorder, cld, htOldVectorPosition);
  -				newVector.add(id);
  -				htNewHashtable.put(id, objectToReorder);
  -				reorderCollection(htNewHashtable, newVector, objectToReorder, cld, htOldVectorPosition);
  -			}
  -		}
  +        if (objectToReorder != null)
  +        {
  +            ClassDescriptor cld = broker.getClassDescriptor(objectToReorder.getObject().getClass());
  +            if (objectToReorder.needsDelete())
  +            {
  +                reorderCollection(
  +                    htNewHashtable,
  +                    newVector,
  +                    objectToReorder,
  +                    cld,
  +                    htOldVectorPosition);
  +                newVector.add(id);
  +                htNewHashtable.put(id, objectToReorder);
  +                reorderReference(
  +                    htNewHashtable,
  +                    newVector,
  +                    objectToReorder,
  +                    cld,
  +                    htOldVectorPosition);
  +            }
  +            else
  +            {
  +                reorderReference(
  +                    htNewHashtable,
  +                    newVector,
  +                    objectToReorder,
  +                    cld,
  +                    htOldVectorPosition);
  +                newVector.add(id);
  +                htNewHashtable.put(id, objectToReorder);
  +                reorderCollection(
  +                    htNewHashtable,
  +                    newVector,
  +                    objectToReorder,
  +                    cld,
  +                    htOldVectorPosition);
  +            }
  +        }
       }
   
       /**
  @@ -503,4 +564,17 @@
               }
           }
       }
  +
  +    /**
  +     * get Configuration
  +     * @return OdmgConfiguration 
  +     */
  +    private OdmgConfiguration getConfiguration()
  +    {
  +        OdmgConfiguration config =
  +            (OdmgConfiguration) PersistenceBrokerFactory.getConfigurator().getConfigurationFor(
  +                null);
  +        return config;
  +    }
  +
   }
  
  
  

Mime
View raw message