snaq.db
Class ConnectionPool

java.lang.Object
  extended by snaq.util.ObjectPool<CacheConnection>
      extended by snaq.db.ConnectionPool
All Implemented Interfaces:
Comparable<ObjectPool>

public class ConnectionPool
extends ObjectPool<CacheConnection>

Implementation of a database connection pool.

Author:
Giles Winstanley
See Also:
CacheConnection, CachedCallableStatement, CachedPreparedStatement

Constructor Summary
ConnectionPool(String name, int minPool, int maxPool, int maxSize, long idleTimeout, String url, Properties props)
          Creates a new ConnectionPool instance.
ConnectionPool(String name, int minPool, int maxPool, int maxSize, long idleTimeout, String url, String username, String password)
          Creates a new ConnectionPool instance.
ConnectionPool(String name, int maxPool, int maxSize, long idleTimeout, String url, Properties props)
          Creates a new ConnectionPool instance (with minPool=0).
ConnectionPool(String name, int maxPool, int maxSize, long idleTimeout, String url, String username, String password)
          Creates a new ConnectionPool instance (with minPool=0).
 
Method Summary
 void addConnectionPoolListener(ConnectionPoolListener listener)
          Adds a ConnectionPoolListener to the event notification list.
 Connection getConnection()
          Gets a Connection from the pool.
 Connection getConnection(long timeout)
          Gets a Connection from the pool.
 PasswordDecoder getPasswordDecoder()
          Returns the current PasswordDecoder class.
 int getPoolSize()
          Deprecated. Use ObjectPool.getMaxPool() instead.
 ConnectionValidator getValidator()
          Returns the current ConnectionValidator class.
 boolean isCachingCallableStatements()
          Returns whether the pool caches CallableStatement instances for each connection.
 boolean isCachingPreparedStatements()
          Returns whether the pool caches PreparedStatement instances for each connection.
 boolean isCachingStatements()
          Returns whether the pool caches Statement instances for each connection.
 boolean isRecycleAfterDelegateUse()
          Returns whether connections may be recycled if the underlying raw/delegate connection has been used.
 void registerMBean()
          Registers this pool with the platform MBean server.
 void removeConnectionPoolListener(ConnectionPoolListener listener)
          Removes a ConnectionPoolListener from the event notification list.
 void setCaching(boolean b)
          Determines whether to perform statement caching.
 void setCaching(boolean ss, boolean ps, boolean cs)
          Determines whether to perform statement caching.
 void setPasswordDecoder(PasswordDecoder pd)
          Sets the PasswordDecoder class.
 void setRecycleAfterDelegateUse(boolean b)
          Sets whether the connection may be recycled if the underlying raw/delegate connection has been used (default: false).
 void setValidator(ConnectionValidator cv)
          Sets the validator class for Connection instances.
 void unregisterMBean()
          Removes this pool from the platform MBean server registration list.
 
Methods inherited from class snaq.util.ObjectPool
addObjectPoolListener, compareTo, equals, flush, getCheckedOut, getCustomLogger, getExpiryTime, getFreeCount, getHitRate, getIdleTimeout, getMaxPool, getMaxSize, getMinPool, getName, getParametersString, getPoolHitRate, getPoolMissRate, getRequestCount, getSize, hashCode, init, init, isAsyncDestroy, isReleased, registerShutdownHook, release, releaseAsync, releaseForcibly, removeObjectPoolListener, removeShutdownHook, resetHitCounter, setAsyncDestroy, setLog, setParameters, setParameters, toString
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ConnectionPool

public ConnectionPool(String name,
                      int minPool,
                      int maxPool,
                      int maxSize,
                      long idleTimeout,
                      String url,
                      String username,
                      String password)
Creates a new ConnectionPool instance.

Parameters:
name - pool name
minPool - minimum number of pooled connections, or 0 for none
maxPool - maximum number of pooled connections, or 0 for none
maxSize - maximum number of possible connections, or 0 for no limit
idleTimeout - idle timeout (seconds) for idle pooled connections, or 0 for no timeout
url - JDBC connection URL
username - database username
password - password for the database username supplied

ConnectionPool

public ConnectionPool(String name,
                      int maxPool,
                      int maxSize,
                      long idleTimeout,
                      String url,
                      String username,
                      String password)
Creates a new ConnectionPool instance (with minPool=0).

