org.jwarp.service.connectionpool
Class ConnectionPool

org.jwarp.service.connectionpool.ConnectionPool

public class ConnectionPool

Connection pool

The connection pool pools connections to a database. When initializing an initial number of connections where opened. Each pool is generateed out of the configuration date (typically read in through a configuration file). The format for defining a database pool is:

Name of valueDescription
nameName of the connection pool
DBPOOL.UPDATETIMEThe update time in ms (for releasing too many free connections)
DBPOOL.name.URLJDBC url to connect to the database
DBPOOL.name.USERUser id for the database connection
DBPOOL.name.PASSWORDPassword for connecting to the database
DBPOOL.name.MAXCONNSMax number of connection this pool will create
DBPOOL.name.INITCONNSInitial number of connections made to database when pool is initialised
DBPool.name.MAXFREECONNSThe maximal number of free connection so some connections will be removed from the pool.
DBPOOL.name.RELEASECONNSDetermines how many connections will be released if too many free connections are available (see MAXFREECONNS setting)
DBPOOL.name.TIMEOUTTimeout in seconds for login to database
DBPOOL.name.INITSTATEMENTInitial SQL statement. This statement can be used to initialise the connection with some cutomized code, e.g. setting the default date format used within the application.
DBPOOL.name.LOGTARGETThe target log writer.

The connections given back by the pool methods are additionally wrapped within the ConnectionWrapper class. A call to the method connection.close() finally gives the connection back to the pool instead of closing it physically.

Version:
1.01
Author:
Anatole Tresch
See Also:
ConnectionWrapper

Constructor Summary
ConnectionPool(org.jdom.Element config)
          Creates a new pool named poolName.
ConnectionPool(java.lang.String name, java.lang.String URL, java.lang.String user, java.lang.String password, int maxConns, int initConns, int timeOut, java.lang.String logTarget)
          This creates a ConnectionPool.
ConnectionPool(java.lang.String name, java.lang.String url, java.lang.String user, java.lang.String password, int maxConns, int initConns, int timeOut, java.lang.String initStatement, java.lang.String logTarget)
          This creates a ConnectionPool.
 
Method Summary
 void checkFreeConnections()
          Checkes for too many free connections.
 int getBusyConnectionCount()
          Returns the current active connections count.
 java.sql.Connection getConnection()
          Returns a connection from the pool.
 java.sql.Connection getConnection(java.lang.String username, java.lang.String password)
          Attempt to establish a database connection.
 int getFreeConnectionCount()
          Returns the number of free connections
 int getLoginTimeout()
          Gets the maximum time in seconds that this data source can wait while attempting to connect to a database.
 java.io.PrintWriter getLogWriter()
          Get the log writer for this data source.
 int getMaxFreeConnections()
          Returns the number of free connections so some connections may be released.
 java.lang.String getName()
          Returns the name of the pool.
 int getReleaseSize()
          Returns the number of connections released in a update count when there are too many free connections.
 java.lang.String getUid()
          Returns the user id for connecting to the database.
 java.lang.String getUrl()
          Returns the connection url.
 void loadPool(org.jdom.Element config)
          This method actually loads the pool from the properties.
 void release()
          This method will physically close all free connections.
 void setAccess(java.lang.String newUid, java.lang.String newPwd)
          Changed the uid and password for connecting to the database.
 void setInitStatement(java.lang.String statement)
          Set init-Statement executed for each new connection within this pool.
 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)
          Set the log writer for this data source.
 void setMaxFreeConnections(int val)
          Sets the maximal number of free connections so an release is triggered.
 void setReleaseSize(int val)
          Sets the number of connections released when there are too many connections.
 void setUrl(java.lang.String newUrl)
          Changed the database url.
(package private)  void wrapperClosed(java.sql.Connection conn)
          This is called from a connection wrapper object when the ConnectionWrapper.close() method was called.
 

Constructor Detail

ConnectionPool

public ConnectionPool(java.lang.String name,
                      java.lang.String URL,
                      java.lang.String user,
                      java.lang.String password,
                      int maxConns,
                      int initConns,
                      int timeOut,
                      java.lang.String logTarget)
This creates a ConnectionPool.
Parameters:
Name - of the pool.
Connection - url for connecting to the database
User - used to connect to the database
Password - used to connect to the database
Maximal - number of connections this pool will create.
Initial - number of connection this pool will create upon initialisation.
Login - timeout in seconds

ConnectionPool

public ConnectionPool(java.lang.String name,
                      java.lang.String url,
                      java.lang.String user,
                      java.lang.String password,
                      int maxConns,
                      int initConns,
                      int timeOut,
                      java.lang.String initStatement,
                      java.lang.String logTarget)
This creates a ConnectionPool.
Parameters:
Name - of the pool.
Connection - url for connecting to the database
User - used to connect to the database
Password - used to connect to the database
Maximal - number of connections this pool will create.
Initial - number of connection this pool will create upon initialisation.
Login - timeout in seconds
String - which is send as an init statement for each newly created connection.

ConnectionPool

public ConnectionPool(org.jdom.Element config)
               throws PoolInitException
