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 2003/05/15 00:15:37 UTC

cvs commit: db-ojb/src/java/org/apache/ojb/otm/core BaseConnection.java ConcreteEditingContext.java RequestContext.java

olegnitz    2003/05/14 15:15:37

  Modified:    src/java/org/apache/ojb/otm EditingContext.java
               src/java/org/apache/ojb/otm/cache GlobalCache.java
               src/java/org/apache/ojb/otm/core BaseConnection.java
                        ConcreteEditingContext.java RequestContext.java
  Log:
  Some cache problems solved
  
  Revision  Changes    Path
  1.3       +11 -10    db-ojb/src/java/org/apache/ojb/otm/EditingContext.java
  
  Index: EditingContext.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/otm/EditingContext.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- EditingContext.java	16 Mar 2003 18:13:06 -0000	1.2
  +++ EditingContext.java	14 May 2003 22:15:36 -0000	1.3
  @@ -83,24 +83,25 @@
        *
        */
       public void insert (OTMKit kit, Identity oid, Object object, int lock)
  -        throws LockingException;
  -    
  +            throws LockingException;
  +
       /**
  -     * 
  +     *
        * Remove a managed object from the management of this EditingContext. All edits on the object
        * will be lost. All locks kept by this object will be released.
  -     * 
  +     *
        * @param oid                   the Identity of the object to be removed from this context.
  -     * 
  +     *
        */
       public void remove (Identity oid);
  -    
  +
       /**
  -     * 
  +     *
        * Lookup object with the given oid in the Context.
  -     * 
  +     *
        * @param oid           the oid of the object to lookup
  -     * 
  +     *
        */
  -    public Object lookup (Identity oid);
  +    public Object lookup (Identity oid)
  +            throws LockingException;
   }
  
  
  
  1.5       +16 -28    db-ojb/src/java/org/apache/ojb/otm/cache/GlobalCache.java
  
  Index: GlobalCache.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/otm/cache/GlobalCache.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- GlobalCache.java	19 Mar 2003 06:48:58 -0000	1.4
  +++ GlobalCache.java	14 May 2003 22:15:36 -0000	1.5
  @@ -18,17 +18,14 @@
   {
       private final OTMKit _kit;
   
  -    public GlobalCache (OTMKit kit)
  +    public GlobalCache(OTMKit kit)
       {
           _kit = kit;
       }
   
  -	/**
  -	 * @see org.apache.ojb.broker.cache.ObjectCache#cache(Identity, Object)
  -	 */
  -	public void cache (Identity oid, Object obj, int lockHeld)
  -	{
  -        if ((lockHeld != LockType.WRITE_LOCK) && (obj != null))
  +    public void cache(Identity oid, Object obj)
  +    {
  +        if (obj != null)
           {
               ObjectCopyStrategy copyStrategy = _kit.getCopyStrategy(oid);
               obj = copyStrategy.copy(obj);
  @@ -36,42 +33,33 @@
   
           ObjectStore store = _kit.getCacheStore();
           store.store(oid, obj);
  -	}
  +    }
   
  -	/**
  -	 * @see org.apache.ojb.broker.cache.ObjectCache#lookup(Identity)
  -	 */
  -	public Object lookup (Identity oid, int lockHeld)
  -	{
  -		Object obj;
  +    public Object lookup(Identity oid)
  +    {
  +        Object obj;
           ObjectStore store = _kit.getCacheStore();
           obj = store.retrieve(oid);
   
  -        if ((lockHeld != LockType.WRITE_LOCK) && (obj != null))
  +        if (obj != null)
           {
               ObjectCopyStrategy copyStrategy = _kit.getCopyStrategy(oid);
               obj = copyStrategy.copy(obj);
           }
   
           return obj;
  -	}
  +    }
   
  -	/**
  -	 * @see org.apache.ojb.broker.cache.ObjectCache#remove(Object)
  -	 */
  -	public void evict (Identity oid)
  -	{
  +    public void evict(Identity oid)
  +    {
           ObjectStore store = _kit.getCacheStore();
           store.remove(oid);
  -	}
  +    }
   
  -	/**
  -	 * @see org.apache.ojb.broker.cache.ObjectCache#clear()
  -	 */
  -	public void evictAll ()
  -	{
  +    public void evictAll()
  +    {
           ObjectStore store = _kit.getCacheStore();
           store.removeAll();
  -	}
  +    }
   
   }
  
  
  
  1.5       +13 -13    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.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- BaseConnection.java	26 Apr 2003 23:18:24 -0000	1.4
  +++ BaseConnection.java	14 May 2003 22:15:37 -0000	1.5
  @@ -86,23 +86,23 @@
        * Constructor for BaseConnection.
        *
        */
  -    public BaseConnection (PBKey pbKey)
  +    public BaseConnection(PBKey pbKey)
       {
           _requestContext = new RequestContext(this);
           _kernel = createKernel(pbKey, _requestContext);
       }
   
  -    public PersistenceBroker getKernelBroker ()
  +    public PersistenceBroker getKernelBroker()
       {
           return _kernel;
       }
   
  -    public void setTransaction (Transaction transaction)
  +    public void setTransaction(Transaction transaction)
       {
           _transaction = transaction;
       }
   
  -    public Transaction getTransaction ()
  +    public Transaction getTransaction()
       {
           return _transaction;
       }
  @@ -154,7 +154,7 @@
        * @param object The object
        * @return the identity of the object
        */
  -    public Identity getIdentity (Object object)
  +    public Identity getIdentity(Object object)
       {
           return new Identity(object, _kernel);
       }
  @@ -215,7 +215,7 @@
           editingContext.deletePersistent(kit, oid, object);
       }
   
  -    public EditingContext getEditingContext ()
  +    public EditingContext getEditingContext()
       {
           return _transaction.getContext();
       }
  @@ -230,7 +230,7 @@
        *  Notification issued by the driving transaction to begin this transaction
        *
        */
  -    public abstract void transactionBegin ()
  +    public abstract void transactionBegin()
           throws TransactionException;
   
       /**
  @@ -261,7 +261,7 @@
        *  connection.
        *
        */
  -    public abstract void transactionCommit ()
  +    public abstract void transactionCommit()
           throws TransactionException;
   
       /**
  @@ -270,7 +270,7 @@
        *  connection.
        *
        */
  -    public abstract void transactionRollback ()
  +    public abstract void transactionRollback()
           throws TransactionException;
   
   
  @@ -278,7 +278,7 @@
       // protected operations
       ///////////////////////////////////////
   
  -    protected PersistenceBroker createKernel (PBKey pbKey, RequestContext requestContext)
  +    protected PersistenceBroker createKernel(PBKey pbKey, RequestContext requestContext)
       {
           PersistenceBrokerKernel pb = new PersistenceBrokerKernel(pbKey);
   
  @@ -292,7 +292,7 @@
       // Private Operations
       ///////////////////////////////////////
   
  -    private void establishContext (int lock)
  +    private void establishContext(int lock)
       {
           if (_requestContext.isInUse())
           {
  @@ -316,12 +316,12 @@
           extends PersistenceBrokerImpl
       {
   
  -        public PersistenceBrokerKernel (PBKey pbKey)
  +        public PersistenceBrokerKernel(PBKey pbKey)
           {
               super(pbKey, PersistenceBrokerFactoryFactory.instance());
           }
   
  -        public void setObjectCache (ObjectCache cache)
  +        public void setObjectCache(ObjectCache cache)
           {
               objectCache = cache;
           }
  
  
  
  1.4       +45 -16    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.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ConcreteEditingContext.java	19 Mar 2003 06:48:58 -0000	1.3
  +++ ConcreteEditingContext.java	14 May 2003 22:15:37 -0000	1.4
  @@ -54,8 +54,9 @@
    * <http://www.apache.org/>.
    */
   
  -import java.util.HashMap;
  +import java.util.ArrayList;
   import java.util.Iterator;
  +import java.util.HashMap;
   
   import org.apache.ojb.broker.Identity;
   import org.apache.ojb.broker.PersistenceBroker;
  @@ -78,16 +79,18 @@
   public class ConcreteEditingContext implements EditingContext
   {
       private HashMap _objects;
  +    private ArrayList _order;
       private Transaction _transaction;
   
       //////////////////////////////////////////
       // Constructor
       //////////////////////////////////////////
   
  -    public ConcreteEditingContext (Transaction transaction)
  +    public ConcreteEditingContext(Transaction transaction)
       {
           _transaction = transaction;
           _objects = new HashMap();
  +        _order = new ArrayList();
       }
   
       //////////////////////////////////////////
  @@ -97,7 +100,7 @@
       /**
        * @see org.apache.ojb.otm.EditingContext#insert(Object, int)
        */
  -    public void insert (OTMKit kit, Identity oid, Object insertedObject, int lock)
  +    public void insert(OTMKit kit, Identity oid, Object insertedObject, int lock)
               throws LockingException
       {
           ContextEntry entry = insertInternal(kit, oid, insertedObject, lock);
  @@ -108,7 +111,7 @@
           }
       }
   
  -    private ContextEntry insertInternal (OTMKit kit, Identity oid, Object insertedObject, int lock)
  +    private ContextEntry insertInternal(OTMKit kit, Identity oid, Object insertedObject, int lock)
               throws LockingException
       {
           ContextEntry entry = (ContextEntry) _objects.get(oid);
  @@ -147,37 +150,59 @@
                   entry.object = swizzledObject;
               }
           }
  +        if (!_order.contains(oid)) {
  +            _order.add(oid);
  +        }
           return entry;
       }
   
       /**
        * @see org.apache.ojb.otm.EditingContext#remove(Identity)
        */
  -    public void remove (Identity oid)
  +    public void remove(Identity oid)
       {
           _objects.remove(oid);
  +        _order.remove(oid);
       }
   
       /**
        * @see org.apache.ojb.otm.EditingContext#remove(Identity)
        */
  -    public void deletePersistent (OTMKit kit, Identity oid, Object object)
  +    public void deletePersistent(OTMKit kit, Identity oid, Object object)
               throws LockingException
       {
  -        ContextEntry entry = insertInternal(kit, oid, object, LockType.WRITE_LOCK);
  +        ContextEntry entry;
   
  +        if (_order.contains(oid)) {
  +            _order.remove(oid);
  +        }
  +        entry = insertInternal(kit, oid, object, LockType.WRITE_LOCK);
           entry.state = entry.state.deletePersistent();
       }
   
       /**
        * @see org.apache.ojb.otm.EditingContext#lookup(Identity)
        */
  -    public Object lookup (Identity oid)
  +    public Object lookup(Identity oid)
       {
           ContextEntry entry = (ContextEntry) _objects.get(oid);
  +
           return (entry == null ? null : entry.object);
       }
   
  +    public Object getFromGlobalCache(OTMKit kit, Identity oid, int lock)
  +            throws LockingException
  +    {
  +        Object object = kit.getGlobalCache().lookup(oid);
  +
  +        if (object != null)
  +        {
  +            insertInternal(kit, oid, object, lock);
  +        }
  +
  +        return object;
  +    }
  +
   
       //////////////////////////////////////////
       // Other operations
  @@ -193,13 +218,13 @@
        *                      into the persistent store.
        *
        */
  -    public void commit (OTMKit kit, PersistenceBroker pb)
  +    public void commit(OTMKit kit, PersistenceBroker pb)
       {
           Swizzling swizzlingStrategy = kit.getSwizzlingStrategy();
   
  -        for (Iterator iterator = _objects.values().iterator(); iterator.hasNext();)
  +        for (Iterator iterator = _order.iterator(); iterator.hasNext();)
           {
  -            ContextEntry entry = (ContextEntry) iterator.next();
  +            ContextEntry entry = (ContextEntry) _objects.get(iterator.next());
               Object realTarget = swizzlingStrategy.getRealTarget(entry.object);
               State state = entry.state;
   
  @@ -213,6 +238,8 @@
               }
               entry.state = state.commit();
           }
  +        _objects.clear();
  +        _order.clear();
       }
   
       /**
  @@ -221,13 +248,15 @@
        *  a rollback.
        *
        */
  -    public void rollback ()
  +    public void rollback()
       {
  -        for (Iterator iterator = _objects.values().iterator(); iterator.hasNext();)
  +        for (Iterator iterator = _order.iterator(); iterator.hasNext();)
           {
  -            ContextEntry entry = (ContextEntry) iterator.next();
  +            ContextEntry entry = (ContextEntry) _objects.get(iterator.next());
               entry.state = entry.state.rollback();
           }
  +        _objects.clear();
  +        _order.clear();
       }
   
   
  @@ -238,9 +267,9 @@
       private static class ContextEntry
       {
           Object object;
  -        State state = State.HOLLOW;
  +        State state = State.PERSISTENT_CLEAN;
   
  -        ContextEntry (Object object)
  +        ContextEntry(Object object)
           {
               this.object = object;
           }
  
  
  
  1.6       +26 -19    db-ojb/src/java/org/apache/ojb/otm/core/RequestContext.java
  
  Index: RequestContext.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/otm/core/RequestContext.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- RequestContext.java	19 Mar 2003 06:48:58 -0000	1.5
  +++ RequestContext.java	14 May 2003 22:15:37 -0000	1.6
  @@ -55,9 +55,11 @@
    */
   
   import org.apache.ojb.broker.Identity;
  +import org.apache.ojb.broker.PersistenceBrokerException;
   import org.apache.ojb.broker.cache.ObjectCache;
   import org.apache.ojb.otm.OTMKit;
   import org.apache.ojb.otm.lock.LockType;
  +import org.apache.ojb.otm.lock.LockingException;
   
   /**
    *
  @@ -74,7 +76,7 @@
       private int _requestedLockType;
       private boolean _inUse;
   
  -    public RequestContext (BaseConnection connection)
  +    public RequestContext(BaseConnection connection)
       {
           _connection = connection;
           _inUse = false;
  @@ -88,7 +90,7 @@
        * @return the connection in context
        *
        */
  -    public BaseConnection getConnection ()
  +    public BaseConnection getConnection()
       {
           return _connection;
       }
  @@ -100,7 +102,7 @@
        * @return the requested Lock type
        *
        */
  -    public int getRequestedLockType ()
  +    public int getRequestedLockType()
       {
           return _requestedLockType;
       }
  @@ -111,13 +113,13 @@
        *
        * @param requestedLockType The requestedLockType to set
        */
  -    public void establishContext (int requestedLockType)
  +    public void establishContext(int requestedLockType)
       {
           _requestedLockType = requestedLockType;
           _inUse = true;
       }
   
  -    public void releaseContext ()
  +    public void releaseContext()
       {
           _inUse = false;
       }
  @@ -135,39 +137,44 @@
       /**
        * @see org.apache.ojb.broker.cache.ObjectCache#cache(Identity, Object)
        */
  -    public void cache (Identity oid, Object obj)
  +    public void cache(Identity oid, Object obj)
       {
           OTMKit kit = _connection.getTransaction().getKit();
  -        int lock = LockType.NO_LOCK;
  -
  -        if (_inUse)
  -        {
  -            lock = _requestedLockType;
  -        }
  -        kit.getGlobalCache().cache(oid, obj, lock);
  +        kit.getGlobalCache().cache(oid, obj);
       }
   
       /**
        * @see org.apache.ojb.broker.cache.ObjectCache#clear()
        */
  -    public void clear ()
  +    public void clear()
       {
  -        throw new UnsupportedOperationException("Cannot call clear on the OTM RequestContext");
  +         OTMKit kit = _connection.getTransaction().getKit();
  +         kit.getGlobalCache().evictAll();
       }
   
       /**
        * @see org.apache.ojb.broker.cache.ObjectCache#lookup(Identity)
        */
  -    public Object lookup (Identity oid)
  +    public Object lookup(Identity oid)
       {
           OTMKit kit = _connection.getTransaction().getKit();
           int lock = LockType.NO_LOCK;
  +        ConcreteEditingContext editingContext =
  +                _connection.getTransaction().getContext();
   
           if (_inUse)
           {
               lock = _requestedLockType;
           }
  -        return kit.getGlobalCache().lookup(oid, lock);
  +
  +        try
  +        {
  +            return editingContext.getFromGlobalCache(kit, oid, lock);
  +        }
  +        catch (LockingException ex)
  +        {
  +            throw new PersistenceBrokerException(ex);
  +        }
       }
   
       /**
  @@ -175,7 +182,7 @@
        */
       public void remove (Identity oid)
       {
  -        ConcreteEditingContext editingContext = _connection.getTransaction().getContext();
  -        editingContext.remove(oid);
  +         OTMKit kit = _connection.getTransaction().getKit();
  +         kit.getGlobalCache().evict(oid);
       }
   }