org.apache.turbine.util.db.pool
Class ConnectionPool

java.lang.Object
  |
  +--org.apache.turbine.util.db.pool.ConnectionPool

public class ConnectionPool
extends java.lang.Object

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

Version:
$Id: ConnectionPool.java,v 1.1.1.1 2001/08/16 05:09:54 jvanzyl Exp $
Author:
Costas Stergiou, Frank Y. Kim, Brett McLaughlin, Greg Ritter, Daniel L. Rall, Paul O'Leary, Magnús Þór Torfason, Jon S. Stevens

Field Summary
private  long connectionAttemptsCounter
          The number of times that an attempt to obtain a pooled connection has been made.
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 if the driver is JDBC 2.0 compliant
private  DB db
          The class containing the database specific info for connections in this pool (i.e.
private  long dbCheckFrequency
          Thread sleep time between checks for database connectivity problems.
private  java.lang.String driver
          The driver type for this pool.
private  long expiryTime
          The amount of time in milliseconds that a connection will be pooled.
private  long maxConnectionAttempts
          The number of times to attempt to obtain a pooled connection before giving up.
private  int maxConnections
          The maximum number of database connections that can be created.
private  java.lang.String password
          The password for this pool.
private  java.util.Stack pool
          Pool containing database connections.
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.
 
Constructor Summary
ConnectionPool()
          Deprecated. Use the constructor specifying db parameters.
ConnectionPool(int maxCons, long expiryTime)
          Deprecated. Use the constructor specifying db parameters.
ConnectionPool(java.lang.String driver, java.lang.String url, java.lang.String username, java.lang.String password)
          Creates a ConnectionPool with the default attributes.
ConnectionPool(java.lang.String driver, java.lang.String url, java.lang.String username, java.lang.String password, int maxCons, long expiryTime)
          Creates a ConnectionPool with the specified attributes.
 
Method Summary
 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.
 DBConnection getConnection()
          Returns a connection that maintains a link to the pool it came from.
 DBConnection getConnection(java.lang.String driver, java.lang.String url, java.lang.String username, java.lang.String password)
          Deprecated. Database parameters should not be specified each time a DBConnection is fetched from the pool.
 DB getDB()
          Returns an instance of the database adapter associated with this pool.
private  DBConnection getInternalPooledConnection()
          Gets a pooled database connection.
 int getLoginTimeout()
           
 java.io.PrintWriter getLogWriter()
          The log writer is a character output stream to which all logging and tracing messages for this data source object instance will be printed.
 int getNbrAvailable()
          Returns the available connections in the pool
 int getNbrCheckedOut()
          Returns the checked out connections in the pool
protected  DBConnection getNewConnection()
          Returns a fresh connection to the database.
 javax.sql.PooledConnection getPooledConnection()
          Attempt to establish a database connection.
 javax.sql.PooledConnection getPooledConnection(java.lang.String user, java.lang.String password)
          Attempt to establish a database connection.
 int getTotalCount()
          Re turns the Total connections in the pool
private  boolean isExpired(DBConnection connection)
          Helper method which determines whether a connection has expired.
private  boolean isValid(DBConnection connection)
          Determines if a connection is still valid.
private  DBConnection 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.
 void releaseConnection(DBConnection dbconn)
          This method returns a connection to the pool, and must be called by the requestor when finished with the connection.
 void setLoginTimeout(int seconds)
          Sets the maximum time in seconds that this data source will wait while attempting to connect to a database.
 void setLogWriter(java.io.PrintWriter out)
          The log writer is a character output stream to which all logging and tracing messages for this data source object instance will be printed.
 void shutdown()
          Close all connections to the database,
 
Methods inherited from class java.lang.Object
, clone, equals, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

pool

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

driver

private java.lang.String driver
The driver type for this pool.

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 long expiryTime
The amount of time in milliseconds that a connection will be pooled.

maxConnectionAttempts

private long maxConnectionAttempts
The number of times to attempt to obtain a pooled connection before giving up.

connectionAttemptsCounter

private long connectionAttemptsCounter
The number of times that an attempt to obtain a pooled connection has been made.

dbCheckFrequency

private long dbCheckFrequency
Thread sleep time between checks for database connectivity problems.

db

private DB db
The class containing the database specific info for connections in this pool (i.e. the Turbine database adapter).

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 if the driver is JDBC 2.0 compliant
Constructor Detail

ConnectionPool

public ConnectionPool()
Deprecated. Use the constructor specifying db parameters.

Creates a ConnectionPool with the default attributes.

ConnectionPool

public ConnectionPool(int maxCons,
                      long expiryTime)
Deprecated. Use the constructor specifying db parameters.

Creates a ConnectionPool with the specified attributes.
Parameters:
maxCons - The maximum number of connections for this pool.
expiryTime - The expiration time in milliseconds.

ConnectionPool

public ConnectionPool(java.lang.String driver,
                      java.lang.String url,
                      java.lang.String username,
                      java.lang.String password)
Creates a ConnectionPool with the default attributes.
Parameters:
driver - The driver type for this pool.
url - The url for this pool.
usernam - The user name for this pool.
password - The password for this pool.

ConnectionPool

public ConnectionPool(java.lang.String driver,
                      java.lang.String url,
                      java.lang.String username,
                      java.lang.String password,
                      int maxCons,
                      long expiryTime)
Creates a ConnectionPool with the specified attributes.
Parameters:
driver - The driver type for this pool.
url - The url for this pool.
usernam - The user name for this pool.
password - The password for this pool.
maxCons - The maximum number of connections for this pool.
expiryTime - The expiration time in milliseconds.
Method Detail

finalize

protected void finalize()
                 throws java.lang.Throwable
Close any open connections when this object is garbage collected.
Overrides:
finalize in class java.lang.Object
Throws:
java.lang.Throwable - Anything might happen...

getPooledConnection

public javax.sql.PooledConnection getPooledConnection()
                                               throws java.sql.SQLException
Attempt to establish a database connection.

getPooledConnection

public javax.sql.PooledConnection getPooledConnection(java.lang.String user,
                                                      java.lang.String password)
                                               throws java.sql.SQLException
Attempt to establish a database connection.

getConnection

public final DBConnection getConnection()
                                 throws java.lang.Exception
Returns a connection that maintains a link to the pool it came from.
Parameters:
driver - The fully-qualified name of the JDBC driver to use.
url - The URL of the database from which the connection is desired.
username - The name of the database user.
password - The password of the database user.
Returns:
A database connection.
Throws:
java.lang.Exception -  

getConnection

public final DBConnection getConnection(java.lang.String driver,
                                        java.lang.String url,
                                        java.lang.String username,
                                        java.lang.String password)
                                 throws java.lang.Exception
Deprecated. Database parameters should not be specified each time a DBConnection is fetched from the pool.

This function is unsafe to use, since the user passed parameters that he has no way to know if are used.
Parameters:
driver - The fully-qualified name of the JDBC driver to use.
url - The URL of the database from which the connection is desired.
username - The name of the database user.
password - The password of the database user.
Returns:
A database connection.
Throws:
Exception. -  

getDB

public DB getDB()
         throws java.lang.Exception
Returns an instance of the database adapter associated with this pool.
Returns:
The DB associated with this pool.

getLogWriter

public java.io.PrintWriter getLogWriter()
                                 throws java.sql.SQLException
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

setLogWriter

public void setLogWriter(java.io.PrintWriter out)
                  throws java.sql.SQLException
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

setLoginTimeout

public void setLoginTimeout(int seconds)
                     throws java.sql.SQLException
Sets the maximum time in seconds that this data source will wait while attempting to connect to a database. A value of zero specifies that the timeout is the default system timeout if there is one; otherwise it specifies that there is no timeout. When a data source object is created the login timeout is initially zero.

getLoginTimeout

public int getLoginTimeout()
                    throws java.sql.SQLException

getNewConnection

protected DBConnection getNewConnection()
                                 throws java.lang.Exception
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.lang.Exception -  

popConnection

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

getInternalPooledConnection

private DBConnection 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.

isExpired

private boolean isExpired(DBConnection connection)
                   throws java.lang.Exception
Helper method which determines whether a connection has expired.
Parameters:
connection - The connection to test.
Returns:
True if the connection is expired, false otherwise.
Throws:
java.lang.Exception -  

isValid

private boolean isValid(DBConnection connection)
                 throws java.lang.Exception
Determines if a connection is still valid.
Parameters:
connection - The connection to test.
Returns:
True if the connection is valid, false otherwise.
Throws:
java.lang.Exception -  

releaseConnection

public void releaseConnection(DBConnection dbconn)
                       throws java.lang.Exception
This method returns a connection to the pool, and must be called by the requestor when finished with the connection.
Parameters:
connection - The database connection to release.
Throws:
java.lang.Exception - Trouble releasing the connection.

shutdown

public void shutdown()
Close all connections to the database,

getTotalCount

public int getTotalCount()
Re turns the Total connections in the pool

getNbrAvailable

public int getNbrAvailable()
Returns the available connections in the pool

getNbrCheckedOut

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

decrementConnections

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


Copyright © 2000-2002 Apache Software Foundation. All Rights Reserved.