Parameters:
name - pool name
maxPool - maximum number of pooled connections, or 0 for none
maxSize - maximum number of possible connections, or 0 for no limit
idleTimeout - idle timeout (seconds) for idle pooled connections, or 0 for no timeout
url - JDBC connection URL
username - database username
password - password for the database username supplied

ConnectionPool

public ConnectionPool(String name,
                      int minPool,
                      int maxPool,
                      int maxSize,
                      long idleTimeout,
                      String url,
                      Properties props)
Creates a new ConnectionPool instance.

Parameters:
name - pool name
minPool - minimum number of pooled connections, or 0 for none
maxPool - maximum number of pooled connections, or 0 for none
maxSize - maximum number of possible connections, or 0 for no limit
idleTimeout - idle timeout (seconds) for idle pooled connections, or 0 for no timeout
url - JDBC connection URL
props - connection properties

ConnectionPool

public ConnectionPool(String name,
                      int maxPool,
                      int maxSize,
                      long idleTimeout,
                      String url,
                      Properties props)
Creates a new ConnectionPool instance (with minPool=0).

Parameters:
name - pool name
maxPool - maximum number of pooled connections, or 0 for none
maxSize - maximum number of possible connections, or 0 for no limit
idleTimeout - idle timeout (seconds) for idle pooled connections, or 0 for no timeout
url - JDBC connection URL
props - connection properties
Method Detail

registerMBean

public void registerMBean()
Registers this pool with the platform MBean server. Once this method has been called, each pool instance is accessible as an MBean using the javax.management API.


unregisterMBean

public void unregisterMBean()
Removes this pool from the platform MBean server registration list. Once this method has been called, each pool instance is accessible as an MBean using the javax.management API.


getPoolSize

@Deprecated
public final int getPoolSize()
Deprecated. Use ObjectPool.getMaxPool() instead.

Returns the maximum number of items that can be pooled. Moved from ObjectPool class to here, and maintained only for backwards-compatibility.


setValidator

public void setValidator(ConnectionValidator cv)
Sets the validator class for Connection instances.


getValidator

public ConnectionValidator getValidator()
Returns the current ConnectionValidator class.


setPasswordDecoder

public void setPasswordDecoder(PasswordDecoder pd)
Sets the PasswordDecoder class.


getPasswordDecoder

public PasswordDecoder getPasswordDecoder()
Returns the current PasswordDecoder class.


getConnection

public Connection getConnection()
                         throws SQLException
Gets a Connection from the pool.

Throws:
SQLException

getConnection

public Connection getConnection(long timeout)
                         throws SQLException
Gets a Connection from the pool.

Throws:
SQLException

setCaching

public void setCaching(boolean b)
Determines whether to perform statement caching. This applies to all types of statements (normal, prepared, callable).


setCaching

public void setCaching(boolean ss,
                       boolean ps,
                       boolean cs)
Determines whether to perform statement caching.

Parameters:
ss - whether to cache Statement objects
ps - whether to cache PreparedStatement objects
cs - whether to cache CallableStatement objects

isCachingStatements

public boolean isCachingStatements()
Returns whether the pool caches Statement instances for each connection.


isCachingPreparedStatements

public boolean isCachingPreparedStatements()
Returns whether the pool caches PreparedStatement instances for each connection.


isCachingCallableStatements

public boolean isCachingCallableStatements()
Returns whether the pool caches CallableStatement instances for each connection.


setRecycleAfterDelegateUse

public final void setRecycleAfterDelegateUse(boolean b)
Sets whether the connection may be recycled if the underlying raw/delegate connection has been used (default: false).

Each CacheConnection instance tracks whether a call to CacheConnection.getDelegateConnection() has been made, and by default prevents recycling of the connection if so, in order to help maintain integrity of the pool. In certain circumstances it may be beneficial in terms of performance to enable such recycling, provided the raw connections are not compromised in any way, and the CacheConnection.close() method is called on each CacheConnection instance and NOT the raw connection.


isRecycleAfterDelegateUse

public boolean isRecycleAfterDelegateUse()
Returns whether connections may be recycled if the underlying raw/delegate connection has been used.


addConnectionPoolListener

public final void addConnectionPoolListener(ConnectionPoolListener listener)
Adds a ConnectionPoolListener to the event notification list.


removeConnectionPoolListener

public final void removeConnectionPoolListener(ConnectionPoolListener listener)
Removes a ConnectionPoolListener from the event notification list.