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 ol...@apache.org on 2004/03/02 02:27:15 UTC

cvs commit: db-ojb/src/java/org/apache/ojb/otm/lock/map InMemoryLockMap.java LockMap.java

olegnitz    2004/03/01 17:27:15

  Modified:    src/java/org/apache/ojb/otm/core BaseConnection.java
                        ConcreteEditingContext.java Transaction.java
               src/java/org/apache/ojb/otm/lock ObjectLock.java
               src/java/org/apache/ojb/otm/lock/map InMemoryLockMap.java
                        LockMap.java
  Log:
  Now makePersistent can update object with dependants,
  which was changed outside of the current transaction ("long transaction").
  Also fixed some locking flaws and bugs.
  
  Revision  Changes    Path
  1.32      +13 -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.31
  retrieving revision 1.32
  diff -u -r1.31 -r1.32
  --- BaseConnection.java	27 Feb 2004 21:02:05 -0000	1.31
  +++ BaseConnection.java	2 Mar 2004 01:27:15 -0000	1.32
  @@ -367,7 +367,10 @@
       {
           checkTransaction("makePersistent");
           Identity oid = new Identity(object, _pb);
  -        _editingContext.insert(oid, object, LockType.READ_LOCK);
  +        // Assure that old object (if exists) is in the editing 
  +        // context and is write locked
  +        getObjectByIdentity(oid, LockType.WRITE_LOCK);
  +        _editingContext.insert(oid, object, LockType.WRITE_LOCK);
       }
   
       /**
  @@ -377,7 +380,14 @@
       {
           checkTransaction("deletePersistent");
           Identity oid = new Identity(object, _pb);
  -        _editingContext.deletePersistent(oid, object);
  +        // We should remove the existing object with all dependents, rigth?
  +        // Thus we need to replace the provided object (possibly changed 
  +        // or outdated) with the current object from the editing context
  +        object = getObjectByIdentity(oid, LockType.WRITE_LOCK);
  +        if (object != null) 
  +        {
  +            _editingContext.deletePersistent(oid, object);
  +        }
       }
   
       public EditingContext getEditingContext()
  
  
  
  1.38      +21 -18    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.37
  retrieving revision 1.38
  diff -u -r1.37 -r1.38
  --- ConcreteEditingContext.java	29 Feb 2004 15:15:48 -0000	1.37
  +++ ConcreteEditingContext.java	2 Mar 2004 01:27:15 -0000	1.38
  @@ -195,35 +195,38 @@
   
           if (entry == null)
           {
  -            lockManager.ensureLock(oid, _tx, lock, _pb);
               Object swizzledObject = swizzlingStrategy.swizzle(newObj, null, _pb, this);
               entry = new ContextEntry(swizzledObject);
               if (entry.handler != null)
               {
  +                // Assume that object exists, otherwise were the proxy came from?
  +                _objects.put(oid, entry);
  +                lockManager.ensureLock(oid, _tx, lock, _pb); // lock after _objects.put to avoid hanged locks
                   entry.handler.addListener(this);
               }
               else
               {
                   Object origObj = _pb.getObjectByIdentity(oid);
   
  -                if (origObj == null)
  +                if ((origObj == null) && !mainObject && !isDependent)
                   {
  -                    if (mainObject || isDependent)
  -                    {
  -                        entry.state = State.PERSISTENT_NEW;
  -                        if (kit.isEagerInsert(newObj)
  -                                || hasBidirectionalAssociation(newObj.getClass()))
  -                        {
  -                            _pb.store(newObj, entry.state);
  -                            entry.state = State.PERSISTENT_CLEAN;
  -                            origObj = newObj;
  -                        }
  -                    }
  -                    else
  +                    // don't create the object by reachability
  +                    // just do nothing
  +                    return null;
  +                }
  +                
  +                _objects.put(oid, entry);
  +                lockManager.ensureLock(oid, _tx, lock, _pb); // lock after _objects.put to avoid hanged locks
  +
  +                if ((origObj == null) && (mainObject || isDependent))
  +                {
  +                    entry.state = State.PERSISTENT_NEW;
  +                    if (kit.isEagerInsert(newObj)
  +                            || hasBidirectionalAssociation(newObj.getClass()))
                       {
  -                        // don't create the object by reachability
  -                        // just do nothing
  -                        return null;
  +                        _pb.store(newObj, entry.state);
  +                        entry.state = State.PERSISTENT_CLEAN;
  +                        origObj = newObj;
                       }
                   }
   
  @@ -232,7 +235,6 @@
                       _original.put(oid, getFields(origObj, false, true));
                   }
               }
  -            _objects.put(oid, entry);
               if (insertBeforeThis != null)
               {
                   int insertIndex = _order.indexOf(insertBeforeThis);
  @@ -835,6 +837,7 @@
               Identity oid = (Identity) it.next();
               lockManager.releaseLock(oid, _tx);
           }
  +        _tx.getKit().getLockMap().gc();
       }
   
       /**
  
  
  
  1.11      +2 -2      db-ojb/src/java/org/apache/ojb/otm/core/Transaction.java
  
  Index: Transaction.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/otm/core/Transaction.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- Transaction.java	23 Feb 2004 19:10:21 -0000	1.10
  +++ Transaction.java	2 Mar 2004 01:27:15 -0000	1.11
  @@ -98,9 +98,9 @@
               throw new TransactionInProgressException(
                   "Transaction already in progress, cannot restart");
           }
  -        
  +
           _isInProgress = true;
  -        
  +
           for (Iterator iterator = _connections.iterator(); iterator.hasNext();)
           {
               BaseConnection connection = (BaseConnection) iterator.next();
  
  
  
  1.8       +6 -1      db-ojb/src/java/org/apache/ojb/otm/lock/ObjectLock.java
  
  Index: ObjectLock.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/otm/lock/ObjectLock.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- ObjectLock.java	27 Feb 2004 20:21:35 -0000	1.7
  +++ ObjectLock.java	2 Mar 2004 01:27:15 -0000	1.8
  @@ -124,7 +124,7 @@
   
       public void readLock(Transaction tx)
       {
  -        if (!isReader(tx)) 
  +        if (!isReader(tx))
           {
               new LockEntry(tx);
           }
  @@ -161,6 +161,11 @@
           OTMKit kit = tx.getKit();
           LockWaitStrategy waitStrategy = kit.getLockWaitStrategy();
           waitStrategy.waitForLock(this, tx);
  +    }
  +
  +    public boolean isFree()
  +    {
  +        return ((_writer == null) && _readers.isEmpty());
       }
   
       /////////////////////////////////////////
  
  
  
  1.4       +23 -0     db-ojb/src/java/org/apache/ojb/otm/lock/map/InMemoryLockMap.java
  
  Index: InMemoryLockMap.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/otm/lock/map/InMemoryLockMap.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- InMemoryLockMap.java	27 Feb 2004 20:21:36 -0000	1.3
  +++ InMemoryLockMap.java	2 Mar 2004 01:27:15 -0000	1.4
  @@ -54,7 +54,9 @@
    * <http://www.apache.org/>.
    */
   
  +import java.util.Iterator;
   import java.util.HashMap;
  +import java.util.Map;
   
   import org.apache.ojb.broker.Identity;
   import org.apache.ojb.otm.lock.ObjectLock;
  @@ -96,5 +98,26 @@
               }
           }
           return lock;
  +    }
  +
  +    public void gc()
  +    {
  +        synchronized (_locks)
  +        {
  +            for (Iterator it = _locks.entrySet().iterator(); it.hasNext(); )
  +            {
  +                Map.Entry entry = (Map.Entry) it.next();
  +                ObjectLock lock = (ObjectLock) entry.getValue();
  +                if (lock.isFree())
  +                {
  +                    it.remove();
  +                }
  +            }
  +        }
  +    }
  +
  +    public String toString()
  +    {
  +        return _locks.toString();
       }
   }
  
  
  
  1.3       +12 -3     db-ojb/src/java/org/apache/ojb/otm/lock/map/LockMap.java
  
  Index: LockMap.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/otm/lock/map/LockMap.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- LockMap.java	27 Feb 2004 20:21:36 -0000	1.2
  +++ LockMap.java	2 Mar 2004 01:27:15 -0000	1.3
  @@ -60,11 +60,20 @@
   /**
    *
    * <javadoc>
  - * 
  + *
    * @author <a href="mailto:rraghuram@hotmail.com">Raghu Rajah</a>
  - * 
  + *
    */
   public interface LockMap
   {
  -    public ObjectLock getLock (Identity oid);
  +    
  +    public ObjectLock getLock(Identity oid);
  +
  +    /**
  +     * This is "garbage collection" - remove free locks from 
  +     * the map. This method is called at the end of each transaction.
  +     * Maybe that would be better to call it by timer?
  +     */
  +    public void gc();
  +
   }
  
  
  

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