anvil.database
Class ConnectionPool

java.lang.Object
  |
  +--anvil.database.ConnectionPool
All Implemented Interfaces:
ConnectionMonitor

public class ConnectionPool
extends java.lang.Object
implements ConnectionMonitor

Pool for storing limited amount of connections as PooledConnection. Pool is used by ConnectionAccessQueue which takes care of the situations when connections aren't immediately available.

Methods which might alter the connection vectors are internally synchronized so that only one thread at a time can perform operations on them. However several threads may be creating connections at the same time.

Version:
$Revision: 1.16 $
Author:
Jani Lehtimäki
See Also:
PooledConnection, ConnectionAccessQueue, ConnectionFactory

Nested Class Summary
 class ConnectionPool.ConnectionAllocator
          Connection allocator runs through the loop sleeping specified amount on each loop and invoking checkConnections method on each loop.
 
Constructor Summary
ConnectionPool(Zone zone, PoolPreferences prefs)
          Constructs the pool.
 
Method Summary
 PooledConnection acquire(int timeout)
          Acquires new connection from the pool.
 void connectionAcquireTimedout(ConnectionPool pool, long duration)
          Notifies unsuccessful attempt to acquire connection.
 void connectionClosed(PooledConnection connection)
          Notifies that connection is being closed, because maximum lifetime has been exceeded or java.sql.Connection instance has been expliticly closed (for instance with java.sql.Connection.close()).
 void connectionCreated(PooledConnection connection, long duration)
          Notifies creation of connection.
 void connectionException(java.lang.String source, java.lang.Exception exception)
          Notifies exception situation.
 void connectionReleased(PooledConnection connection)
          Notifies release of connection immediately after operation has been performed.
 void connectionReserved(PooledConnection connection, long duration)
          Notifies reservation of connection immediately after operation has been performed.
 void connectionTimeout(PooledConnection connection)
          Notifies that the maximum reservation time of connection has been exceed.
 int getActiveConnections()
          Gets the current amount of connections active.
 int getConnectionAcquireTimeout()
          Gets the connection reservation timeout.
 int getConnectionLifetime()
          Gets the connection lifetime.
 int getConnectionTimeout()
          Gets the connection reservation timeout.
 java.lang.String getDriver()
          Gets the class of driver.
 int getFreeConnections()
          Gets the current amount of connections free.
 int getMaxConnections()
          Gets the maximum amount of connections.
 int getMinConnections()
          Gets the recommended minimum amount of connections.
 java.lang.String getName()
          Gets the name of pool.
 java.lang.String getPassword()
           
 int getPendingConnections()
          Gets the current amount of pending connections, ie.
 java.lang.Object getPreference(java.lang.String name)
          Gets preference by name from PoolPreferences.
 int getTotalConnections()
          Gets the current amount of connections on this pool.
 java.lang.String getURL()
           
 java.lang.String getUsername()
           
 Zone getZone()
          Gets the zhe for this pool.
 void initialize(ConnectionPool pool)
          Initializes the monitor, called after the manager has been started up.
 void release(PooledConnection connection)
          Notifies pool about the connection being released.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ConnectionPool

public ConnectionPool(Zone zone,
                      PoolPreferences prefs)
Constructs the pool.

Method Detail

getName

public java.lang.String getName()
Gets the name of pool.

Returns:
Name of pool

getDriver

public java.lang.String getDriver()
Gets the class of driver.

Returns:
Connection timeout, milliseconds

getZone

public Zone getZone()
Gets the zhe for this pool.

Returns:
Connection timeout, milliseconds

getURL

public java.lang.String getURL()

getUsername

public java.lang.String getUsername()

getPassword

public java.lang.String getPassword()

getMinConnections

public int getMinConnections()
Gets the recommended minimum amount of connections.

Returns:
Minimum amount of connections

getMaxConnections

public int getMaxConnections()
Gets the maximum amount of connections.

Returns:
Maximum amount of connections

getConnectionLifetime

public int getConnectionLifetime()
Gets the connection lifetime. Lifetime may be exceed if the connection is reserved.

Returns:
Connection lifetime, milliseconds

