db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arm...@apache.org
Subject cvs commit: db-ojb/src/java/org/apache/ojb/odmg ObjectEnvelopeTable.java TransactionImpl.java
Date Thu, 24 Jun 2004 15:36:57 GMT
arminw      2004/06/24 08:36:57

  Modified:    src/java/org/apache/ojb/odmg ObjectEnvelopeTable.java
                        TransactionImpl.java
  Log:
  - avoid duplicate code, use helper method to obtain
  Iterator for collection object
  
  - fix "semicolon" bug? 
  if (!proxy.isLoaded()) ;  <==
  {
   ....
  }
  
  Revision  Changes    Path
  1.32      +5 -22     db-ojb/src/java/org/apache/ojb/odmg/ObjectEnvelopeTable.java
  
  Index: ObjectEnvelopeTable.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/odmg/ObjectEnvelopeTable.java,v
  retrieving revision 1.31
  retrieving revision 1.32
  diff -u -r1.31 -r1.32
  --- ObjectEnvelopeTable.java	10 Jun 2004 14:40:08 -0000	1.31
  +++ ObjectEnvelopeTable.java	24 Jun 2004 15:36:56 -0000	1.32
  @@ -16,28 +16,25 @@
    */
   
   import java.util.ArrayList;
  -import java.util.Collection;
  +import java.util.Collections;
   import java.util.Enumeration;
   import java.util.HashMap;
   import java.util.Iterator;
   import java.util.List;
   import java.util.Map;
  -import java.util.Collections;
   
   import org.apache.commons.lang.builder.ToStringBuilder;
   import org.apache.commons.lang.builder.ToStringStyle;
   import org.apache.ojb.broker.Identity;
  -import org.apache.ojb.broker.ManageableCollection;
  -import org.apache.ojb.broker.OJBRuntimeException;
   import org.apache.ojb.broker.OptimisticLockException;
   import org.apache.ojb.broker.PersistenceBroker;
   import org.apache.ojb.broker.PersistenceBrokerFactory;
  -import org.apache.ojb.broker.core.proxy.ProxyHelper;
   import org.apache.ojb.broker.accesslayer.ConnectionManagerIF;
  +import org.apache.ojb.broker.core.proxy.ProxyHelper;
   import org.apache.ojb.broker.metadata.ClassDescriptor;
   import org.apache.ojb.broker.metadata.CollectionDescriptor;
   import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
  -import org.apache.ojb.broker.util.ArrayIterator;
  +import org.apache.ojb.broker.util.BrokerHelper;
   import org.apache.ojb.broker.util.logging.Logger;
   import org.apache.ojb.broker.util.logging.LoggerFactory;
   import org.apache.ojb.odmg.locking.LockManagerFactory;
  @@ -525,25 +522,11 @@
                   Iterator colIterator;
                   if (ProxyHelper.isCollectionProxy(col) && !ProxyHelper.getCollectionProxy(col).isLoaded())
                   {
  -                    colIterator = Collections.EMPTY_LIST.iterator(); 
  -                }
  -                else if (col instanceof ManageableCollection)
  -                {
  -                    colIterator = ((ManageableCollection) col).ojbIterator();
  -                }
  -                else if (col instanceof Collection)
  -                {
  -                    colIterator = ((Collection) col).iterator();
  -                }
  -                else if (col.getClass().isArray())
  -                {
  -                    colIterator = new ArrayIterator(col);
  +                    colIterator = Collections.EMPTY_LIST.iterator();
                   }
                   else
                   {
  -                    throw new OJBRuntimeException(
  -                        col.getClass()
  -                            + " can not be managed by OJB, use Array, Collection or ManageableCollection
instead !");
  +                    colIterator = BrokerHelper.getCollectionIterator(col);
                   }
                   while (colIterator.hasNext())
                   {
  
  
  
  1.59      +76 -90    db-ojb/src/java/org/apache/ojb/odmg/TransactionImpl.java
  
  Index: TransactionImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/odmg/TransactionImpl.java,v
  retrieving revision 1.58
  retrieving revision 1.59
  diff -u -r1.58 -r1.59
  --- TransactionImpl.java	10 Jun 2004 14:40:08 -0000	1.58
  +++ TransactionImpl.java	24 Jun 2004 15:36:56 -0000	1.59
  @@ -26,25 +26,23 @@
   
   import org.apache.commons.lang.SystemUtils;
   import org.apache.ojb.broker.Identity;
  -import org.apache.ojb.broker.ManageableCollection;
  -import org.apache.ojb.broker.OJBRuntimeException;
   import org.apache.ojb.broker.PBFactoryException;
   import org.apache.ojb.broker.PersistenceBroker;
   import org.apache.ojb.broker.PersistenceBrokerException;
   import org.apache.ojb.broker.PersistenceBrokerFactory;
   import org.apache.ojb.broker.PersistenceBrokerSQLException;
   import org.apache.ojb.broker.core.ValueContainer;
  +import org.apache.ojb.broker.core.proxy.CollectionProxy;
  +import org.apache.ojb.broker.core.proxy.CollectionProxyDefaultImpl;
  +import org.apache.ojb.broker.core.proxy.CollectionProxyListener;
   import org.apache.ojb.broker.core.proxy.IndirectionHandler;
   import org.apache.ojb.broker.core.proxy.MaterializationListener;
   import org.apache.ojb.broker.core.proxy.ProxyHelper;
  -import org.apache.ojb.broker.core.proxy.CollectionProxy;
  -import org.apache.ojb.broker.core.proxy.CollectionProxyListener;
  -import org.apache.ojb.broker.core.proxy.CollectionProxyDefaultImpl;
   import org.apache.ojb.broker.metadata.ClassDescriptor;
   import org.apache.ojb.broker.metadata.CollectionDescriptor;
   import org.apache.ojb.broker.metadata.FieldDescriptor;
   import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
  -import org.apache.ojb.broker.util.ArrayIterator;
  +import org.apache.ojb.broker.util.BrokerHelper;
   import org.apache.ojb.broker.util.GUID;
   import org.apache.ojb.broker.util.configuration.Configurable;
   import org.apache.ojb.broker.util.configuration.Configuration;
  @@ -120,6 +118,12 @@
       private ArrayList unmaterializedLocks = new ArrayList();
   
       /**
  +     * If a checkpoint call is made we keep the locks till commit or
  +     * abort is called.
  +     */
  +    private ArrayList locksToRelease = new ArrayList();
  +
  +    /**
        * Creates new Transaction
        * @param theCurrentDB - create a transaction that is associated with the database.
        */
  @@ -210,8 +214,7 @@
        */
       public void lock(Object obj, int lockMode) throws LockNotGrantedException
       {
  -        if (log.isDebugEnabled()) log.debug("lock object was called on tx " + this);
  -        if (log.isDebugEnabled()) log.debug("object is " + obj.toString());
  +        if (log.isDebugEnabled()) log.debug("lock object was called on tx " + this + ",
object is " + obj.toString());
           checkOpen();
   
           if (!ProxyHelper.isProxy(obj))
  @@ -239,21 +242,21 @@
           {
               if (!lm.readLock(this, obj))
               {
  -                throw new LockNotGrantedException("Can not lock " + obj + " for READ");
  +                throw new LockNotGrantedException("Can not lock for READ: " + obj);
               }
           }
           else if (lockMode == Transaction.WRITE)
           {
               if (!lm.writeLock(this, obj))
               {
  -                throw new LockNotGrantedException("Can not lock " + obj + " for WRITE");
  +                throw new LockNotGrantedException("Can not lock for WRITE: " + obj);
               }
           }
           else if (lockMode == Transaction.UPGRADE)
           {
               if (!lm.upgradeLock(this, obj))
               {
  -                throw new LockNotGrantedException("Can not lock " + obj + " for UPGRADE");
  +                throw new LockNotGrantedException("Can not lock for UPGRADE: " + obj);
               }
           }
           try
  @@ -284,7 +287,7 @@
        * I don't know what we should do if we are in a checkpoint and
        * we need to abort.
        */
  -    private synchronized void doCommitOnObjects() throws TransactionAbortedException, LockNotGrantedException
  +    private synchronized void doCommitOnObjects(boolean keepLogs) throws TransactionAbortedException,
LockNotGrantedException
       {
           ObjectEnvelope item;
           /*
  @@ -310,6 +313,11 @@
           while (enum.hasMoreElements())
           {
               item = (ObjectEnvelope) enum.nextElement();
  +            // keep locked objects in a list, release locks on end of commit (see checkpoint)
  +            if(keepLogs)
  +            {
  +                locksToRelease.add(item.getObject());
  +            }
               item.afterCommit();
           }
           registeredForLock.clear();
  @@ -367,6 +375,13 @@
                   removeLock(it.next(), Transaction.WRITE);
               }
   
  +            //remove locks for objects which were committed within a checkpoint
  +            for (Iterator it = locksToRelease.iterator(); it.hasNext();)
  +            {
  +                Object obj = it.next();
  +                removeLock(obj, Transaction.WRITE);
  +            }
  +
               // this tx is no longer interested in materialization callbacks
               unRegisterFromAllIndirectionHandlers();
               unRegisterFromAllCollectionProxies();
  @@ -422,6 +437,7 @@
           broker = null;
           registeredForLock.clear();
           unmaterializedLocks.clear();
  +        locksToRelease.clear();
           m_txStatus = Status.STATUS_NO_TRANSACTION;
       }
   
  @@ -438,13 +454,13 @@
           try
           {
               checkOpen();
  -            doCommitOnObjects();
  +            doCommitOnObjects(true);
               // do commit on PB
               if (broker != null && broker.isInTransaction()) broker.commitTransaction();
           }
           catch (Throwable t)
           {
  -            log.error("checkpoint failed", t);
  +            log.error("checkpoint call failed, do abort transaction", t);
               try
               {
                   doAbort();
  @@ -478,7 +494,7 @@
           try
           {
               checkOpen();
  -            doCommitOnObjects();
  +            doCommitOnObjects(true);
           }
           catch (Throwable t)
           {
  @@ -553,9 +569,9 @@
       /**
        * removeLock removes the transactions lock from the Object.
        */
  -    private void removeLock(Object obj, int lockType)
  +    private boolean removeLock(Object obj, int lockType)
       {
  -        LockManagerFactory.getLockManager().releaseLock(this, obj);
  +        return LockManagerFactory.getLockManager().releaseLock(this, obj);
       }
   
       /**
  @@ -625,7 +641,7 @@
           try
           {
               m_txStatus = Status.STATUS_PREPARING;
  -            doCommitOnObjects();
  +            doCommitOnObjects(false);
               m_txStatus = Status.STATUS_PREPARED;
           }
           catch (LockNotGrantedException e)
  @@ -931,63 +947,42 @@
                   CollectionProxy proxy = ProxyHelper.getCollectionProxy(col);
                   if (proxy != null)
                   {
  -                    if (!proxy.isLoaded()) ;
  +                    if (!proxy.isLoaded())
                       {
                           if (log.isDebugEnabled()) log.debug("adding self as listener to
collection proxy");
                           proxy.addListener(this);
                           continue;
                       }
                   }
  -                Iterator colIterator;
  -                if (col instanceof ManageableCollection)
  -                {
  -                    colIterator = ((ManageableCollection) col).ojbIterator();
  -                }
  -                else if (col instanceof Collection)
  -                {
  -                    colIterator = ((Collection) col).iterator();
  -                }
  -                else if (col.getClass().isArray())
  -                {
  -                    colIterator = new ArrayIterator(col);
  -                }
  -                else
  -                {
  -                    throw new OJBRuntimeException(
  -                            col.getClass()
  -                            + " can not be managed by OJB, use Array, Collection or ManageableCollection
instead !");
  -                }
  -
  +                Iterator colIterator = BrokerHelper.getCollectionIterator(col);
                   // get foreign info for collection elements
                   ClassDescriptor itemCld = this.getBroker().getClassDescriptor(cds.getItemClass());
                   // BRJ: do not convertToSql
                   ValueContainer[] objPkValues = this.getBroker().serviceBrokerHelper().getKeyValues(cld,
newTxObject, false);
                   FieldDescriptor[] itemFkFields = cds.getForeignKeyFieldDescriptors(itemCld);
  -                /**
  -                 * itemFkFields will be null in the case of many to many relationships.
  -                 */
  -                if (itemFkFields != null)
  +
  +                Object item = null;
  +                try
                   {
  -                    Object item = null;
  -                    try
  +                    while (colIterator.hasNext())
                       {
  -                        while (colIterator.hasNext())
  +                        item = colIterator.next();
  +                        IndirectionHandler handler = ProxyHelper.getIndirectionHandler(item);
  +                        if (handler != null)
                           {
  -                            item = colIterator.next();
  -                            IndirectionHandler handler = ProxyHelper.getIndirectionHandler(item);
  -                            if (handler != null)
  +                            if (!handler.alreadyMaterialized())
                               {
  -                                if (!handler.alreadyMaterialized())
  -                                {
  -                                    continue;
  -                                }
  -                                else
  -                                {
  -                                    // @todo consider registering to hear when this is
  -                                    // derefernced instead of just loading here -bmc
  -                                    item = handler.getRealSubject();
  -                                }
  +                                continue;
                               }
  +                            else
  +                            {
  +                                // @todo consider registering to hear when this is
  +                                // derefernced instead of just loading here -bmc
  +                                item = handler.getRealSubject();
  +                            }
  +                        }
  +                        if(!cds.isMtoNRelation())
  +                        {
                               //if itemCld refers to an interface the foreignKeyFieldDescriptors
                               //have to be computed again for each concrete class
                               if (itemCld.isInterface())
  @@ -1002,40 +997,31 @@
                                   FieldDescriptor fld = itemFkFields[j];
                                   fld.getPersistentField().set(item, objPkValues[j].getValue());
                               }
  -                            lock(item, lockMode);
                           }
  -                    }
  -                    catch (PersistenceBrokerException e)
  -                    {
  -                        String eol = SystemUtils.LINE_SEPARATOR;
  -                        log.error("Error while set FK in collection references[" +
  -                                eol + "current reference descriptor:" +
  -                                eol + cds.toXML() +
  -                                eol + "current item object: " + item +
  -                                eol + "main object class: " + newTxObject.getClass().getName()
+
  -                                eol + "]", e);
  -                        throw e;
  -                    }
  -                    catch (LockNotGrantedException e)
  -                    {
  -                        String eol = SystemUtils.LINE_SEPARATOR;
  -                        log.error("Lock not granted, while set FK in collection references["
+
  -                                eol + "current reference descriptor:" +
  -                                eol + cds.toXML() +
  -                                eol + "object to lock: " + item +
  -                                eol + "main object class: " + newTxObject.getClass().getName()
+
  -                                eol + "]", e);
  -                        throw e;
  +                        lock(item, lockMode);
                       }
                   }
  -                else
  +                catch (PersistenceBrokerException e)
                   {
  -                    //objects of m:n-relations have to be locked as well
  -                    while (colIterator.hasNext())
  -                    {
  -                        Object item = colIterator.next();
  -                        lock(item, lockMode);
  -                    }
  +                    String eol = SystemUtils.LINE_SEPARATOR;
  +                    log.error("Error while set FK in collection references[" +
  +                            eol + "current reference descriptor:" +
  +                            eol + cds.toXML() +
  +                            eol + "current item object: " + item +
  +                            eol + "main object class: " + newTxObject.getClass().getName()
+
  +                            eol + "]", e);
  +                    throw e;
  +                }
  +                catch (LockNotGrantedException e)
  +                {
  +                    String eol = SystemUtils.LINE_SEPARATOR;
  +                    log.error("Lock not granted, while set FK in collection references["
+
  +                            eol + "current reference descriptor:" +
  +                            eol + cds.toXML() +
  +                            eol + "object to lock: " + item +
  +                            eol + "main object class: " + newTxObject.getClass().getName()
+
  +                            eol + "]", e);
  +                    throw e;
                   }
               }
           }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org


Mime
View raw message