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/proxy AbstractIndirectionHandler.java
Date Wed, 28 Sep 2005 00:32:09 GMT
arminw      2005/09/27 17:32:09

  Modified:    src/java/org/apache/ojb/broker/core/proxy
                        AbstractIndirectionHandler.java
  Log:
  use inner class to handle temporary used PB instances to avoid concurrency issues
  
  Revision  Changes    Path
  1.3       +42 -26    db-ojb/src/java/org/apache/ojb/broker/core/proxy/AbstractIndirectionHandler.java
  
  Index: AbstractIndirectionHandler.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/core/proxy/AbstractIndirectionHandler.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- AbstractIndirectionHandler.java	27 Aug 2005 12:12:09 -0000	1.2
  +++ AbstractIndirectionHandler.java	28 Sep 2005 00:32:09 -0000	1.3
  @@ -22,8 +22,8 @@
   import org.apache.ojb.broker.OJBRuntimeException;
   import org.apache.ojb.broker.PBFactoryException;
   import org.apache.ojb.broker.PBKey;
  -import org.apache.ojb.broker.PersistenceBroker;
   import org.apache.ojb.broker.PersistenceBrokerException;
  +import org.apache.ojb.broker.PersistenceBrokerInternal;
   import org.apache.ojb.broker.PersistenceConfiguration;
   import org.apache.ojb.broker.core.PersistenceBrokerThreadMapping;
   import org.apache.ojb.broker.util.logging.LoggerFactory;
  @@ -46,8 +46,6 @@
       private Identity id = null;
       /** The materialization listeners */
       private transient ArrayList listeners;
  -    /** Specifies whether we obtained a fresh broker which we have to close after we used
it */
  -    private boolean needsClose;
   
       /**
        * Creates a new indirection handler for the indicated object.
  @@ -167,9 +165,10 @@
        * 
        * @return a PersistenceBroker
        */
  -    protected synchronized PersistenceBroker getBroker() throws PBFactoryException
  +    protected TemporaryBrokerWrapper getBroker() throws PBFactoryException
       {
  -        PersistenceBroker broker;
  +        PersistenceBrokerInternal broker;
  +        boolean needsClose = false;
   
           if (getBrokerKey() == null)
           {
  @@ -187,24 +186,10 @@
           if ((broker == null) || broker.isClosed())
           {
               broker = persistenceConf.createPersistenceBroker();
  -            // TODO: Better way?
  +            /** Specifies whether we obtained a fresh broker which we have to close after
we used it */
               needsClose = true;
           }
  -        return broker;
  -    }
  -
  -    /**
  -     * Release the PersistenceBroker instance.
  -     * 
  -     * @param broker The broker 
  -     */
  -    protected synchronized void releaseBroker(PersistenceBroker broker)
  -    {
  -        if (broker != null && needsClose)
  -        {
  -        	needsClose = false;
  -            broker.close();
  -        }
  +        return new TemporaryBrokerWrapper(broker, needsClose);
       }
   
       /**
  @@ -357,11 +342,10 @@
        */
       protected synchronized Object materializeSubject() throws PersistenceBrokerException
       {
  -        PersistenceBroker broker = getBroker();
  -
  +        TemporaryBrokerWrapper tmp = getBroker();
           try
           {
  -            Object realSubject = broker.getObjectByIdentity(id);
  +            Object realSubject = tmp.broker.getObjectByIdentity(id);
   
               if (realSubject == null)
               {
  @@ -375,7 +359,7 @@
           }
           finally
           {
  -			releaseBroker(broker);
  +			tmp.close();
           }
       }
   
  @@ -388,4 +372,36 @@
       {
           return realSubject != null;
       }
  +
  +
  +    //===================================================================
  +    // inner class
  +    //===================================================================
  +    /**
  +     * wrapper class for temporary used broker instances.
  +     */
  +    static final class TemporaryBrokerWrapper
  +    {
  +        /** Specifies whether we obtained a fresh broker which we have to close after we
used it */
  +        boolean needsClose;
  +        PersistenceBrokerInternal broker;
  +
  +        public TemporaryBrokerWrapper(PersistenceBrokerInternal broker, boolean needsClose)
  +        {
  +            this.broker = broker;
  +            this.needsClose = needsClose;
  +        }
  +
  +        /**
  +         * Cleanup the used broker instance, it's mandatory to call
  +         * this method after use.
  +         */
  +        public void close()
  +        {
  +            if(needsClose)
  +            {
  +                broker.close();
  +            }
  +        }
  +    }
   }
  
  
  

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