org.apache.torque.pool
Class ConnectionPool

java.lang.Object
  extended byorg.apache.torque.pool.ConnectionPool
All Implemented Interfaces:
javax.sql.ConnectionEventListener, java.util.EventListener

class ConnectionPool
extends java.lang.Object
implements javax.sql.ConnectionEventListener

This class implements a simple connection pooling scheme. Multiple pools are available through use of the PoolBrokerService.

Version:
$Id: ConnectionPool.java,v 1.14.2.2 2003/05/27 22:38:35 mpoeschl Exp $
Author:
Costas Stergiou, Frank Y. Kim, Brett McLaughlin, Greg Ritter, Daniel L. Rall, Paul O'Leary, Magnús Þór Torfason, Jon S. Stevens, John McNally

Nested Class Summary
protected  class ConnectionPool.Monitor
          This inner class monitors the PoolBrokerService.
 
Field Summary
private  long connectionWaitTimeout
          Amount of time a thread asking the pool for a cached connection will wait before timing out and throwing an error.
private  javax.sql.ConnectionPoolDataSource cpds
          The ConnectionPoolDataSource
private  int expiryTime
          The amount of time in milliseconds that a connection will be pooled.
private  int logInterval
          Interval (in seconds) that the monitor thread reports the pool state
private  java.io.PrintWriter logWriter
           
private  int maxConnections
          The maximum number of database connections that can be created.
private  ConnectionPool.Monitor monitor
          Monitor thread reporting the pool state
private  java.lang.String password
          The password for this pool.
private  java.util.Stack pool
          Pool containing database connections.
private  java.util.Map timeStamps
          Keep track of when connections were created.
private  int totalConnections
          The current number of database connections that have been created.
private  java.lang.String url
          The url for this pool.
private  java.lang.String username
          The user name for this pool.
private  int waitCount
          Counter that keeps track of the number of threads that are in the wait state, waiting to aquire a connection.
 
Constructor Summary
(package private) ConnectionPool(javax.sql.ConnectionPoolDataSource cpds, java.lang.String username, java.lang.String password, int maxConnections, int expiryTime, int connectionWaitTimeout, int logInterval, java.io.PrintWriter logWriter)
          Creates a ConnectionPool with the default attributes.
 
Method Summary
private  void closePooledConnection(javax.sql.PooledConnection pcon)
           
 void connectionClosed(javax.sql.ConnectionEvent event)
          This will be called if the Connection returned by the getConnection method came from a PooledConnection, and the user calls the close() method of this connection object.
 void connectionErrorOccurred(javax.sql.ConnectionEvent event)
          If a fatal error occurs, close the underlying physical connection so as not to be returned in the future
(package private)  void decrementConnections()
          Decreases the count of connections in the pool and also calls notify().
protected  void finalize()
          Close any open connections when this object is garbage collected.
(package private)  javax.sql.PooledConnection getConnection(java.lang.String username, java.lang.String password)
          Returns a connection that maintains a link to the pool it came from.
private  javax.sql.PooledConnection getInternalPooledConnection()
          Gets a pooled database connection.
(package private)  int getNbrAvailable()
          Returns the available connections in the pool
(package private)  int getNbrCheckedOut()
          Returns the checked out connections in the pool
private  javax.sql.PooledConnection getNewConnection()
          Returns a fresh connection to the database.
(package private)  java.lang.String getPoolName()
          Get the name of the pool
(package private)  int getTotalCount()
          Returns the Total connections in the pool
private  boolean isExpired(javax.sql.PooledConnection connection)
          Helper method which determines whether a connection has expired.
private  boolean isValid(javax.sql.PooledConnection connection)
          Determines if a connection is still valid.
private  void log(java.lang.String s)
           
private  javax.sql.PooledConnection popConnection()
          Helper function that attempts to pop a connection off the pool's stack, handling the case where the popped connection has become invalid by creating a new connection.
private  void releaseConnection(javax.sql.PooledConnection pcon)
          This method returns a connection to the pool, and must be called by the requestor when finished with the connection.
(package private)  void shutdown()
          Close all connections to the database,
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

pool

private java.util.Stack pool
Pool containing database connections.


url

private java.lang.String url
The url for this pool.


username

private java.lang.String username
The user name for this pool.


password

private java.lang.String password
The password for this pool.


totalConnections

private int totalConnections
The current number of database connections that have been created.


maxConnections

private int maxConnections
The maximum number of database connections that can be created.


expiryTime

private int expiryTime
The amount of time in milliseconds that a connection will be pooled.


waitCount

private int waitCount
Counter that keeps track of the number of threads that are in the wait state, waiting to aquire a connection.


logInterval

private int logInterval
Interval (in seconds) that the monitor thread reports the pool state


monitor

