db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mka...@apache.org
Subject cvs commit: db-ojb/src/test/org/apache/ojb/broker/metadata MetadataMultithreadedTest.java
Date Fri, 11 Mar 2005 18:01:05 GMT
mkalen      2005/03/11 10:01:05

  Modified:    src/java/org/apache/ojb/broker/core/proxy
                        AbstractCollectionProxy.java
               src/java/org/apache/ojb/broker/metadata MetadataManager.java
               src/test/org/apache/ojb/broker/metadata
                        MetadataMultithreadedTest.java
  Log:
  Merge with OJB_1_0_RELEASE branch: Fix bug when materialising collection proxy created with
pre-thread metadata changes activated.
  
  Revision  Changes    Path
  1.3       +73 -2     db-ojb/src/java/org/apache/ojb/broker/core/proxy/AbstractCollectionProxy.java
  
  Index: AbstractCollectionProxy.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/core/proxy/AbstractCollectionProxy.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- AbstractCollectionProxy.java	5 Dec 2004 12:31:01 -0000	1.2
  +++ AbstractCollectionProxy.java	11 Mar 2005 18:01:05 -0000	1.3
  @@ -30,6 +30,8 @@
   import org.apache.ojb.broker.PersistenceBroker;
   import org.apache.ojb.broker.PersistenceBrokerException;
   import org.apache.ojb.broker.PersistenceConfiguration;
  +import org.apache.ojb.broker.metadata.MetadataManager;
  +import org.apache.ojb.broker.metadata.MetadataException;
   import org.apache.ojb.broker.core.PersistenceBrokerThreadMapping;
   import org.apache.ojb.broker.query.Query;
   import org.apache.ojb.broker.util.collections.RemovalAwareCollection;
  @@ -53,6 +55,13 @@
        * holding onto a link into OJB for longer than necessary.
        */
       private transient PersistenceConfiguration persistenceConf;
  +    /**
  +     * Profile key used when lazy-loading with per-thread metadata profiles.
  +     * Cleared when loading data.
  +     */
  +    private transient Object profileKey;
  +    /** Flag set when per-thread metadata profiles are in use. */
  +    private boolean _perThreadDescriptorsEnabled;
       /** The query that defines the values in the collection */
       private Query query;
       /** The actual data (if already loaded) */
  @@ -88,11 +97,35 @@
        */
       public AbstractCollectionProxy(PersistenceConfiguration persistenceConf, Class collClass,
Query query)
       {
  +        final MetadataManager mm = persistenceConf.getOjb().getMetadataManager();
  +        _perThreadDescriptorsEnabled = mm.isEnablePerThreadChanges();
  +        if (_perThreadDescriptorsEnabled)
  +        {
  +            // mkalen:  To minimize memory footprint we remember only the OJB profile key
  +            //          (instead of all active class-mappings).
  +            Object key = mm.getCurrentProfileKey();
  +            if (key == null)
  +            {
  +                // mkalen:  Unsupported: using proxies with per-thread metadata changes
without profile keys.
  +                throw new MetadataException("Trying to create a CollectionProxy with per-thread
metadata changes enabled, but no profile key.");
  +            }
  +            setProfileKey(key);
  +        }
           this.persistenceConf = persistenceConf;
           setCollectionClass(collClass);
           setQuery(query);
       }
   
  +    protected void loadProfile()
  +    {
  +        final Object key = getProfileKey();
  +        if (key != null)
  +        {
  +            final MetadataManager mm = persistenceConf.getOjb().getMetadataManager();
  +            mm.loadProfile(key);
  +        }
  +    }
  +
       /**
        * @see org.apache.ojb.broker.core.proxy.CollectionProxy#load()
        */
  @@ -102,8 +135,9 @@
           {
               beforeLoading();
               setData(loadData());
  -            // release reference into OJB
  +            // release reference to OJB and profile-key
               persistenceConf = null;
  +            profileKey = null;
               afterLoading();
           }
       }
  @@ -129,6 +163,10 @@
           PersistenceBroker broker = getBroker();
           try
           {
  +            if (_perThreadDescriptorsEnabled)
  +            {
  +                loadProfile();
  +            }
               return broker.getCount(getQuery());
           }
           catch (Exception ex)
  @@ -170,6 +208,10 @@
               }
               else if (size != 0)
               {
  +                if (_perThreadDescriptorsEnabled)
  +                {
  +                    loadProfile();
  +                }
                   result = (Collection) broker.getCollectionByQuery(getCollectionClass(),
getQuery());
               }
               else
  @@ -197,6 +239,10 @@
           {
               CollectionProxyListener listener;
   
  +            if (_perThreadDescriptorsEnabled)
  +            {
  +                loadProfile();
  +            }
               for (int idx = listeners.size() - 1; idx >= 0; idx--)
               {
                   listener = (CollectionProxyListener)listeners.get(idx);
  @@ -214,6 +260,10 @@
           {
               CollectionProxyListener listener;
   
  +            if (_perThreadDescriptorsEnabled)
  +            {
  +                loadProfile();
  +            }
               for (int idx = listeners.size() - 1; idx >= 0; idx--)
               {
                   listener = (CollectionProxyListener)listeners.get(idx);
  @@ -513,6 +563,27 @@
       }
   
       /**
  +     * Returns the metadata profile key used when creating this proxy.
  +     *
  +     * @return the metadata profile key
  +     * or null if per-thread changes were not active
  +     */
  +    protected Object getProfileKey()
  +    {
  +        return profileKey;
  +    }
  +
  +    /**
  +     * Sets the metadata profile key used when creating this proxy.
  +     *
  +     * @param profileKey the metadata profile key
  +     */
  +    public void setProfileKey(Object profileKey)
  +    {
  +        this.profileKey = profileKey;
  +    }
  +
  +    /**
        * Adds a listener to this collection.
        * 
        * @param listener The listener to add
  
  
  
  1.25      +24 -3     db-ojb/src/java/org/apache/ojb/broker/metadata/MetadataManager.java
  
  Index: MetadataManager.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/metadata/MetadataManager.java,v
  retrieving revision 1.24
  retrieving revision 1.25
  diff -u -r1.24 -r1.25
  --- MetadataManager.java	18 Dec 2004 13:57:02 -0000	1.24
  +++ MetadataManager.java	11 Mar 2005 18:01:05 -0000	1.25
  @@ -149,6 +149,7 @@
       private boolean enablePerThreadChanges;
       private PBKey defaultPBKey;
       private Map persistenceConfigurationDescriptorMap;
  +    private Object currentProfileKey;
   
       public MetadataManager(PersistentFieldFactory fieldFactory)
       {
  @@ -264,12 +265,28 @@
       }
   
       /**
  +     * Returns the last activated profile key.
  +     *
  +     * @return the last activated profile key or null if no profile has been loaded
  +     * @throws MetadataException if per-thread changes has not been activated
  +     * @see #loadProfile(Object)
  +     */
  +    public Object getCurrentProfileKey() throws MetadataException
  +    {
  +        if (!isEnablePerThreadChanges())
  +        {
  +            throw new MetadataException("Call to this method is undefined, since per-thread
mode is disabled.");
  +        }
  +        return currentProfileKey;
  +    }
  +
  +    /**
        * Initializes the metadata, i.e. parses the repository file. 
        */
       public void initialize()
       {
  -        metadataProfiles = new Hashtable();
  -
  +        metadataProfiles  = new Hashtable();
  +        currentProfileKey = null;
           try
           {
               globalRepository     = persistor.readDescriptorRepository(repositoryPath);
  @@ -629,6 +646,7 @@
               throw new MetadataException("Can not find profile for key '" + key + "'");
           }
           setDescriptor(rep);
  +        currentProfileKey = key;
       }
   
       /**
  @@ -645,6 +663,7 @@
       public void clearProfiles()
       {
           metadataProfiles.clear();
  +        currentProfileKey = null;
       }
   
       /**
  @@ -656,6 +675,7 @@
       public void removeAllProfiles()
       {
           metadataProfiles.clear();
  +        currentProfileKey = null;
       }
   
       /**
  @@ -708,4 +728,5 @@
                   " to enable this at runtime using 'setDefaultKey' method.");
           return null;
       }
  +
   }
  
  
  
  1.11      +1 -3      db-ojb/src/test/org/apache/ojb/broker/metadata/MetadataMultithreadedTest.java
  
  Index: MetadataMultithreadedTest.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/broker/metadata/MetadataMultithreadedTest.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- MetadataMultithreadedTest.java	11 Mar 2005 17:31:10 -0000	1.10
  +++ MetadataMultithreadedTest.java	11 Mar 2005 18:01:05 -0000	1.11
  @@ -105,11 +105,9 @@
               // supported
               Throwable expectedThrowable = null;
               try {
  -                System.err.println("------ The following exception is part of the tests...");
                   groupIter.next();
               } catch (Throwable t) {
                   expectedThrowable = t;
  -                System.err.println("------");
               }
               assertNotNull("Should get metadata exception from proxy", expectedThrowable);
               groupIter.releaseDbResources();
  
  
  

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