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/broker/core PersistenceBrokerImpl.java QueryReferenceBroker.java
Date Fri, 12 Nov 2004 00:15:59 GMT
arminw      2004/11/11 16:15:59

  Modified:    src/java/org/apache/ojb/broker/accesslayer RsIterator.java
               src/java/org/apache/ojb/broker/cache LocalCache.java
               src/java/org/apache/ojb/broker/core
                        PersistenceBrokerImpl.java
                        QueryReferenceBroker.java
  Log:
  fix, among other things LocalCache was used to buffer objects while materialization. If
the materialization of an object fails (e.g. while performing the query), we have to guarantee
that
  none partial materialized objects will be pushed to the second level cache. So do clear
the local cache when exception occurs while object materialization.
  
  Revision  Changes    Path
  1.70      +8 -3      db-ojb/src/java/org/apache/ojb/broker/accesslayer/RsIterator.java
  
  Index: RsIterator.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/accesslayer/RsIterator.java,v
  retrieving revision 1.69
  retrieving revision 1.70
  diff -u -r1.69 -r1.70
  --- RsIterator.java	6 Oct 2004 23:09:59 -0000	1.69
  +++ RsIterator.java	12 Nov 2004 00:15:58 -0000	1.70
  @@ -463,6 +463,7 @@
                           try
                           {
                               getCache().doLocalCache(oid, result, LocalCache.TYPE_READ_NEW);
  +
                               /**
                                * MBAIRD if you have multiple classes mapped to a
                                * table, and you query on the base class you could get
  @@ -477,10 +478,14 @@
                               // Maps ReferenceDescriptors to HashSets of owners
                               getBroker().getReferenceBroker().retrieveReferences(result,
cld, unforced);
                               getBroker().getReferenceBroker().retrieveCollections(result,
cld, unforced);
  +
  +                            getCache().disableMaterializationCache();
                           }
  -                        finally
  +                        catch(RuntimeException e)
                           {
  -                            getCache().disableMaterializationCache();
  +                            // catch runtime exc. to guarantee clearing of internal buffer
on failure
  +                            getCache().reset(false);
  +                            throw e;
                           }
                       }
                   }
  
  
  
  1.4       +25 -3     db-ojb/src/java/org/apache/ojb/broker/cache/LocalCache.java
  
  Index: LocalCache.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/cache/LocalCache.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- LocalCache.java	20 Sep 2004 16:13:36 -0000	1.3
  +++ LocalCache.java	12 Nov 2004 00:15:58 -0000	1.4
  @@ -92,15 +92,34 @@
           return broker.getConfiguration().getCacheManager().getCacheFor(oid.getObjectsTopLevelClass());
       }
   
  +    /**
  +     * Helper method to guarantee that only full materialized objects
  +     * will be pushed to the real cache regardless if an local PB transaction
  +     * is running or not. When a complex object is materialized there will be
  +     * nested calls to the same PB instance methods, e.g. materialization of a referenced
  +     * object which itself have several references, ...
  +     * This method and {@link #disableMaterializationCache()} are used to delimit nested
calls
  +     * and to detect the end of an object materialization.
  +     */
       public void enableMaterializationCache()
       {
           ++invokeCounter;
           enabledReadCache = true;
       }
   
  +    /**
  +     * @see #enableMaterializationCache()
  +     */
       public void disableMaterializationCache()
       {
  +        if(!enabledReadCache) return;
  +
           --invokeCounter;
  +        /*
  +        if materialization of the requested object was completed, the
  +        counter represents '0' and if we are not in a tx, push the object
  +        to the real cache
  +        */
           if (invokeCounter == 0 && !broker.isInTransaction())
           {
               pushToRealCache();
  @@ -193,8 +212,11 @@
           }
           else
           {
  -            if (log.isEnabledFor(Logger.INFO)) log.info("By pass local cache and push object
to real cache");
  -            if (entry.getType() != TYPE_CACHED_READ) getCacheStrategy(oid).cache(broker,
oid, entry);
  +            if (log.isEnabledFor(Logger.INFO)) log.info("Can't cache object " + oid +
  +                    "Local cache is not prepared, no running tx or enabled local cache");
  +            // TODO: sould we allow to bypass the first level cache?
  +            // if (log.isEnabledFor(Logger.INFO)) log.info("By pass local cache and push
object to real cache");
  +            // if (entry.getType() != TYPE_CACHED_READ) getCacheStrategy(oid).cache(broker,
oid, entry);
           }
       }
   
  
  
  
  1.97      +31 -43    db-ojb/src/java/org/apache/ojb/broker/core/PersistenceBrokerImpl.java
  
  Index: PersistenceBrokerImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/core/PersistenceBrokerImpl.java,v
  retrieving revision 1.96
  retrieving revision 1.97
  diff -u -r1.96 -r1.97
  --- PersistenceBrokerImpl.java	20 Oct 2004 18:50:45 -0000	1.96
  +++ PersistenceBrokerImpl.java	12 Nov 2004 00:15:59 -0000	1.97
  @@ -1336,55 +1336,43 @@
   
       public Object getObjectByIdentity(Identity id) throws PersistenceBrokerException
       {
  -        localCache.enableMaterializationCache();
  -        Object result;
  +        Object obj = null;
           try
           {
  -            result = doGetObjectByIdentity(id);
  -        }
  -        finally
  -        {
  -            localCache.disableMaterializationCache();
  -        }
  -        return result;
  -    }
  +            if (logger.isDebugEnabled()) logger.debug("getObjectByIdentity " + id);
   
  -    /**
  -     * Internal used method to retrieve object based on Identity.
  -     *
  -     * @param id
  -     * @return
  -     * @throws PersistenceBrokerException
  -     */
  -    public Object doGetObjectByIdentity(Identity id) throws PersistenceBrokerException
  -    {
  -        if (logger.isDebugEnabled()) logger.debug("getObjectByIdentity " + id);
  +            localCache.enableMaterializationCache();
  +            // check if object is present in ObjectCache:
  +            obj = localCache.lookup(id);
  +            // only perform a db lookup if necessary (object not cached yet)
  +            if (obj == null)
  +            {
  +                obj = getDBObject(id);
  +            }
  +            else
  +            {
  +                ClassDescriptor cld = getClassDescriptor(obj.getClass());
  +                // if specified in the ClassDescriptor the instance must be refreshed
  +                if (cld.isAlwaysRefresh())
  +                {
  +                    refreshInstance(obj, id, cld);
  +                }
  +                // now refresh all references
  +                refreshRelationships(obj, cld);
  +            }
   
  -        // check if object is present in ObjectCache:
  -        Object obj = localCache.lookup(id);
  -        // only perform a db lookup if necessary (object not cached yet)
  -        if (obj == null)
  -        {
  -            obj = getDBObject(id);
  +            // Invoke events on PersistenceBrokerAware instances and listeners
  +            AFTER_LOOKUP_EVENT.setTarget(obj);
  +            fireBrokerEvent(AFTER_LOOKUP_EVENT);
  +            AFTER_LOOKUP_EVENT.setTarget(null);
  +            localCache.disableMaterializationCache();
           }
  -        else
  +        catch(RuntimeException e)
           {
  -            ClassDescriptor cld = getClassDescriptor(obj.getClass());
  -            // if specified in the ClassDescriptor the instance must be refreshed
  -            if (cld.isAlwaysRefresh())
  -            {
  -                refreshInstance(obj, id, cld);
  -            }
  -            // now refresh all references
  -            refreshRelationships(obj, cld);
  +            // catch runtime exc. to guarantee clearing of internal buffer on failure
  +            localCache.reset(false);
  +            throw e;
           }
  -
  -        // Invoke events on PersistenceBrokerAware instances and listeners
  -        AFTER_LOOKUP_EVENT.setTarget(obj);
  -        fireBrokerEvent(AFTER_LOOKUP_EVENT);
  -        AFTER_LOOKUP_EVENT.setTarget(null);
  -
  -        //logger.info("RETRIEVING object " + obj);
           return obj;
       }
   
  
  
  
  1.22      +39 -15    db-ojb/src/java/org/apache/ojb/broker/core/QueryReferenceBroker.java
  
  Index: QueryReferenceBroker.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/core/QueryReferenceBroker.java,v
  retrieving revision 1.21
  retrieving revision 1.22
  diff -u -r1.21 -r1.22
  --- QueryReferenceBroker.java	20 Oct 2004 18:50:45 -0000	1.21
  +++ QueryReferenceBroker.java	12 Nov 2004 00:15:59 -0000	1.22
  @@ -103,7 +103,9 @@
               // Maps ReferenceDescriptors to HashSets of owners
               m_retrievalTasks = new HashMap();
           }
  +
           pb.ojbLocalCache().enableMaterializationCache();
  +
           try
           {
               result = (ManageableCollection) collectionClass.newInstance();
  @@ -157,8 +159,6 @@
                       }
                   }
               }
  -
  -
               if (isRetrievalTasksCreated)
               {
                   // turn off auto prefetching for related proxies
  @@ -171,16 +171,23 @@
           catch (InstantiationException ex)
           {
               log.error(ex);
  +            pb.ojbLocalCache().reset(false);
               throw new PersistenceBrokerException(ex);
           }
           catch (IllegalAccessException ex)
           {
               log.error(ex);
  +            pb.ojbLocalCache().reset(false);
               throw new PersistenceBrokerException(ex);
           }
  +        catch(RuntimeException e)
  +        {
  +            // catch runtime exc. to guarantee clearing of internal buffer on failure
  +            pb.ojbLocalCache().reset(false);
  +            throw e;
  +        }
           finally
           {
  -            pb.ojbLocalCache().disableMaterializationCache();
               if (iter != null)
               {
                   iter.releaseDbResources();
  @@ -191,6 +198,8 @@
               }
           }
   
  +        pb.ojbLocalCache().disableMaterializationCache();
  +
           // BRJ: store fullSize in Query to re-enable deprecated functionality
           // to be removed when Query.fullSize is removed
           if (fullSize < 0)
  @@ -361,7 +370,7 @@
                   } //JMM : why not see if the object has already been loaded
                   else if ( pb.ojbLocalCache().lookup(id) != null )
                   {
  -                    refObj = pb.doGetObjectByIdentity(id);
  +                    refObj = pb.getObjectByIdentity(id);
                   }
                   else if ((m_retrievalTasks != null)
                           && !rds.isLazy()
  @@ -395,10 +404,14 @@
                       }
                   }
               }
  -            finally
  +            catch(RuntimeException e)
               {
  -                pb.ojbLocalCache().disableMaterializationCache();
  +                // catch runtime exc. to guarantee clearing of internal buffer on failure
  +                pb.ojbLocalCache().reset(false);
  +                throw e;
               }
  +
  +            pb.ojbLocalCache().disableMaterializationCache();
           }
       }
   
  @@ -417,7 +430,6 @@
           Class saveClassToPrefetch = classToPrefetch;
           classToPrefetch = null;
           pb.ojbLocalCache().enableMaterializationCache();
  -
           try
           {
               while (i.hasNext())
  @@ -426,10 +438,14 @@
                   retrieveReference(newObj, cld, rds, forced);
               }
           }
  -        finally
  +        catch(RuntimeException e)
           {
  -            pb.ojbLocalCache().disableMaterializationCache();
  +            // catch runtime exc. to guarantee clearing of internal buffer on failure
  +            pb.ojbLocalCache().reset(false);
  +            throw e;
           }
  +        pb.ojbLocalCache().disableMaterializationCache();
  +
           classToPrefetch = saveClassToPrefetch;
       }
   
  @@ -545,7 +561,7 @@
           }
           else
           {
  -            return pb.doGetObjectByIdentity(id);
  +            return pb.getObjectByIdentity(id);
           }
       }
   
  @@ -625,10 +641,14 @@
                           }
                       }
                   }
  -                finally
  +                catch(RuntimeException e)
                   {
  -                    pb.ojbLocalCache().disableMaterializationCache();
  +                    // catch runtime exc. to guarantee clearing of internal buffer on failure
  +                    pb.ojbLocalCache().reset(false);
  +                    throw e;
                   }
  +
  +                pb.ojbLocalCache().disableMaterializationCache();
               }
           }
       }
  @@ -777,10 +797,14 @@
                   retrieveCollection(newObj, cld, cds, forced);
               }
           }
  -        finally
  +        catch(RuntimeException e)
           {
  -            pb.ojbLocalCache().disableMaterializationCache();
  +            // catch runtime exc. to guarantee clearing of internal buffer on failure
  +            pb.ojbLocalCache().reset(false);
  +            throw e;
           }
  +        pb.ojbLocalCache().disableMaterializationCache();
  +
           classToPrefetch = saveClassToPrefetch;
       }
   
  
  
  

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