amber.server.manager.database
Class ConnectionPool

java.lang.Object
  |
  +--java.lang.Thread
        |
        +--amber.server.manager.database.ConnectionPool
All Implemented Interfaces:
java.lang.Runnable

public class ConnectionPool
extends java.lang.Thread

This class controls a cache of connection pools. It is instantiated by the Database Manager and coordinates connections to a database and the interaction to the database system.

Version:
1.0.0
Author:
Dr. David J. Knowles
See Also:
DatabaseManager, DatabaseConnection

Field Summary
protected  java.util.Vector connectionPool
          The connection pool
static long ConnectionWaitQuanta
          How many milliseconds to block before rechecking an event.
protected  java.lang.String databaseDriver
          The name of the database driver.
protected  java.lang.String databaseName
          The name of the database URL.
static java.lang.String header
           
protected  long inactivityDuration
          The amount of time the thread should sleep between refreshing connections.
protected  Log loggingFile
          Logging file.
protected  int maxConnections
          The maximum number of connections to ever establish.
protected  int minConnections
          The minimum number of connections to establish on start up.
protected  java.lang.String password
          The password to use when connecting to the database.
protected  java.lang.String userName
          The user name to use when connecting to the database.
static java.lang.String version
           
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
ConnectionPool(java.lang.String name, java.lang.String databaseDriver, java.lang.String databaseName, java.lang.String userName, java.lang.String password, int minConnections, int maxConnections, java.lang.String logFilename, int logLevel, long inactivityDuration)
          The constructor for the pool.
 
Method Summary
protected  DatabaseConnection createNewConnection(java.lang.String databaseName, java.lang.String userName, java.lang.String password)
          This function creates a new connection and adds it to the pool of available database objects.
 boolean equals(java.lang.Object other)
          This function determines if the input object is the same as this object.
 int getAvailableConnections()
          Return the number of available connections.
 java.sql.Connection getConnection()
          Gets a connection from the available connection pool.
 java.sql.Connection getConnection(long timeout)
          Gets a connection from the available connection pool.
 int getMaximumConnections()
          Return the maximum number of connections which it is possible to raise.
 int getMinimumConnections()
          Return the minimum number of connections raised on creation.
 java.lang.String getPoolName()
          This function returns the name of the ConnectionPool.
 int getTotalConnections()
          Return the total number of connections raised currently to the database.
 void init(java.lang.String logFilename, int logLevel)
          Main initialisation function for this database system.
protected  void initialiseDataBase(java.lang.String databaseDriver, java.lang.String databaseName, java.lang.String userName, java.lang.String password)
          This function opens the database given the specified characteristics.
protected  java.sql.Connection openConnection(java.lang.String databaseName, java.lang.String userName, java.lang.String password)
          This function opens a new connection to the database.
protected  void openConnections(java.lang.String databaseName, java.lang.String userName, java.lang.String password)
          This function opens the specified number of connections in the properties file for the manager.
protected  void refreshConnection(DatabaseConnection entry)
          This function refreshes an individual connection pool entry.
 void refreshConnectionPool()
          This function refreshes the connection pool and ensures that the connections are still valid.
 void releaseConnection(java.sql.Connection connection)
          Returns the connection back to the available connection pool.
 void run()
          The thread run method.
 void shutDown()
          Termination function.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

version

public static final java.lang.String version

header

public static final java.lang.String header

ConnectionWaitQuanta

public static final long ConnectionWaitQuanta
How many milliseconds to block before rechecking an event.

loggingFile

protected Log loggingFile
Logging file.

connectionPool

protected java.util.Vector connectionPool
The connection pool

databaseName

protected java.lang.String databaseName
The name of the database URL.

databaseDriver

protected java.lang.String databaseDriver
The name of the database driver.

userName

protected java.lang.String userName
The user name to use when connecting to the database.

password

protected java.lang.String password
The password to use when connecting to the database.

minConnections

protected int minConnections
The minimum number of connections to establish on start up.

maxConnections

protected int maxConnections
The maximum number of connections to ever establish.

inactivityDuration

protected long inactivityDuration
The amount of time the thread should sleep between refreshing connections.
Constructor Detail

ConnectionPool

public ConnectionPool(java.lang.String name,
                      java.lang.String databaseDriver,
                      java.lang.String databaseName,
                      java.lang.String userName,
                      java.lang.String password,
                      int minConnections,
                      int maxConnections,
                      java.lang.String logFilename,
                      int logLevel,
                      long inactivityDuration)
               throws DatabaseManagerException
