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 CollectionProxyDefaultImpl.java
Date Tue, 16 Nov 2004 17:44:45 GMT
arminw      2004/11/16 09:44:45

  Modified:    src/java/org/apache/ojb/broker/accesslayer Tag:
                        OJB_1_0_RELEASE ConnectionManagerImpl.java
               src/java/org/apache/ojb/broker/core/proxy Tag:
                        OJB_1_0_RELEASE CollectionProxyDefaultImpl.java
  Log:
  - add check for closed connections
  - more strict handling of PB instance
  see http://nagoya.apache.org/eyebrowse/ReadMsg?listName=ojb-user@db.apache.org&msgNo=15009
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.17.2.1  +26 -15    db-ojb/src/java/org/apache/ojb/broker/accesslayer/ConnectionManagerImpl.java
  
  Index: ConnectionManagerImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/accesslayer/ConnectionManagerImpl.java,v
  retrieving revision 1.17
  retrieving revision 1.17.2.1
  diff -u -r1.17 -r1.17.2.1
  --- ConnectionManagerImpl.java	19 Jun 2004 22:58:53 -0000	1.17
  +++ ConnectionManagerImpl.java	16 Nov 2004 17:44:44 -0000	1.17.2.1
  @@ -15,13 +15,16 @@
    * limitations under the License.
    */
   
  +import java.sql.Connection;
  +import java.sql.SQLException;
  +
  +import org.apache.ojb.broker.OJBRuntimeException;
   import org.apache.ojb.broker.PBKey;
   import org.apache.ojb.broker.PersistenceBroker;
   import org.apache.ojb.broker.PersistenceBrokerException;
   import org.apache.ojb.broker.TransactionAbortedException;
   import org.apache.ojb.broker.TransactionInProgressException;
   import org.apache.ojb.broker.TransactionNotInProgressException;
  -import org.apache.ojb.broker.OJBRuntimeException;
   import org.apache.ojb.broker.metadata.JdbcConnectionDescriptor;
   import org.apache.ojb.broker.metadata.MetadataManager;
   import org.apache.ojb.broker.platforms.Platform;
  @@ -30,9 +33,6 @@
   import org.apache.ojb.broker.util.logging.Logger;
   import org.apache.ojb.broker.util.logging.LoggerFactory;
   
  -import java.sql.Connection;
  -import java.sql.SQLException;
  -
   /**
    * Manages Connection ressources.
    *
  @@ -86,20 +86,32 @@
       }
   
       /**
  -     * Returns the underlying connection, requested at the
  +     * Returns the underlying connection, requested from
        * {@link org.apache.ojb.broker.accesslayer.ConnectionFactory}.
  -     * PB.beginTransaction() opens a single jdbc connection via
  -	 * serviceConnectionManager().localBegin().
  -	 * If you call ask serviceConnectionManager().getConnection() later
  +     * <p>
  +     * PB#beginTransaction() opens a single jdbc connection via
  +	 * PB#serviceConnectionManager().localBegin().
  +	 * If you call PB#serviceConnectionManager().getConnection() later
   	 * it returns the already opened connection.
  -	 * the PB instance will close the connection during
  -	 * commitTransaction() or abortTransaction().
  +	 * The PB instance will release the used connection during
  +	 * PB#commitTransaction() or PB#abortTransaction() or PB#close().
  +     * </p>
  +     * <p>
  +     * NOTE: Never call Connection.close() on the connection requested from the ConnectionManager.
  +     * Cleanup of used connection is done by OJB itself. If you need to release a used
connection
  +     * call {@link #releaseConnection()}.
  +     * </p>
        */
       public Connection getConnection() throws LookupException
       {
  -        // if (con == null || !isAlive(con))
  -        // isAlive check, how do we react if false and we in tx?
  -        // create a new connection isn't the right way I think.
  +        /*
  +        if the connection is not null and we are not in a local tx, we check
  +        the connection state and release "dead" connections.
  +        */
  +        if(con != null && !isInLocalTransaction() && !isAlive(con))
  +        {
  +            releaseConnection();
  +        }
           if (con == null)
           {
               con = this.connectionFactory.lookupConnection(jcd);
  @@ -287,7 +299,6 @@
       {
           if (this.con == null)
           {
  -            //if (log.isDebugEnabled()) log.debug("No connection to release");
               return;
           }
           if(isInLocalTransaction())
  
  
  
  No                   revision
  No                   revision
  1.7.2.2   +18 -22    db-ojb/src/java/org/apache/ojb/broker/core/proxy/CollectionProxyDefaultImpl.java
  
  Index: CollectionProxyDefaultImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/core/proxy/CollectionProxyDefaultImpl.java,v
  retrieving revision 1.7.2.1
  retrieving revision 1.7.2.2
  diff -u -r1.7.2.1 -r1.7.2.2
  --- CollectionProxyDefaultImpl.java	26 Jul 2004 15:49:52 -0000	1.7.2.1
  +++ CollectionProxyDefaultImpl.java	16 Nov 2004 17:44:45 -0000	1.7.2.2
  @@ -40,8 +40,6 @@
    */
   public class CollectionProxyDefaultImpl implements Collection, ManageableCollection, CollectionProxy
   {
  -    /** Reference to the used PersistenceBroker */
  -    private transient PersistenceBroker _broker;
       /** The key for acquiring the above broker */
       private PBKey _brokerKey;
       /** The query that defines the values in the collection */
  @@ -107,9 +105,10 @@
        */
       protected synchronized int loadSize() throws PersistenceBrokerException
       {
  +        PersistenceBroker broker = getBroker();
           try
           {
  -            return getBroker().getCount(getQuery());
  +            return broker.getCount(getQuery());
           }
           catch (Exception ex)
           {
  @@ -117,7 +116,7 @@
           }
           finally
           {
  -            releaseBroker();
  +            releaseBroker(broker);
           }
       }
   
  @@ -139,6 +138,7 @@
        */
       protected Collection loadData() throws PersistenceBrokerException
       {
  +        PersistenceBroker broker = getBroker();
           try
           {
               Collection result;
  @@ -149,7 +149,9 @@
               }
               else if (_size != 0)
               {
  -                result = (Collection)getBroker().getCollectionByQuery(getCollectionClass(),
getQuery());
  +                // TODO: returned ManageableCollection should extend Collection to avoid
  +                // this cast
  +                result = (Collection) broker.getCollectionByQuery(getCollectionClass(),
getQuery());
               }
               else
               {
  @@ -163,7 +165,7 @@
           }
           finally
           {
  -            releaseBroker();
  +            releaseBroker(broker);
           }
       }
   
  @@ -365,7 +367,7 @@
       /**
        * Release the broker instance.
        */
  -    protected synchronized void releaseBroker()
  +    protected synchronized void releaseBroker(PersistenceBroker broker)
       {
           /*
           arminw:
  @@ -373,11 +375,10 @@
           it from the PBF, do nothing if we obtain it from
           PBThreadMapping
           */
  -        if (_broker != null && _needsClose)
  +        if (broker != null && _needsClose)
           {
               _needsClose = false;
  -            _broker.close();
  -            _broker = null;
  +            broker.close();
           }
       }
   
  @@ -389,7 +390,6 @@
       protected synchronized PersistenceBroker getBroker() throws PBFactoryException
       {
           PersistenceBroker broker;
  -
           if (getBrokerKey() == null)
           {
               /*
  @@ -402,16 +402,13 @@
           }
           // first try to use the current threaded broker to avoid blocking
           broker = PersistenceBrokerThreadMapping.currentPersistenceBroker(getBrokerKey());
  -        // current broker not found, create a intern new one
  -        if (broker == null)
  +        // current broker not found or was closed, create a intern new one
  +        if (broker == null || broker.isClosed())
           {
  -            if (_broker == null)
  -            {
  -                _broker = PersistenceBrokerFactory.createPersistenceBroker(getBrokerKey());
  -                // TODO: Better way?
  -                _needsClose = true;
  -                broker      = _broker;
  -            }
  +            broker = PersistenceBrokerFactory.createPersistenceBroker(getBrokerKey());
  +            // signal that we use a new internal obtained PB instance to read the
  +            // data and that this instance have to be closed after use
  +            _needsClose = true;
           }
           return broker;
       }
  @@ -429,7 +426,6 @@
               setData(loadData());
               afterLoading();
           }
  -
           return _data;
       }
   
  
  
  

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