You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by jm...@apache.org on 2002/05/20 02:07:54 UTC

cvs commit: jakarta-commons-sandbox/jdbc2pool/src/java/org/apache/commons/jdbc2pool ConnectionPool.java

jmcnally    02/05/19 17:07:54

  Modified:    jdbc2pool/src/java/org/apache/commons/jdbc2pool
                        ConnectionPool.java
  Log:
  added a counter to make sure a thread that is awakened due to a connection
  being made available will not have its connection stolen before it is able
  to claim it.
  
  This patch was originally given by Byron Foster around the time this pool was
  being separated from torque.
  
  Revision  Changes    Path
  1.5       +17 -19    jakarta-commons-sandbox/jdbc2pool/src/java/org/apache/commons/jdbc2pool/ConnectionPool.java
  
  Index: ConnectionPool.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jdbc2pool/src/java/org/apache/commons/jdbc2pool/ConnectionPool.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- ConnectionPool.java	19 May 2002 23:22:54 -0000	1.4
  +++ ConnectionPool.java	20 May 2002 00:07:54 -0000	1.5
  @@ -80,7 +80,7 @@
    * @author <a href="mailto:magnus@handtolvur.is">Magn�s ��r Torfason</a>
    * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
    * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
  - * @version $Id: ConnectionPool.java,v 1.4 2002/05/19 23:22:54 jmcnally Exp $
  + * @version $Id: ConnectionPool.java,v 1.5 2002/05/20 00:07:54 jmcnally Exp $
    */
   class ConnectionPool
       implements ConnectionEventListener
  @@ -128,12 +128,11 @@
        */
       private int expiryTime; // 1 hour
   
  -    /**
  -     * Thread sleep time between checks for database connectivity
  -     * problems.
  +     /**
  +     * Counter that keeps track of the number of threads that are in
  +     * the wait state, waiting to aquire a connection.
        */
  -    //private int dbCheckFrequency;
  -
  +    private int waitCount = 0;
   
       int logInterval;
       Monitor monitor;
  @@ -295,21 +294,26 @@
       private synchronized PooledConnection getInternalPooledConnection()
           throws ConnectionWaitTimeoutException, Exception
       {
  -        PooledConnection pcon = null;
  -
  -        if ( pool.empty() )
  +        // We test waitCount > 0 to make sure no other threads are
  +        // waiting for a connection.
  +        if ( waitCount > 0 || pool.empty() )
           {
               // The connection pool is empty and we cannot allocate any new
               // connections.  Wait the prescribed amount of time and see if
               // a connection is returned.
               try
               {
  +                waitCount++;
                   wait( connectionWaitTimeout );
               }
               catch (InterruptedException ignored)
               {
                   // Don't care how we come out of the wait state.
               }
  +            finally
  +            {
  +                waitCount--;
  +            }
   
               // Check for a returned connection.
               if ( pool.empty() )
  @@ -318,14 +322,8 @@
                   // someone returning a connection.
                   throw new ConnectionWaitTimeoutException(url);
               }
  -            pcon = popConnection();
           }
  -        else
  -        {
  -            pcon = popConnection();
  -        }
  -
  -        return pcon;
  +        return popConnection();
       }
       /**
        * Helper function that attempts to pop a connection off the pool's stack,
  @@ -561,9 +559,10 @@
               pcon.close();
               timeStamps.remove(pcon);
           }
  -        catch (Exception ignored)
  +        catch (Exception e)
           {
  -            // ignored, should log this !FIXME!
  +            log("[ERROR] Error occurred trying to close a PooledConnection." +
  +                e.getMessage());
           }
           finally
           {
  @@ -621,7 +620,6 @@
               }
               catch (InterruptedException ignored)
               {
  -                // Don't care.
               }
           }
   
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>