jmcnally    02/01/13 17:14:26

  Modified:    proposals/jdbc2pool/org/apache/torque/jdbc2pool Tag:
                        JDBC2POOL_BRANCH ConnectionPool.java
  Log:
  moved default initializations into the ctor.
  since this class is only accessed through TorqueDataSource the class and
  all methods have package access.
  removed unused code.
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.1.2.3   +101 -179  
jakarta-turbine-torque/proposals/jdbc2pool/org/apache/torque/jdbc2pool/ConnectionPool.java
  
  Index: ConnectionPool.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-turbine-torque/proposals/jdbc2pool/org/apache/torque/jdbc2pool/ConnectionPool.java,v
  retrieving revision 1.1.2.2
  retrieving revision 1.1.2.3
  diff -u -r1.1.2.2 -r1.1.2.3
  --- ConnectionPool.java       9 Jan 2002 08:03:31 -0000       1.1.2.2
  +++ ConnectionPool.java       14 Jan 2002 01:14:26 -0000      1.1.2.3
  @@ -82,88 +82,72 @@
    * @author <a href="mailto:[EMAIL PROTECTED]";>Paul O'Leary</a>
    * @author <a href="mailto:[EMAIL PROTECTED]";>Magn�s ��r Torfason</a>
    * @author <a href="mailto:[EMAIL PROTECTED]";>Jon S. Stevens</a>
  - * @version $Id: ConnectionPool.java,v 1.1.2.2 2002/01/09 08:03:31 jmcnally Exp $
  + * @version $Id: ConnectionPool.java,v 1.1.2.3 2002/01/14 01:14:26 jmcnally Exp $
    */
  -public class ConnectionPool
  +class ConnectionPool
       implements ConnectionEventListener
   {
       /**
        * Pool containing database connections.
        */
  -    private Stack pool = null;
  +    private Stack pool;
   
       /**
        * The driver type for this pool.
        */
  -    private String driver = null;
  +    private String driver;
   
       /**
        * The url for this pool.
        */
  -    private String url = null;
  +    private String url;
   
       /**
        * The user name for this pool.
        */
  -    private String username = null;
  +    private String username;
   
       /**
        * The password for this pool.
        */
  -    private String password = null;
  +    private String password;
   
       /**
        * The current number of database connections that have been
        * created.
        */
  -    private int totalConnections = 0;
  +    private int totalConnections;
   
       /**
        * The maximum number of database connections that can be
        * created.
        */
  -    private int maxConnections = 10;
  +    private int maxConnections;
   
       /**
        * The amount of time in milliseconds that a connection will be
        * pooled.
        */
  -    private long expiryTime = 3600000;   // 1 hour
  -
  -    /**
  -     * The number of times to attempt to obtain a pooled connection
  -     * before giving up.
  -     */
  -    private long maxConnectionAttempts = 50;
  -
  -    /**
  -     * The number of times that an attempt to obtain a pooled
  -     * connection has been made.
  -     */
  -    private long connectionAttemptsCounter = 0;
  -
  +    private int expiryTime; // 1 hour
   
       /**
        * Thread sleep time between checks for database connectivity
        * problems.
        */
  -    private long dbCheckFrequency = 5000;
  +    //private int dbCheckFrequency;
   
   
       int logInterval;
  -    String name;
       Monitor monitor;
   
       /**
        * Amount of time a thread asking the pool for a cached connection will
        * wait before timing out and throwing an error.
        */
  -    private long connectionWaitTimeout = 10 * 1000; // ten seconds
  +    private long connectionWaitTimeout;
   
  -    /**
  -     * The ConnectionPoolDataSource if the driver is JDBC 2.0 compliant
  -     */
  -    private ConnectionPoolDataSource cpds = null;
  +    /** The ConnectionPoolDataSource  */
  +    private ConnectionPoolDataSource cpds;
   
       /**
        * Keep track of when connections were created.  Keyed by a 
  @@ -171,13 +155,8 @@
        */
       private Map timeStamps;
   
  -    /* *
  -     * Empty ctor.
  -     * /
  -    private ConnectionPool()
  -    {
  -    }
  -    */
  +    private PrintWriter logWriter;
  +    private Category category;
   
       /**
        * Creates a <code>ConnectionPool</code> with the default
  @@ -185,49 +164,49 @@
        *
        * @param driver   The driver type for this pool.
        * @param url      The url for this pool.
  -     * @param usernam  The user name for this pool.
  +     * @param username  The user name for this pool.
        * @param password The password for this pool.
        */
  -    public ConnectionPool(ConnectionPoolDataSource cpds,
  -                          String username,
  -                          String password)
  -    {
  -        this(cpds, username, password, 0, 0, 0, 0, 0, null);
  -    }
  -
  -
  -    /**
  -     * Creates a <code>ConnectionPool</code> with the default
  -     * attributes.
  -     *
  -     * @param driver   The driver type for this pool.
  -     * @param url      The url for this pool.
  -     * @param usernam  The user name for this pool.
  -     * @param password The password for this pool.
  -     */
  -    public ConnectionPool(ConnectionPoolDataSource cpds,
  -                          String username,
  -                          String password,
  -                          int maxConnections,
  -                          long expiryTime,
  -                          long maxConnectionAttempts,
  -                          long connectionWaitTimeout,
  -                          int logInterval,
  -                          String name)
  +    ConnectionPool(ConnectionPoolDataSource cpds, String username,
  +                   String password, int maxConnections, int expiryTime,
  +                   int connectionWaitTimeout, int logInterval,
  +                   PrintWriter logWriter)
       {
  +        totalConnections = 0;
           pool = new Stack();
           timeStamps = new HashMap();
           
           this.cpds = cpds;
           this.username = username;
           this.password = password;
  -        this.maxConnections = maxConnections;
  -        this.expiryTime = expiryTime;
  -        this.maxConnectionAttempts = maxConnectionAttempts;
  -        this.connectionWaitTimeout = connectionWaitTimeout;
  -        this.logInterval = logInterval;
  -        this.name = name;
  -
  +        if ( maxConnections > 0 ) 
  +        {
  +            this.maxConnections = maxConnections;
  +        }
  +        else 
  +        {
  +            this.maxConnections = 1;            
  +        }
  +        if ( expiryTime > 0 ) 
  +        {
  +            this.expiryTime = expiryTime * 1000;            
  +        }
  +        else 
  +        {
  +            this.expiryTime = 3600 * 1000; // one hour
  +        }
  +        if ( connectionWaitTimeout > 0 ) 
  +        {
  +            this.connectionWaitTimeout = connectionWaitTimeout * 1000;    
  +        }
  +        else
  +        {
  +            this.connectionWaitTimeout = 10 * 1000; // ten seconds
  +        }
  +        this.logInterval = logInterval * 1000;    
  +        this.logWriter = logWriter;        
  +        this.category = Category.exists(getClass().getName());
  + 
           // Create monitor thread
           monitor = new Monitor();
           // Indicate that this is a system thread. JVM will quit only when there
  @@ -236,7 +215,6 @@
           // to terminate in an orderly manner.
           monitor.setDaemon(true);
           monitor.start();
  -
       }
   
       /**
  @@ -251,7 +229,7 @@
        *
        * @exception Exception
        */
  -    public synchronized final PooledConnection 
  +    synchronized final PooledConnection 
           getConnection(String username, String password)
           throws SQLException
       {
  @@ -291,9 +269,22 @@
       private PooledConnection getNewConnection()
           throws SQLException
       {
  -        PooledConnection pc = cpds.getPooledConnection(username, password);
  +        PooledConnection pc = null;
  +        if ( username == null ) 
  +        {
  +            pc = cpds.getPooledConnection();
  +        }
  +        else 
  +        {
  +            pc = cpds.getPooledConnection(username, password);
  +        }
           pc.addConnectionEventListener(this);
  -        timeStamps.put(pc, new Date());
  +        // Age some connections so that there will not be a run on the db,
  +        // when connections start expiring
  +        long currentTime = System.currentTimeMillis();
  +        double ratio = (1.0 * totalConnections)/maxConnections;
  +        currentTime -= expiryTime*0.25*(1.0-ratio);
  +        timeStamps.put(pc, new Long(currentTime));
           totalConnections++;
           return pc;
       }
  @@ -312,8 +303,6 @@
   
           if ( pool.empty() )
           {
  -            connectionAttemptsCounter++;
  -
               // 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.
  @@ -361,7 +350,6 @@
               // valid even though it's checked before being pooled.
               if ( isValid(con) )
               {
  -                connectionAttemptsCounter = 0;
                   return con;
               }
               else
  @@ -369,7 +357,6 @@
                   // Close invalid connection.
                   con.close();
                   totalConnections--;
  -                connectionAttemptsCounter = 0;
   
                   // If the pool is now empty, create a new connection.  We're
                   // guaranteed not to exceed the connection limit since we
  @@ -391,51 +378,6 @@
   
   
       /**
  -     * The log writer is a character output stream to which all
  -     * logging and tracing messages for this data source object
  -     * instance will be printed. This includes messages printed by
  -     * the methods of this object, messages printed by methods of
  -     * other objects manufactured by this object, and so on.
  -     * Messages printed to a data source specific log writer are
  -     * not printed to the log writer associated with the
  -     * java.sql.Drivermanager class. When a data source object is
  -     * created the log writer is initially null, in other words,
  -     * logging is disabled.
  -     *
  -     * If using JDBC2.0 dispatch to the ConnectionPoolDataSource
  -     */
  -    public PrintWriter getLogWriter()
  -        throws SQLException
  -    {
  -        if ( cpds != null )
  -            return cpds.getLogWriter();
  -        else
  -            return null;
  -    }
  -
  -    /**
  -     * The log writer is a character output stream to which all
  -     * logging and tracing messages for this data source object
  -     * instance will be printed. This includes messages printed by
  -     * the methods of this object, messages printed by methods of
  -     * other objects manufactured by this object, and so on.
  -     * Messages printed to a data source specific log writer are
  -     * not printed to the log writer associated with the
  -     * java.sql.Drivermanager class. When a data source object is
  -     * created the log writer is initially null, in other words,
  -     * logging is disabled.
  -     *
  -     * If using JDBC2.0 dispatch to the ConnectionPoolDataSource
  -     */
  -    public void setLogWriter(PrintWriter out)
  -        throws SQLException
  -    {
  -        if ( cpds != null )
  -            cpds.setLogWriter(out);
  -    }
  -
  -
  -    /**
        * Helper method which determines whether a connection has expired.
        *
        * @param connection The connection to test.
  @@ -447,8 +389,8 @@
           // Test the age of the connection (defined as current time
           // minus connection birthday) against the connection pool
           // expiration time.
  -        return ((System.currentTimeMillis() -
  -                 ((Date)timeStamps.get(connection)).getTime()) > expiryTime);
  +        return ( expiryTime < (System.currentTimeMillis() -
  +                 ((Long)timeStamps.get(connection)).longValue()) );
       }
   
       /**
  @@ -491,7 +433,7 @@
       /**
        * Close all connections to the database,
        */
  -    public void shutdown()
  +    void shutdown()
       {
           if ( pool != null )
           {
  @@ -517,7 +459,7 @@
        * Re
        turns the Total connections in the pool
        */
  -    public int getTotalCount()
  +    int getTotalCount()
       {
           return totalConnections;
       }
  @@ -525,7 +467,7 @@
       /**
        * Returns the available connections in the pool
        */
  -    public int getNbrAvailable()
  +    int getNbrAvailable()
       {
           return pool.size();
       }
  @@ -533,7 +475,7 @@
       /**
        * Returns the checked out connections in the pool
        */
  -    public int getNbrCheckedOut()
  +    int getNbrCheckedOut()
       {
           return (totalConnections - pool.size());
       }
  @@ -542,19 +484,15 @@
        * Decreases the count of connections in the pool
        * and also calls <code>notify()</code>.
        */
  -    public void decrementConnections()
  +    void decrementConnections()
       {
           totalConnections--;
           notify();
       }
   
  -    public String getPoolName()
  +    String getPoolName()
       {
  -        if ( name == null ) 
  -        {
  -            name = "";
  -        }
  -        return name;
  +        return toString();
       }
   
       // ***********************************************************************
  @@ -569,7 +507,8 @@
        */
       public void connectionClosed(ConnectionEvent event) 
       {
  -            releaseConnection( (PooledConnection)event.getSource());
  +        System.out.println("connectionClosed event caught");
  +        releaseConnection( (PooledConnection)event.getSource());
       }
   
       /**
  @@ -586,7 +525,14 @@
               //interested in errors since we are about to close this connection
               ( (PooledConnection) event.getSource() )
                   .removeConnectionEventListener(this);
  +        }
  +        catch (Exception ignore) 
  +        {
  +            //just ignore
  +        }
   
  +        try 
  +        {
               closePooledConnection( (PooledConnection)event.getSource() );
           }
           catch (Exception ignore) 
  @@ -632,6 +578,22 @@
           }
       }
   
  +    private void log(String s)
  +    {
  +        if ( logWriter != null ) 
  +        {
  +            logWriter.println(s);
  +        }
  +        else if ( category != null ) 
  +        {
  +            category.info(s);
  +        }
  +        else 
  +        {
  +            System.out.println(s);
  +        }
  +    }
  +
       ///////////////////////////////////////////////////////////////////////////
   
       /**
  @@ -649,8 +611,6 @@
       protected class Monitor extends Thread
       {
           private boolean isRun = true;
  -        private Category category = 
  -            Category.getInstance(this.getClass().getName());
   
           public void run()
           {
  @@ -662,8 +622,7 @@
                      .append(getNbrAvailable()).append(" + ")
                      .append(getNbrCheckedOut()).append(" = ")
                      .append(getTotalCount());
  -                    
  -                category.info(buf.toString());
  +                log(buf.toString());
               }
   
               // Wait for a bit.
  @@ -682,42 +641,5 @@
               isRun = false;
           }
       }
  -
  -
  -    /*
  -      This is being left commented for now because it requires more
  -      thought that I'm not ready to give to it yet. -JSS
  -
  -    public void run()
  -    {
  -        while (true)
  -        {
  -            // Wait for a bit.
  -            try
  -            {
  -                Thread.sleep(dbCheckFrequency);
  -            }
  -            catch (InterruptedException e)
  -            {
  -            }
  -
  -            // Check for database connectivity problems.
  -            if ( dbconnAttemptsCounter >= maxConnectionAttempts )
  -            {
  -                // Dump all connections.
  -                try
  -                {
  -                    finalize();
  -                }
  -                catch (Throwable e)
  -                {
  -                }
  -
  -                connectionAttemptsCounter = 0;
  -                notify();
  -            }
  -        }
  -    }
  -    */
   }
   
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to