db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From olegn...@apache.org
Subject cvs commit: db-ojb/src/java/org/apache/ojb/otm/kit SimpleKit.java
Date Wed, 30 Jul 2003 21:46:18 GMT
olegnitz    2003/07/30 14:46:18

  Modified:    src/java/org/apache/ojb/broker/accesslayer
                        IndirectionHandler.java
               src/java/org/apache/ojb/otm/copy
                        MetadataObjectCopyStrategy.java
               src/java/org/apache/ojb/otm/core BaseConnection.java
                        ConcreteEditingContext.java
               src/java/org/apache/ojb/otm/kit SimpleKit.java
  Log:
  Fixed proxies handling, activated fast MetadataObjectCopyStrategy by default
  
  Revision  Changes    Path
  1.18      +11 -0     db-ojb/src/java/org/apache/ojb/broker/accesslayer/IndirectionHandler.java
  
  Index: IndirectionHandler.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/accesslayer/IndirectionHandler.java,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- IndirectionHandler.java	27 Jul 2003 16:18:15 -0000	1.17
  +++ IndirectionHandler.java	30 Jul 2003 21:46:17 -0000	1.18
  @@ -291,6 +291,17 @@
       }
   
       /**
  +     * [olegnitz] This looks stupid, but is really necessary for OTM:
  +     * the materialization listener replaces the real subject
  +     * by its clone to ensure transaction isolation.
  +     * Is there a better way to do this?
  +     */
  +    public void setRealSubject(Object object)
  +    {
  +        realSubject = object;
  +    }
  +
  +    /**
        * retrieve the real subject from the underlying RDBMS
        */
       private synchronized void materializeSubject() throws PersistenceBrokerException
  
  
  
  1.10      +11 -16    db-ojb/src/java/org/apache/ojb/otm/copy/MetadataObjectCopyStrategy.java
  
  Index: MetadataObjectCopyStrategy.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/otm/copy/MetadataObjectCopyStrategy.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- MetadataObjectCopyStrategy.java	29 Jul 2003 16:51:57 -0000	1.9
  +++ MetadataObjectCopyStrategy.java	30 Jul 2003 21:46:18 -0000	1.10
  @@ -54,6 +54,7 @@
    * <http://www.apache.org/>.
    */
   
  +import org.apache.ojb.broker.VirtualProxy;
   import org.apache.ojb.broker.metadata.*;
   import org.apache.ojb.broker.metadata.fieldaccess.PersistentField;
   import org.apache.ojb.broker.accesslayer.CollectionProxy;
  @@ -112,7 +113,11 @@
           if (toCopy == null)
               return null;
   
  -        if (toCopy instanceof Proxy)
  +        /**
  +         * if this is a proxy, just copy the proxy, don't materialize it, and stop recursing
  +         */
  +        if (Proxy.isProxyClass(toCopy.getClass())
  +                    || (toCopy instanceof VirtualProxy))
           {
               return _serialize.copy(toCopy, broker);
           }
  @@ -152,7 +157,7 @@
           {
               throw new ObjectCopyException("InstantiationException", e);
           }
  -        
  +
           /**
            * first copy all the fields
            * fields are not mapped objects (ie ObjectReferenceDescriptors)
  @@ -184,19 +189,9 @@
                * register in the objMap first
                */
               final Object object = f.get(toCopy);
  -            /**
  -             * if this is a proxy, just copy the proxy, don't materialize it, and stop
recursing
  -             */
  -            if (object instanceof Proxy)
  -            {
  -                f.set(retval, object);
  -            }
  -            else
  -            {
  -                final Object clone = clone(object, objMap, broker);
  -                objMap.put(object, clone);
  -                f.set(retval, clone);
  -            }
  +            final Object clone = clone(object, objMap, broker);
  +            objMap.put(object, clone);
  +            f.set(retval, clone);
           }
           /**
            * then copy all the 1:M and M:N references
  @@ -218,7 +213,7 @@
               }
               else if (collection instanceof CollectionProxy)
               {
  -                f.set(retval, collection);
  +                f.set(retval, _serialize.copy(collection, broker));
               }
               else if (collection instanceof Collection)
               {
  
  
  
  1.21      +6 -3      db-ojb/src/java/org/apache/ojb/otm/core/BaseConnection.java
  
  Index: BaseConnection.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/otm/core/BaseConnection.java,v
  retrieving revision 1.20
  retrieving revision 1.21
  diff -u -r1.20 -r1.21
  --- BaseConnection.java	25 Jul 2003 00:22:00 -0000	1.20
  +++ BaseConnection.java	30 Jul 2003 21:46:18 -0000	1.21
  @@ -140,7 +140,6 @@
           return _tx;
       }
   
  -
       private static boolean isMaterialized(Object object)
       {
           IndirectionHandler handler;
  @@ -180,7 +179,8 @@
               object = _pb.getObjectByIdentity(oid);
               if (object != null)
               {
  -                if (lock != LockType.WRITE_LOCK)
  +                if ((lock != LockType.WRITE_LOCK)
  +                        || !_tx.getKit().isImplicitLockingUsed());
                   {
                       copyStrategy = _tx.getKit().getCopyStrategy(oid);
                       object = copyStrategy.copy(object, _pb);
  @@ -378,11 +378,14 @@
       {
           private final Iterator _it;
           private final int _lock;
  +        private final boolean _needToCopy;
   
           OTMIterator(Iterator it, int lock)
           {
               _it = it;
               _lock = lock;
  +            _needToCopy = (_lock != LockType.WRITE_LOCK)
  +                    || !_tx.getKit().isImplicitLockingUsed();
           }
   
           public boolean hasNext()
  @@ -413,7 +416,7 @@
               {
                   object = ctxObject;
               }
  -            else if (_lock != LockType.WRITE_LOCK && isMaterialized(object))
  +            else if (_needToCopy)
               {
                   copyStrategy = _tx.getKit().getCopyStrategy(oid);
                   object = copyStrategy.copy(object, _pb);
  
  
  
  1.26      +62 -23    db-ojb/src/java/org/apache/ojb/otm/core/ConcreteEditingContext.java
  
  Index: ConcreteEditingContext.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/otm/core/ConcreteEditingContext.java,v
  retrieving revision 1.25
  retrieving revision 1.26
  diff -u -r1.25 -r1.26
  --- ConcreteEditingContext.java	27 Jul 2003 20:12:18 -0000	1.25
  +++ ConcreteEditingContext.java	30 Jul 2003 21:46:18 -0000	1.26
  @@ -93,6 +93,7 @@
   import org.apache.ojb.broker.util.ArrayIterator;
   import org.apache.ojb.otm.EditingContext;
   import org.apache.ojb.otm.OTMKit;
  +import org.apache.ojb.otm.copy.ObjectCopyStrategy;
   import org.apache.ojb.otm.lock.LockManager;
   import org.apache.ojb.otm.lock.LockType;
   import org.apache.ojb.otm.lock.LockingException;
  @@ -290,7 +291,7 @@
           // if the inserted object is materialized
           if ((handler == null) && (newObj != null))
           {
  -            lockReachableObjects(newObj);
  +            lockReachableObjects(newObj, lock);
           }
   
           return entry;
  @@ -346,10 +347,15 @@
               return;
           }
   
  -        // IndirectionHandler doesn't put the materialized objects
  -        // to the cache, I don't know for what reason.
  -        // I think it make sense for efficiency
  -        _pb.serviceObjectCache().cache(oid, object);
  +        int lock = LockManager.getInstance().getLockHeld(oid, _tx);
  +        boolean needToCopy = (lock != LockType.WRITE_LOCK)
  +                || !_tx.getKit().isImplicitLockingUsed();
  +        if (needToCopy)
  +        {
  +            ObjectCopyStrategy copyStrategy = _tx.getKit().getCopyStrategy(oid);
  +            object = copyStrategy.copy(object, _pb);
  +            handler.setRealSubject(object);
  +        }
   
           _original.put(oid, getFields(object, false, true));
   
  @@ -358,11 +364,11 @@
           entry.handler.removeListener(this);
           entry.handler = null;
   
  -        // perform automatic read lock for all reachable objects
  +        // perform automatic lock for all reachable objects
           // if the inserted object is materialized
           try
           {
  -            lockReachableObjects(object);
  +            lockReachableObjects(object, lock);
           }
           catch (LockingException ex)
           {
  @@ -665,7 +671,7 @@
               return null;
           }
       }
  -    
  +
       private void removeMaterializationListener()
       {
           for (Iterator it = _order.iterator(); it.hasNext();)
  @@ -694,9 +700,10 @@
       }
   
       /**
  -     * Read lock all objects reachable via 1:N and N:1 relations
  +     * Lock all objects reachable via 1:N and N:1 relations,
  +     * @param lock The lock type to use
        */
  -    private void lockReachableObjects(Object object)
  +    private void lockReachableObjects(Object object, int lock)
           throws LockingException
       {
           boolean onlyDependants = !_tx.getKit().isImplicitLockingUsed();
  @@ -707,11 +714,13 @@
           ObjectReferenceDescriptor rds = null;
           Object relObj = null;
           Identity relOid;
  +        boolean isDependent;
   
           while (iter.hasNext())
           {
               rds = (ObjectReferenceDescriptor) iter.next();
  -            if (onlyDependants && !rds.getOtmDependent())
  +            isDependent = rds.getOtmDependent();
  +            if (onlyDependants && !isDependent)
               {
                   continue;
               }
  @@ -721,8 +730,7 @@
                   relOid = new Identity(relObj, _pb);
                   if (_objects.get(relOid) == null)
                   {
  -                    insertInternal(relOid, relObj, LockType.READ_LOCK, false,
  -                                   rds.getOtmDependent());
  +                    insertInternal(relOid, relObj, lock, false, isDependent);
                   }
               }
           }
  @@ -740,7 +748,8 @@
               cds = (CollectionDescriptor) collections.next();
               f = cds.getPersistentField();
               type = f.getType();
  -            if (onlyDependants && !cds.getOtmDependent())
  +            isDependent = cds.getOtmDependent();
  +            if (onlyDependants && !isDependent)
               {
                   continue;
               }
  @@ -774,8 +783,7 @@
                       relOid = new Identity(relObj, _pb);
                       if (_objects.get(relOid) == null)
                       {
  -                        insertInternal(relOid, relObj, LockType.READ_LOCK, false,
  -                                       cds.getOtmDependent());
  +                        insertInternal(relOid, relObj, lock, false, isDependent);
                       }
                   }
               }
  @@ -856,6 +864,7 @@
           ArrayList[] collections = new ArrayList[colDescs.size()];
           Object[] references = null;
           ArrayList[] collectionsOfObjects = null;
  +        int lockForListeners = LockType.NO_LOCK;
   
           if (withObjects)
           {
  @@ -863,6 +872,12 @@
               collectionsOfObjects = new ArrayList[colDescs.size()];
           }
   
  +        if (addListeners)
  +        {
  +            lockForListeners = LockManager.getInstance().getLockHeld(
  +                    new Identity(obj, _pb), _tx);
  +        }
  +
           fields[0] = obj.getClass(); // we must notice if the object class changes
           count++;
   
  @@ -904,7 +919,8 @@
                   if (addListeners)
                   {
                       OTMCollectionProxyListener listener =
  -                            new OTMCollectionProxyListener(cds, collections, count);
  +                            new OTMCollectionProxyListener(cds, collections,
  +                                                           count, lockForListeners);
   
                       ((CollectionProxy) col).addListener(listener);
                       if (_colProxyListeners == null)
  @@ -1405,18 +1421,24 @@
           private final CollectionDescriptor _cds;
           private final ArrayList[] _collections;
           private final int _index;
  +        private final int _lock;
   
           OTMCollectionProxyListener(CollectionDescriptor cds,
  -                ArrayList[] collections, int index)
  +                ArrayList[] collections, int index, int lock)
           {
               _cds = cds;
               _collections = collections;
               _index = index;
  +            _lock = lock;
           }
   
           public void loaded(CollectionProxy colProxy)
           {
               ArrayList list = new ArrayList();
  +            ArrayList newCol = new ArrayList();
  +            LockManager lockManager = LockManager.getInstance();
  +            boolean needToCopy = (_lock != LockType.WRITE_LOCK)
  +                    || !_tx.getKit().isImplicitLockingUsed();
   
               _collections[_index] = list;
   
  @@ -1424,21 +1446,38 @@
               {
                   Object relObj = it.next();
                   Identity relOid = new Identity(relObj, _pb);
  +                Object ctxObj;
   
                   list.add(relOid);
  -                if (_objects.get(relOid) == null)
  +                ctxObj = _objects.get(relOid);
  +                if (ctxObj != null)
                   {
  -                    try 
  +                    relObj = ctxObj;
  +                }
  +                else
  +                {
  +                    ObjectCopyStrategy copyStrategy;
  +
  +                    if (needToCopy)
                       {
  -                        insertInternal(relOid, relObj, LockType.READ_LOCK, false,
  -                                    _cds.getOtmDependent());
  -                    } 
  +                        copyStrategy = _tx.getKit().getCopyStrategy(relOid);
  +                        relObj = copyStrategy.copy(relObj, _pb);
  +                    }
  +
  +                    try
  +                    {
  +                        insertInternal(relOid, relObj, _lock, false,
  +                                       _cds.getOtmDependent());
  +                    }
                       catch (LockingException ex)
                       {
                           throw new LockingPassthruException(ex);
                       }
                   }
  +                newCol.add(ctxObj);
               }
  +            colProxy.clear();
  +            colProxy.addAll(newCol);
           }
       }
   }
  
  
  
  1.15      +2 -2      db-ojb/src/java/org/apache/ojb/otm/kit/SimpleKit.java
  
  Index: SimpleKit.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/otm/kit/SimpleKit.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- SimpleKit.java	25 Jul 2003 00:22:00 -0000	1.14
  +++ SimpleKit.java	30 Jul 2003 21:46:18 -0000	1.15
  @@ -100,8 +100,8 @@
           _lockMap = new InMemoryLockMap();
           _noOpCopyStrategy = new NoOpObjectCopyStrategy();
           //_defaultCopyStrategy = new ReflectiveObjectCopyStrategy();
  -        _defaultCopyStrategy = new SerializeObjectCopyStrategy();
  -        //_defaultCopyStrategy = new MetadataObjectCopyStrategy();
  +        //_defaultCopyStrategy = new SerializeObjectCopyStrategy();
  +        _defaultCopyStrategy = new MetadataObjectCopyStrategy();
           _cloneableCopyStrategy = new CloneableObjectCopyStrategy();
       }
   
  
  
  

---------------------------------------------------------------------
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