The constructor for the pool.
Parameters:
name - String containing the name of the connection pool object.
databaseDriver - String name of the JDBC driver.
databaseName - String JDBC URL of the database.
userName - String name to use when authenticating this server.
password - String password to use when authenticating this server.
minConnections - int number of connections to start pool with.
maxConnections - int number of upper limit of connections to create.
logFilename - String containing the name of the log file.
logLevel - int containing the log level to log.
inactivityDuration - long time that the thread should sleep between refreshes.
Throws:
DatabaseManagerException - containing errors.
Method Detail

init

public void init(java.lang.String logFilename,
                 int logLevel)
          throws DatabaseManagerException
Main initialisation function for this database system.
Parameters:
logFilename - String containing the name of the log file.
logLevel - int containing the log level to log.
Throws:
DatabaseManagerException - containing errors.

initialiseDataBase

protected void initialiseDataBase(java.lang.String databaseDriver,
                                  java.lang.String databaseName,
                                  java.lang.String userName,
                                  java.lang.String password)
                           throws DatabaseManagerException
This function opens the database given the specified characteristics.
Parameters:
databaseDriver - String name of the JDBC driver.
databaseName - String JDBC URL of the database.
userName - String name to use when authenticating this server.
password - String password to use when authenticating this server.
Throws:
DatabaseManagerException - containing errors.

openConnections

protected void openConnections(java.lang.String databaseName,
                               java.lang.String userName,
                               java.lang.String password)
                        throws DatabaseManagerException
This function opens the specified number of connections in the properties file for the manager.
Parameters:
databaseName - String JDBC URL of the database.
userName - String name to use when authenticating this server.
password - String password to use when authenticating this server.
Throws:
DatabaseManagerException - containing errors.

createNewConnection

protected DatabaseConnection createNewConnection(java.lang.String databaseName,
                                                 java.lang.String userName,
                                                 java.lang.String password)
This function creates a new connection and adds it to the pool of available database objects.
Parameters:
databaseName - String JDBC URL of the database.
userName - String name to use when authenticating this server.
password - String password to use when authenticating this server.
Returns:
DatabaseConnection which was created.

openConnection

protected java.sql.Connection openConnection(java.lang.String databaseName,
                                             java.lang.String userName,
                                             java.lang.String password)
This function opens a new connection to the database.
Parameters:
databaseName - String JDBC URL of the database.
userName - String name to use when authenticating this server.
password - String password to use when authenticating this server.
Returns:
Connection which was created to the database.

getConnection

public java.sql.Connection getConnection(long timeout)
Gets a connection from the available connection pool. This function will check that the connection is live before handing it off to the client. This function waits until a connection becomes available or the timeout expires.
Parameters:
timeout - long time in milliseconds to wait for the connection to become available.
Returns:
Connection to the database or null if there are no available connections.

getConnection

public java.sql.Connection getConnection()
Gets a connection from the available connection pool. This function will check that the connection is live before handing it off to the client. This function will return instantly.
Returns:
Connection to the database or null if there are no available connections.

releaseConnection

public void releaseConnection(java.sql.Connection connection)
Returns the connection back to the available connection pool.
Parameters:
connection - Connection to return to the pool.

getTotalConnections

public int getTotalConnections()
Return the total number of connections raised currently to the database.
Returns:
int number of total connections.

getMinimumConnections

public int getMinimumConnections()
Return the minimum number of connections raised on creation.
Returns:
int number of minimum connections.

getMaximumConnections

public int getMaximumConnections()
Return the maximum number of connections which it is possible to raise.
Returns:
int number of maximum connections.

getAvailableConnections

public int getAvailableConnections()
Return the number of available connections.
Returns:
int number of available connections.

refreshConnectionPool

public void refreshConnectionPool()
This function refreshes the connection pool and ensures that the connections are still valid.

refreshConnection

protected void refreshConnection(DatabaseConnection entry)
This function refreshes an individual connection pool entry. It does this one of two ways: if the connection is still valid it pings the database connection. If the connection is closed then it reinitiates a new connection.
Parameters:
entry - DatabaseConnection to refresh.

run

public void run()
The thread run method. This is concerned with ensuring that the connections remain open.
Overrides:
run in class java.lang.Thread

shutDown

public void shutDown()
Termination function. When called it terminates the thread.

getPoolName

public java.lang.String getPoolName()
This function returns the name of the ConnectionPool.
Returns:
String containing the Connection pool name.

equals

public boolean equals(java.lang.Object other)
This function determines if the input object is the same as this object. It does this by name. For this reason ConnectionPools MUST NOT HAVE THE SAME NAME.
Overrides:
equals in class java.lang.Object
Parameters:
other - Object to match against.
Returns:
boolean state of the comparison.


Copyright © 2002 Clearfield Research Ltd. All Rights Reserved.