getConnectionTimeout

public int getConnectionTimeout()
Gets the connection reservation timeout. When this period is exceed connection is unconditionally closed.

Returns:
Connection timeout, milliseconds

getConnectionAcquireTimeout

public int getConnectionAcquireTimeout()
Gets the connection reservation timeout. When this period is exceed connection is unconditionally closed.

Returns:
Connection timeout, milliseconds

getPreference

public java.lang.Object getPreference(java.lang.String name)
Gets preference by name from PoolPreferences.


getActiveConnections

public int getActiveConnections()
Gets the current amount of connections active.

Returns:
Amount of active connections

getPendingConnections

public int getPendingConnections()
Gets the current amount of pending connections, ie. connections that currently being opened.

Returns:
Amount of pending connections

getFreeConnections

public int getFreeConnections()
Gets the current amount of connections free.

Returns:
Amount of free connections

getTotalConnections

public int getTotalConnections()
Gets the current amount of connections on this pool.

Returns:
Amount of connections

acquire

public PooledConnection acquire(int timeout)
                         throws java.lang.Exception
Acquires new connection from the pool. Strategy:

Parameters:
timeout - Timeout override (if > 0)
Returns:
Connection or null if it couldn't be created
java.lang.Exception

release

public void release(PooledConnection connection)
Notifies pool about the connection being released. Allocator thread is interrupted immediately because active connections now contains released connection whose state need to be checked.

Parameters:
connection - Connection

initialize

public void initialize(ConnectionPool pool)
Description copied from interface: ConnectionMonitor
Initializes the monitor, called after the manager has been started up.

Specified by:
initialize in interface ConnectionMonitor
Parameters:
pool - Instance of connection pool

connectionCreated

public void connectionCreated(PooledConnection connection,
                              long duration)
Description copied from interface: ConnectionMonitor
Notifies creation of connection. Upon notification connection is considered pending, not yet reserved.

Specified by:
connectionCreated in interface ConnectionMonitor
Parameters:
connection - PooledConnection
duration - Duration of operation, in milliseconds

connectionReserved

public void connectionReserved(PooledConnection connection,
                               long duration)
Description copied from interface: ConnectionMonitor
Notifies reservation of connection immediately after operation has been performed.

Specified by:
connectionReserved in interface ConnectionMonitor
Parameters:
connection - PooledConnection
duration - Duration of operation, in milliseconds

connectionAcquireTimedout

public void connectionAcquireTimedout(ConnectionPool pool,
                                      long duration)
Description copied from interface: ConnectionMonitor
Notifies unsuccessful attempt to acquire connection. Maximum timeout for acquire operation has been exceed.

Specified by:
connectionAcquireTimedout in interface ConnectionMonitor
Parameters:
pool - ConnectionPool
duration - Duration of operation, in milliseconds

connectionReleased

public void connectionReleased(PooledConnection connection)
Description copied from interface: ConnectionMonitor
Notifies release of connection immediately after operation has been performed.

Specified by:
connectionReleased in interface ConnectionMonitor
Parameters:
connection - PooledConnection

connectionClosed

public void connectionClosed(PooledConnection connection)
Description copied from interface: ConnectionMonitor
Notifies that connection is being closed, because maximum lifetime has been exceeded or java.sql.Connection instance has been expliticly closed (for instance with java.sql.Connection.close()).

Specified by:
connectionClosed in interface ConnectionMonitor
Parameters:
connection - PooledConnection

connectionTimeout

public void connectionTimeout(PooledConnection connection)
Description copied from interface: ConnectionMonitor
Notifies that the maximum reservation time of connection has been exceed. Thus the reserving thread is considered dead and connection has been a subject to forced shutdown.

Specified by:
connectionTimeout in interface ConnectionMonitor
Parameters:
connection - PooledConnection

connectionException

public void connectionException(java.lang.String source,
                                java.lang.Exception exception)
Description copied from interface: ConnectionMonitor
Notifies exception situation.

Specified by:
connectionException in interface ConnectionMonitor
Parameters:
source - Informative source name (class.method)
exception - Exception that occured