You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by th...@apache.org on 2002/12/23 22:27:59 UTC

cvs commit: jakarta-ojb/src/java/org/apache/ojb/odmg ObjectEnvelopeTable.java

thma        2002/12/23 13:27:59

  Modified:    src/java/org/apache/ojb/odmg ObjectEnvelopeTable.java
  Log:
  apply Luis fix to a ConcurrentModificationException. (using a temporary copy of the envelope list)
  
  Revision  Changes    Path
  1.10      +77 -55    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.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- ObjectEnvelopeTable.java	9 Dec 2002 22:00:09 -0000	1.9
  +++ ObjectEnvelopeTable.java	23 Dec 2002 21:27:59 -0000	1.10
  @@ -54,6 +54,15 @@
    * <http://www.apache.org/>.
    */
   
  +import java.util.ArrayList;
  +import java.util.Collection;
  +import java.util.Enumeration;
  +import java.util.HashMap;
  +import java.util.Iterator;
  +import java.util.List;
  +import java.util.Map;
  +import java.util.Vector;
  +
   import org.apache.ojb.broker.Identity;
   import org.apache.ojb.broker.ManageableCollection;
   import org.apache.ojb.broker.PersistenceBroker;
  @@ -67,14 +76,6 @@
   import org.odmg.Transaction;
   import org.odmg.TransactionAbortedException;
   
  -import java.util.ArrayList;
  -import java.util.Collection;
  -import java.util.Enumeration;
  -import java.util.HashMap;
  -import java.util.Iterator;
  -import java.util.List;
  -import java.util.Map;
  -
   /**
    * manages all ObjectEnvelopes included by a transaction.
    * Performs commit, and rollack operations on all included Envelopes.
  @@ -103,13 +104,13 @@
        */
       private List mvOrderOfIds = new ArrayList();
   
  -	public void close()
  -	{
  -		/**
  -		 * MBAIRD: be nice and remove all references so they can be
  -		 * gc'd
  -		 */
  -		transaction = null;
  +    public void close()
  +    {
  +        /**
  +         * MBAIRD: be nice and remove all references so they can be
  +         * gc'd
  +         */
  +        transaction = null;
           if (mhtObjectEnvelopes != null)
           {
               Iterator iter = mvOrderOfIds.iterator();
  @@ -117,11 +118,11 @@
               while (iter.hasNext())
               {
                   temp = (ObjectEnvelope) mhtObjectEnvelopes.get(iter.next());
  -        		temp.close();
  -        		temp = null;
  -        	}
  +                temp.close();
  +                temp = null;
  +            }
           }
  -	}
  +    }
   
       /**
        * Creates new ObjectEnvelopeTable
  @@ -142,15 +143,16 @@
   
           try
           {
  -            if(log.isDebugEnabled())
  +            if (log.isDebugEnabled())
               {
  -                log.debug("PB is in intern tx: "+broker.isInTransaction()+
  -                    "  broker was: "+broker);
  +                log.debug(
  +                    "PB is in intern tx: " + broker.isInTransaction() + "  broker was: " + broker);
               }
               // all neccessary db operations are executed within a PersistenceBroker transaction:
  -            if(!broker.isInTransaction())
  +            if (!broker.isInTransaction())
               {
  -                if(log.isDebugEnabled()) log.debug("call beginTransaction() on PB instance");
  +                if (log.isDebugEnabled())
  +                    log.debug("call beginTransaction() on PB instance");
                   broker.beginTransaction();
               }
   
  @@ -162,12 +164,24 @@
               // 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
  +            Vector temp = new Vector();
               Iterator iter = mvOrderOfIds.iterator();
               while (iter.hasNext())
               {
  -                ObjectEnvelope mod = (ObjectEnvelope) mhtObjectEnvelopes.get(iter.next());
  -                if (log.isDebugEnabled()) log.debug("commit: " + mod);
  +                temp.add(iter.next());
  +            }
  +
  +            // 1. upgrade locks
  +            for (int i = 0; i < temp.size(); i++)
  +            {
  +                ObjectEnvelope mod = (ObjectEnvelope) mhtObjectEnvelopes.get(temp.get(i));
  +
  +                if (log.isDebugEnabled())
  +                    log.debug("commit: " + mod);
                   // if the Object has been modified by transaction, mark object as dirty
                   if (mod.hasChanged())
                   {
  @@ -197,12 +211,14 @@
           }
           catch (Throwable t)
           {
  -// we do that in TransactionImpl#abort()
  -//            broker.abortTransaction();
  +            // we do that in TransactionImpl#abort()
  +            //            broker.abortTransaction();
               connMan.clearBatch();
               log.error("Commit on object level failed for tx " + transaction, t);
               throw new TransactionAbortedException(t.getMessage());
  -        } finally {
  +        }
  +        finally
  +        {
               connMan.setBatchMode(saveBatchMode);
           }
       }
  @@ -217,7 +233,8 @@
           while (iter.hasNext())
           {
               ObjectEnvelope mod = (ObjectEnvelope) mhtObjectEnvelopes.get(iter.next());
  -            if (log.isDebugEnabled()) log.debug("rollback: " + mod);
  +            if (log.isDebugEnabled())
  +                log.debug("rollback: " + mod);
               // if the Object has been modified has been modified by transaction, mark object as dirty
               if (mod.hasChanged())
               {
  @@ -281,7 +298,8 @@
               result = new ObjectEnvelope(pKey, transaction);
               mhtObjectEnvelopes.put(id, result);
               mvOrderOfIds.add(id);
  -            if (log.isDebugEnabled()) log.debug("register: " + result);
  +            if (log.isDebugEnabled())
  +                log.debug("register: " + result);
           }
           return result;
       }
  @@ -293,8 +311,10 @@
       {
           Identity id = new Identity(pKey, transaction.getBroker());
           //Integer keyInt = new Integer(System.identityHashCode(key));
  -        if (log.isDebugEnabled()) log.debug("register: " + modification);
  -        if (!mhtObjectEnvelopes.containsKey(id)) mvOrderOfIds.add(id);
  +        if (log.isDebugEnabled())
  +            log.debug("register: " + modification);
  +        if (!mhtObjectEnvelopes.containsKey(id))
  +            mvOrderOfIds.add(id);
           mhtObjectEnvelopes.put(id, modification);
       }
   
  @@ -334,8 +354,7 @@
       /**
        * Reorder the objects in the table to resolve referential integrity dependencies.
        */
  -    private void reorder()
  -            throws IllegalAccessException
  +    private void reorder() throws IllegalAccessException
       {
           List vNewVector = new ArrayList(mvOrderOfIds.size());
           Map htNewHashtable = new HashMap((int) (mvOrderOfIds.size() * 1.1), 1f);
  @@ -363,12 +382,13 @@
        * are put in the vector, then the object in question and then the references.
        * Otherwise the order is reversed.
        */
  -    private void reorderObject(Map htNewHashtable,
  -                               List newVector,
  -                               ObjectEnvelope objectToReorder,
  -                               Identity id,
  -                               Map htOldVectorPosition)
  -            throws IllegalAccessException
  +    private void reorderObject(
  +        Map htNewHashtable,
  +        List newVector,
  +        ObjectEnvelope objectToReorder,
  +        Identity id,
  +        Map htOldVectorPosition)
  +        throws IllegalAccessException
       {
           PersistenceBroker broker = transaction.getBroker();
           ClassDescriptor cld = broker.getClassDescriptor(objectToReorder.getObject().getClass());
  @@ -395,12 +415,13 @@
        * object contains again collections or references) and it is finally put into the new order
        * vector.
        */
  -    private void reorderCollection(Map htNewHashtable,
  -                                   List newVector,
  -                                   ObjectEnvelope objectToReorder,
  -                                   ClassDescriptor cld,
  -                                   Map htOldVectorPosition)
  -            throws IllegalAccessException
  +    private void reorderCollection(
  +        Map htNewHashtable,
  +        List newVector,
  +        ObjectEnvelope objectToReorder,
  +        ClassDescriptor cld,
  +        Map htOldVectorPosition)
  +        throws IllegalAccessException
       {
           Iterator i;
           i = cld.getCollectionDescriptors().iterator();
  @@ -426,7 +447,7 @@
                   else
                   {
                       throw new RuntimeException(
  -                            col.getClass()
  +                        col.getClass()
                               + " can not be managed by OJB, use Array, Collection or ManageableCollection instead !");
                   }
                   while (colIterator.hasNext())
  @@ -456,12 +477,13 @@
        * object contains again collections or references) and it is finally put into the new order
        * vector.
        */
  -    private void reorderReference(Map htNewHashtable,
  -                                  List newVector,
  -                                  ObjectEnvelope objectToReorder,
  -                                  ClassDescriptor cld,
  -                                  Map htOldVectorPosition)
  -            throws IllegalAccessException
  +    private void reorderReference(
  +        Map htNewHashtable,
  +        List newVector,
  +        ObjectEnvelope objectToReorder,
  +        ClassDescriptor cld,
  +        Map htOldVectorPosition)
  +        throws IllegalAccessException
       {
           Iterator i = cld.getObjectReferenceDescriptors().iterator();
           while (i.hasNext())