com.softtech.jdbc
Class ConnectionPool

java.lang.Object
  extended by com.softtech.jdbc.ConnectionPool

public class ConnectionPool
extends java.lang.Object

Manages jdbc connections to the database.

Author:
Jeff S Smith

Nested Class Summary
private  class ConnectionPool.PooledConnection
          Pooled connection object
 
Field Summary
(package private)  java.util.ArrayList conPool
          Connection pool
private  java.lang.String conURL
          database connection URL
private  int dbType
          DatabaseType (e.g.
private  java.lang.String driverName
          database driver name
private  java.lang.String password
          database connection password
private  java.lang.String username
          database connection user name
 
Constructor Summary
ConnectionPool(java.sql.Connection con)
          Constructor uses the given connection (con) as its connection.
ConnectionPool(int numPooledCon, java.lang.String driverName, java.lang.String conURL, java.lang.String username, java.lang.String password)
          Constructor creates a JDBC connection using given parameters
 
Method Summary
private  void addConnectionsToPool(int numPooledCon)
          Creates database connection(s) and adds them to the pool.
 void closeAllConnections()
          Closes all connections in the connection pool.
(package private)  void closeConnection(java.sql.Connection con, int dbType)
          method closes the given connection
 java.sql.Connection getConnection()
          Gets available connection from the pool
 int getDbType()
          Get the dbType
 int getNumConInPool()
          Gets the number of connections in the pool
(package private)  void releaseConnection(java.sql.Connection con)
          Method closeConnection closes the jdbc database connection
 void releaseConnection(java.sql.Connection con, int dbType)
          Makes a connection available for reuse (in the connection pool)
private  void removeAnyClosedConnections()
           
 int resizeConnectionPool(int newSize)
          Attempts to resize the connection pool to the new size.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

driverName

private java.lang.String driverName
database driver name


conURL

private java.lang.String conURL
database connection URL


username

private java.lang.String username
database connection user name


password

private java.lang.String password
database connection password


dbType

private int dbType
DatabaseType (e.g. DatabaseType.ORACLE)


conPool

java.util.ArrayList conPool
Connection pool

Constructor Detail

ConnectionPool

public ConnectionPool(int numPooledCon,
                      java.lang.String driverName,
                      java.lang.String conURL,
                      java.lang.String username,
                      java.lang.String password)
Constructor creates a JDBC connection using given parameters

Parameters:
databaseType -
driverName -
conURL -
username -
password -

ConnectionPool

public ConnectionPool(java.sql.Connection con)
Constructor uses the given connection (con) as its connection. Use this constructor if you want to get your connections from some custom code or from a connection pool.

Parameters:
con -
dbType -
Method Detail

addConnectionsToPool

private void addConnectionsToPool(int numPooledCon)
Creates database connection(s) and adds them to the pool.

Parameters:
numPooledCon -
driverName -
conURL -
username -
password -

getNumConInPool

public int getNumConInPool()
Gets the number of connections in the pool

Returns:
int

removeAnyClosedConnections

private void removeAnyClosedConnections()

getConnection

public java.sql.Connection getConnection()
Gets available connection from the pool

Returns:
Connection

getDbType

public int getDbType()
Get the dbType

Returns:
int

releaseConnection

void releaseConnection(java.sql.Connection con)
Method closeConnection closes the jdbc database connection


closeAllConnections

public void closeAllConnections()
Closes all connections in the connection pool.


resizeConnectionPool

public int resizeConnectionPool(int newSize)
Attempts to resize the connection pool to the new size. This method will not free any connections which are not available (in use)--so it may not resize the pool. It will always enlarge the connection pool if newSize > current size.

Parameters:
newSize -
Returns:
int new size of connection pool

releaseConnection

public void releaseConnection(java.sql.Connection con,
                              int dbType)
Makes a connection available for reuse (in the connection pool)

Parameters:
con - Connection
dbType - int database type

closeConnection

void closeConnection(java.sql.Connection con,
                     int dbType)
method closes the given connection

Parameters:
con - connection to close
dbType - type of database (used to throw appropriate exception)