Creates a new pool named poolName. Note that the name is given as a parameter (which is actually read out from the PoolManager). The parameters are passed as properties in the following format:
Name of valueDescription
DBPOOL.name.URLJDBC url to connect to the database
DBPOOL.name.USERUser id for the database connection
DBPOOL.name.PASSWORDPassword for connecting to the database
DBPOOL.name.MAXCONNSMax number of connection this pool will create
DBPOOL.name.INITCONNSonnsInitial number of connections made to database when pool is initialised
DBPOOL.name.TIMEOUTTimeout in seconds for login to database
DBPOOL.name.INITSTAEMENTInitial SQL statement. This statement can be used to initialise the connection with some cutomized code, e.g. setting the default date format used within the application.
See Also:
PoolManager
Method Detail

loadPool

public void loadPool(org.jdom.Element config)
              throws PoolInitException
This method actually loads the pool from the properties. The pools name is determined by the constructor. The following settings are read from the properties:
DBPOOL.name.URL
DBPOOL.name.USER
DBPOOL.name.PASSWORD
DBPOOL.name.MAXCONNS
DBPOOL.name.INITCONNS
DBPOOL.name.TIMOUT
DBPOOL.name.INITSTATEMENT
DBPOOL.name.LOGTARGET
DBPOOL.name.LOGLEVEL
Parameters:
the - properties
Throws:
PoolInitException - If the pool can not be instantiated.

setInitStatement

public void setInitStatement(java.lang.String statement)
Set init-Statement executed for each new connection within this pool.
Parameters:
The - initial SQL statement.

getConnection

public java.sql.Connection getConnection()
                                  throws java.sql.SQLException
Returns a connection from the pool. The connection is wrapped within a ConnectionWrapper object.
Returns:
a connection or null
Throws:
java.sql.SQLException -  
MaxConnectException - if no more connections can be created and all connections are currently checked out
See Also:
ConnectionWrapper

wrapperClosed

void wrapperClosed(java.sql.Connection conn)
This is called from a connection wrapper object when the ConnectionWrapper.close() method was called. The connection is put back to the free connections pool.
Parameters:
conn - The connection wrapper
See Also:
ConnectionWrapper

getFreeConnectionCount

public int getFreeConnectionCount()
Returns the number of free connections
Returns:
The free connections count

getBusyConnectionCount

public int getBusyConnectionCount()
Returns the current active connections count.
Returns:
the number of active connections

checkFreeConnections

public void checkFreeConnections()
Checkes for too many free connections. If there are too many some connections will be closed and removed from the pool. Called by ConnectionPoolUpdateThread.
See Also:
ConnectionPoolUpdateThread

getMaxFreeConnections

public int getMaxFreeConnections()
Returns the number of free connections so some connections may be released.
Returns:
the max free connection count

getReleaseSize

public int getReleaseSize()
Returns the number of connections released in a update count when there are too many free connections.
Returns:
the number

setMaxFreeConnections

public void setMaxFreeConnections(int val)
Sets the maximal number of free connections so an release is triggered.
Parameters:
val - The maximum connection count

setReleaseSize

public void setReleaseSize(int val)
Sets the number of connections released when there are too many connections.
Parameters:
val - The value

release

public void release()
This method will physically close all free connections.

getUid

public java.lang.String getUid()
Returns the user id for connecting to the database.
Returns:
the user id

getUrl

public java.lang.String getUrl()
Returns the connection url.
Returns:
the pools connection url

getName

public java.lang.String getName()
Returns the name of the pool.
Returns:
The pools name

setUrl

public void setUrl(java.lang.String newUrl)
Changed the database url. Only the future connections will using this new url.
Parameters:
The - new database connection url.

setAccess

public void setAccess(java.lang.String newUid,
                      java.lang.String newPwd)
Changed the uid and password for connecting to the database. Only the future connections will be using this new uid and password.
Parameters:
The - new uid.
The - new password.

getConnection

public java.sql.Connection getConnection(java.lang.String username,
                                         java.lang.String password)
                                  throws java.sql.SQLException

Attempt to establish a database connection.

Parameters:
user - the database user on whose behalf the Connection is being made
password - the user's password
Returns:
a Connection to the database
Throws:
java.sql.SQLException - if a database-access error occurs.

getLogWriter

public java.io.PrintWriter getLogWriter()
                                 throws java.sql.SQLException

Get the log writer for this data source.

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 DataSource object is created the log writer is initially null, in other words, logging is disabled.

Returns:
the log writer for this data source, null if disabled
Throws:
java.sql.SQLException - if a database-access error occurs.

setLogWriter

public void setLogWriter(java.io.PrintWriter out)
                  throws java.sql.SQLException

Set the log writer for this data source.

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 DataSource object is created the log writer is initially null, in other words, logging is disabled.

Parameters:
out - the new log writer; to disable, set to null
Throws:
java.sql.SQLException - if a database-access error occurs.

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 DataSource object is created the login timeout is initially zero.

Parameters:
seconds - the data source login time limit
Throws:
java.sql.SQLException - if a database access error occurs.

getLoginTimeout

public int getLoginTimeout()
                    throws java.sql.SQLException
Gets the maximum time in seconds that this data source can wait while attempting to connect to a database. A value of zero means that the timeout is the default system timeout if there is one; otherwise it means that there is no timeout. When a DataSource object is created the login timeout is initially zero.
Returns:
the data source login time limit
Throws:
java.sql.SQLException - if a database access error occurs.

©   O R C A   S y s t e m s