private ConnectionPool.Monitor monitor
Monitor thread reporting the pool state


connectionWaitTimeout

private long connectionWaitTimeout
Amount of time a thread asking the pool for a cached connection will wait before timing out and throwing an error.


cpds

private javax.sql.ConnectionPoolDataSource cpds
The ConnectionPoolDataSource


timeStamps

private java.util.Map timeStamps
Keep track of when connections were created. Keyed by a PooledConnection and value is a java.util.Date


logWriter

private java.io.PrintWriter logWriter
Constructor Detail

ConnectionPool

ConnectionPool(javax.sql.ConnectionPoolDataSource cpds,
               java.lang.String username,
               java.lang.String password,
               int maxConnections,
               int expiryTime,
               int connectionWaitTimeout,
               int logInterval,
               java.io.PrintWriter logWriter)
Creates a ConnectionPool with the default attributes.

Parameters:
cpds - The datasource
username - The user name for this pool.
password - The password for this pool.
maxConnections - max number of connections
expiryTime - connection expiry time
connectionWaitTimeout - timeout
logInterval - log interval
logWriter - the log
Method Detail

getConnection

final javax.sql.PooledConnection getConnection(java.lang.String username,
                                               java.lang.String password)
                                        throws java.sql.SQLException
Returns a connection that maintains a link to the pool it came from.

Parameters:
username - The name of the database user.
password - The password of the database user.
Returns:
A database connection.
Throws:
java.sql.SQLException - if there is aproblem with the db connection

getNewConnection

private javax.sql.PooledConnection getNewConnection()
                                             throws java.sql.SQLException
Returns a fresh connection to the database. The database type is specified by driver, and its connection information by url, username, and password.

Returns:
A database connection.
Throws:
java.sql.SQLException - if there is aproblem with the db connection

getInternalPooledConnection

private javax.sql.PooledConnection getInternalPooledConnection()
                                                        throws ConnectionWaitTimeoutException,
                                                               java.lang.Exception
Gets a pooled database connection.

Returns:
A database connection.
Throws:
ConnectionWaitTimeoutException - Wait time exceeded.
java.lang.Exception - No pooled connections.

popConnection

private javax.sql.PooledConnection popConnection()
                                          throws java.lang.Exception
Helper function that attempts to pop a connection off the pool's stack, handling the case where the popped connection has become invalid by creating a new connection.

Returns:
An existing or new database connection.
Throws:
java.lang.Exception - if the pool is empty

isExpired

private boolean isExpired(javax.sql.PooledConnection connection)
Helper method which determines whether a connection has expired.

Parameters:
connection - The connection to test.
Returns:
True if the connection is expired, false otherwise.

isValid

private boolean isValid(javax.sql.PooledConnection connection)
Determines if a connection is still valid.

Parameters:
connection - The connection to test.
Returns:
True if the connection is valid, false otherwise.

finalize

protected void finalize()
                 throws java.lang.Throwable
Close any open connections when this object is garbage collected.

Throws:
java.lang.Throwable - Anything might happen...

shutdown

void shutdown()
Close all connections to the database,


getTotalCount

int getTotalCount()
Returns the Total connections in the pool

Returns:
total connections in the pool

getNbrAvailable

int getNbrAvailable()
Returns the available connections in the pool

Returns:
number of available connections in the pool

getNbrCheckedOut

int getNbrCheckedOut()
Returns the checked out connections in the pool

Returns:
number of checked out connections in the pool

decrementConnections

void decrementConnections()
Decreases the count of connections in the pool and also calls notify().


getPoolName

java.lang.String getPoolName()
Get the name of the pool

Returns:
the name of the pool

connectionClosed

public void connectionClosed(javax.sql.ConnectionEvent event)
This will be called if the Connection returned by the getConnection method came from a PooledConnection, and the user calls the close() method of this connection object. What we need to do here is to release this PooledConnection from our pool...

Specified by:
connectionClosed in interface javax.sql.ConnectionEventListener
Parameters:
event - the connection event

connectionErrorOccurred

public void connectionErrorOccurred(javax.sql.ConnectionEvent event)
If a fatal error occurs, close the underlying physical connection so as not to be returned in the future

Specified by:
connectionErrorOccurred in interface javax.sql.ConnectionEventListener
Parameters:
event - the connection event

releaseConnection

private void releaseConnection(javax.sql.PooledConnection pcon)
This method returns a connection to the pool, and must be called by the requestor when finished with the connection.

Parameters:
pcon - The database connection to release.

closePooledConnection

private void closePooledConnection(javax.sql.PooledConnection pcon)
Parameters:
pcon - The database connection to close.

log

private void log(java.lang.String s)


Copyright © 1999-2003 Apache Software Foundation. All Rights Reserved.