org.apache.cayenne.conn
Class PoolManager

java.lang.Object
  extended by org.apache.cayenne.conn.PoolManager
All Implemented Interfaces:
java.util.EventListener, javax.sql.ConnectionEventListener, javax.sql.DataSource

public class PoolManager
extends java.lang.Object
implements javax.sql.DataSource, javax.sql.ConnectionEventListener

PoolManager is a pooling DataSource impementation. Internally to obtain connections PoolManager uses either a JDBC driver or another pooling datasource.

TODO: create a low priority thread that will do pool maintenance.

Author:
Andrei Adamchik

Field Summary
protected  java.lang.String dataSourceUrl
           
protected  java.lang.String jdbcDriver
           
static int MAX_QUEUE_WAIT
          Defines a maximum time in milliseconds that a connection request could wait in the connection queue.
protected  int maxConnections
           
protected  int minConnections
           
protected  java.lang.String password
           
protected  javax.sql.ConnectionPoolDataSource poolDataSource
           
protected  java.util.List unusedPool
           
protected  java.util.List usedPool
           
protected  java.lang.String userName
           
 
Constructor Summary
PoolManager(javax.sql.ConnectionPoolDataSource poolDataSource, int minCons, int maxCons, java.lang.String userName, java.lang.String password)
          Creates new PoolManager with the specified policy for connection pooling and a ConnectionPoolDataSource object.
PoolManager(java.lang.String jdbcDriver, java.lang.String dataSourceUrl, int minCons, int maxCons, java.lang.String userName, java.lang.String password)
          Creates new PoolManager using org.apache.cayenne.conn.PoolDataSource for an underlying ConnectionPoolDataSource.
PoolManager(java.lang.String jdbcDriver, java.lang.String dataSourceUrl, int minCons, int maxCons, java.lang.String userName, java.lang.String password, ConnectionEventLoggingDelegate logger)
           
 
Method Summary
protected  boolean canGrowPool()
           
 void connectionClosed(javax.sql.ConnectionEvent event)
          Returns closed connection to the pool.
 void connectionErrorOccurred(javax.sql.ConnectionEvent event)
          Removes connection with an error from the pool.
 void dispose()
          Closes all existing connections, removes them from the pool.
protected  void disposeOfMaintenanceThread()
           
 java.sql.Connection getConnection()
          Returns connection from the pool using internal values of user name and password.
 java.sql.Connection getConnection(java.lang.String userName, java.lang.String password)
          Returns connection from the pool.
 int getCurrentlyInUse()
          Returns the number of connections obtained via this DataSource that are currently in use by the DataSource clients.
 int getCurrentlyUnused()
          Returns the number of connections maintained in the pool that are currently not used by any clients and are available immediately via getConnection method.
 java.lang.String getDataSourceUrl()
          Returns a database URL used to initialize this pool.
 java.lang.String getJdbcDriver()
          Returns a name of a JDBC driver used to initialize this pool.
 int getLoginTimeout()
           
 java.io.PrintWriter getLogWriter()
           
 int getMaxConnections()
          Returns maximum number of connections this pool can keep.
 int getMinConnections()
          Returns the absolute minimum number of connections allowed in this pool at any moment in time.
 java.lang.String getPassword()
          Returns a data source password used to initialize this pool.
 int getPoolSize()
          Returns current number of connections.
 java.lang.String getUserName()
          Returns a data source user name used to initialize this pool.
protected  int growPool(int addConnections, java.lang.String userName, java.lang.String password)
          Increases connection pool by the specified number of connections.
protected  void init(javax.sql.ConnectionPoolDataSource poolDataSource, int minCons, int maxCons, java.lang.String userName, java.lang.String password)
          Initializes pool.
protected  javax.sql.PooledConnection newPooledConnection(java.lang.String userName, java.lang.String password)
          Creates and returns new PooledConnection object, adding itself as a listener for connection events.
 void setLoginTimeout(int seconds)
           
 void setLogWriter(java.io.PrintWriter out)
           
 void setMaxConnections(int maxConnections)
           
 void setMinConnections(int minConnections)
           
protected  void shrinkPool(int closeConnections)
           
protected  void startMaintenanceThread()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAX_QUEUE_WAIT

public static final int MAX_QUEUE_WAIT
Defines a maximum time in milliseconds that a connection request could wait in the connection queue. After this period expires, an exception will be thrown in the calling method. In the future this parameter should be made configurable.

See Also:
Constant Field Values

poolDataSource

protected javax.sql.ConnectionPoolDataSource poolDataSource

minConnections

protected int minConnections

maxConnections

protected int maxConnections

dataSourceUrl

protected java.lang.String dataSourceUrl

jdbcDriver

protected java.lang.String jdbcDriver

password

protected java.lang.String password

userName

protected java.lang.String userName

unusedPool

protected java.util.List unusedPool

usedPool

protected java.util.List usedPool
Constructor Detail

PoolManager

public PoolManager(java.lang.String jdbcDriver,
                   java.lang.String dataSourceUrl,
                   int minCons,
                   int maxCons,
                   java.lang.String userName,
                   java.lang.String password)
            throws java.sql.SQLException
Creates new PoolManager using org.apache.cayenne.conn.PoolDataSource for an underlying ConnectionPoolDataSource.

Throws:
java.sql.SQLException

PoolManager

public PoolManager(java.lang.String jdbcDriver,
                   java.lang.String dataSourceUrl,
                   int minCons,
                   int maxCons,
                   java.lang.String userName,
                   java.lang.String password,
                   ConnectionEventLoggingDelegate logger)
            throws java.sql.SQLException
Throws:
java.sql.SQLException

PoolManager

public PoolManager(javax.sql.ConnectionPoolDataSource poolDataSource,
                   int minCons,
                   int maxCons,
                   java.lang.String userName,
                   java.lang.String password)
            throws java.sql.SQLException
Creates new PoolManager with the specified policy for connection pooling and a ConnectionPoolDataSource object.

Parameters:
poolDataSource - data source for pooled connections
minCons - Non-negative integer that specifies a minimum number of open connections to keep in the pool at all times
maxCons - Non-negative integer that specifies maximum number of simultaneuosly open connections
Throws:
java.sql.SQLException - if pool manager can not be created.
Method Detail

init

protected void init(javax.sql.ConnectionPoolDataSource poolDataSource,
                    int minCons,
                    int maxCons,
                    java.lang.String userName,
                    java.lang.String password)
             throws java.sql.SQLException
Initializes pool. Normally called from constructor.

Throws:
java.sql.SQLException

startMaintenanceThread

protected void startMaintenanceThread()

newPooledConnection

protected javax.sql.PooledConnection newPooledConnection(java.lang.String userName,
                                                         java.lang.String password)
                                                  throws java.sql.SQLException
Creates and returns new PooledConnection object, adding itself as a listener for connection events.

Throws:
java.sql.SQLException

dispose

public void dispose()
             throws java.sql.SQLException
Closes all existing connections, removes them from the pool.

Throws:
java.sql.SQLException

disposeOfMaintenanceThread

protected void disposeOfMaintenanceThread()

canGrowPool

protected boolean canGrowPool()
Returns:
true if at least one more connection can be added to the pool.

growPool

protected int growPool(int addConnections,
                       java.lang.String userName,
                       java.lang.String password)
                throws java.sql.SQLException
Increases connection pool by the specified number of connections.

Returns:
the actual number of created connections.
Throws:
java.sql.SQLException - if an error happens when creating a new connection.

shrinkPool

protected void shrinkPool(int closeConnections)

getMaxConnections

public int getMaxConnections()
Returns maximum number of connections this pool can keep. This parameter when configured allows to limit the number of simultaneously open connections.


setMaxConnections

public void setMaxConnections(int maxConnections)

getMinConnections

public int getMinConnections()
Returns the absolute minimum number of connections allowed in this pool at any moment in time.


setMinConnections

public void setMinConnections(int minConnections)

getDataSourceUrl

public java.lang.String getDataSourceUrl()
Returns a database URL used to initialize this pool. Will return null if the pool was initialized with ConnectionPoolDataSource.


getJdbcDriver

public java.lang.String getJdbcDriver()
Returns a name of a JDBC driver used to initialize this pool. Will return null if the pool was initialized with ConnectionPoolDataSource.


getPassword

public java.lang.String getPassword()
Returns a data source password used to initialize this pool.


getUserName

public java.lang.String getUserName()
Returns a data source user name used to initialize this pool.


getPoolSize

public int getPoolSize()
Returns current number of connections.


getCurrentlyInUse

public int getCurrentlyInUse()
Returns the number of connections obtained via this DataSource that are currently in use by the DataSource clients.


getCurrentlyUnused

public int getCurrentlyUnused()
Returns the number of connections maintained in the pool that are currently not used by any clients and are available immediately via getConnection method.


getConnection

public java.sql.Connection getConnection()
                                  throws java.sql.SQLException
Returns connection from the pool using internal values of user name and password. Eqivalent to calling:

ds.getConnection(ds.getUserName(), ds.getPassword())

Specified by:
getConnection in interface javax.sql.DataSource
Throws:
java.sql.SQLException

getConnection

public java.sql.Connection getConnection(java.lang.String userName,
                                         java.lang.String password)
                                  throws java.sql.SQLException
Returns connection from the pool.

Specified by:
getConnection in interface javax.sql.DataSource
Throws:
java.sql.SQLException

getLoginTimeout

public int getLoginTimeout()
                    throws java.sql.SQLException
Specified by:
getLoginTimeout in interface javax.sql.DataSource
Throws:
java.sql.SQLException

setLoginTimeout

public void setLoginTimeout(int seconds)
                     throws java.sql.SQLException
Specified by:
setLoginTimeout in interface javax.sql.DataSource
Throws:
java.sql.SQLException

getLogWriter

public java.io.PrintWriter getLogWriter()
                                 throws java.sql.SQLException
Specified by:
getLogWriter in interface javax.sql.DataSource
Throws:
java.sql.SQLException

setLogWriter

public void setLogWriter(java.io.PrintWriter out)
                  throws java.sql.SQLException
Specified by:
setLogWriter in interface javax.sql.DataSource
Throws:
java.sql.SQLException

connectionClosed

public void connectionClosed(javax.sql.ConnectionEvent event)
Returns closed connection to the pool.

Specified by:
connectionClosed in interface javax.sql.ConnectionEventListener

connectionErrorOccurred

public void connectionErrorOccurred(javax.sql.ConnectionEvent event)
Removes connection with an error from the pool. This method is called by PoolManager connections on connection errors to notify PoolManager that connection is in invalid state.

Specified by:
connectionErrorOccurred in interface javax.sql.ConnectionEventListener


Copyright © 2001-2006 Apache.org All Rights Reserved.