info.fingo.db.pool
Class PoolingDataSource

java.lang.Object
  extended by info.fingo.db.pool.PoolingDataSource
All Implemented Interfaces:
javax.sql.DataSource

public class PoolingDataSource
extends java.lang.Object
implements javax.sql.DataSource

Connection pooling implementation of javax.sql.DataSource interface.

This implementation is database vendor independent.

There is a set of PROPERTY_XXX constants, with correspondig getters and setters which can be used to customize the connection pool.

Examplary use:

 
 void poolingDataSourceExample( String driverName, String dbUrl, String dbUser, String dbPassword ) throws SQLException
 {
        Connection conn = null;
        try
        {
                // create new data source instance
                PoolingDataSource pds = new PoolingDataSource( driverName, dbUrl, dbUser, dbPassword );
 
                // customize your pool
                pds.setInitialConnectionCount( 7 );
                // any other setXXX method call
 
                conn = pds.getConnection();
 
                // use the connection
        }
        finally
        {
                if( conn != null )
                {
                        // here, the connection isn't closed in fact
                        // it is now marked in the pool as 'free' - ready to use 
                        conn.close();
                }
        }
 }
 
 

Author:
Maciej Mroczko - FINGO
See Also:
PoolableConnection

Field Summary
protected  java.util.Properties databaseProperties
          The data source properties
static int DEFAULT_ALL_CONNECTIONS_LIMIT
          The default number of the all connections: 15.
static int DEFAULT_CONNECTION_EXPIRE_TIME
          The default connection expire time: 60 minutes.
static int DEFAULT_CONNECTION_IDLE_TIME
          The default connection idle time: 10 minutes.
static int DEFAULT_CONNECTION_TIMEOUT
          The default connection wait timeout: 10 seconds.
static int DEFAULT_INITIAL_CONNECTION_COUNT
          The default initial number of the pooled connections: 1.
static int DEFAULT_MAXIMUM_CONNECTION_COUNT
          The default maximum number of the pooled connections: 5.
static java.lang.String PROPERTY_ALL_CONNECTIONS_LIMIT
          The all connections limit property name: 'cm.connection.all.limit'.
static java.lang.String PROPERTY_CONNECTION_EXPIRE_TIME
          The connection expire time property name: 'cm.connection.expire.time'.
static java.lang.String PROPERTY_CONNECTION_IDLE_TIME
          The connection idle time property name: 'cm.connection.idle.time'.
static java.lang.String PROPERTY_CONNECTION_TEST_QUERY
          The query to ensure that conection is open: 'cm.connection.test.query'.
static java.lang.String PROPERTY_CONNECTION_WAIT_TIMEOUT
          The timeout waiting for connection property name: 'cm.connection.wait.timeout'.
static java.lang.String PROPERTY_DB_PASSWORD
          The databse password property name: 'cm.connection.password'.
static java.lang.String PROPERTY_DB_URL
          The database url property name: 'cm.connection.url'.
static java.lang.String PROPERTY_DB_USER
          The databse user name property name: 'cm.connection.user'.
static java.lang.String PROPERTY_DRIVER_NAME
          The driver property name: 'cm.connection.driver'.
static java.lang.String PROPERTY_INITIAL_CONNECTION_COUNT
          The initial connection count property name: 'cm.connection.count.initial'.
static java.lang.String PROPERTY_MAX_CONNECTION_COUNT
          The maximum connection count property name: 'cm.connection.count.maximum'.
 
Constructor Summary
PoolingDataSource()
          Default constructor
PoolingDataSource(java.util.Properties properties)
          Creates new instance with the given properties as pool configuration.
PoolingDataSource(java.lang.String driverClassName, java.lang.String dbUrl, java.lang.String dbUser, java.lang.String dbPassword)
          Creates new instance with all required properties specified.
 
Method Summary
 int getAllConnectionsLimit()
          Gets the limit for all connections.
 java.sql.Connection getConnection()
           
 java.sql.Connection getConnection(java.lang.String username, java.lang.String password)
           
 int getConnectionExpireTime()
          Gets the connection expire time.
 int getConnectionIdleTime()
          Gets the connection idle time.
 java.lang.String getDriverClassName()
          Gets the database driver class name.
 int getInitialConnectionCount()
          Gets the initial connection count.
 int getLoginTimeout()
           
 java.io.PrintWriter getLogWriter()
           
 int getMaximumConnectionCount()
          Gets the maximum connection count.
 java.lang.String getUrl()
          Gets the database url.
 java.lang.String getUserName()
          Gets the data source user name
 java.lang.String getValidationQuery()
          Gets the connection validation query
protected  boolean isInitialized()
          Tests if the data source is already initialized.
 void setAllConnectionsLimit(int count)
          Sets the limit for all connections.
 void setConnectionExpireTime(int minutes)
          Sets the connection expired time.
 void setConnectionIdleTime(int minutes)
          Sets the connection idle time.
 void setDriverClassName(java.lang.String driverClassName)
          Sets the database driver class name.
 void setInitialConnectionCount(int count)
          Sets the initial count of the connections.
 void setLoginTimeout(int seconds)
           
 void setLogWriter(java.io.PrintWriter out)
           
 void setMaximumConnectionCount(int count)
          Sets the maximum connection count.
 void setPassword(java.lang.String password)
          Sets the data source password
 void setUrl(java.lang.String url)
          Sets the database url
 void setUserName(java.lang.String userName)
          Sets the data source user name.
 void setValidationQuery(java.lang.String validationQuery)
          Sets the connection validation query.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PROPERTY_ALL_CONNECTIONS_LIMIT

public static final java.lang.String PROPERTY_ALL_CONNECTIONS_LIMIT
The all connections limit property name: 'cm.connection.all.limit'.

When the pool has maximum connection open and used, the data source may create connection which will be not pooled. Such extra connection will be phisically closed just after Connection.close() method call.

The PROPERTY_ALL_CONNECTIONS_LIMIT defines the max count of pooled and non-pooled connections together.

See Also:
Constant Field Values

PROPERTY_MAX_CONNECTION_COUNT

public static final java.lang.String PROPERTY_MAX_CONNECTION_COUNT
The maximum connection count property name: 'cm.connection.count.maximum'.

This property defines maximum count of pooled connections. When the value is reached, extra, not pooled connections may be still created (until connection count < PROPERTY_ALL_CONNECTIONS_LIMIT).

See Also:
PROPERTY_ALL_CONNECTIONS_LIMIT, Constant Field Values

PROPERTY_INITIAL_CONNECTION_COUNT

public static final java.lang.String PROPERTY_INITIAL_CONNECTION_COUNT
The initial connection count property name: 'cm.connection.count.initial'.

Defines count of connections the data source will create on initialization time.

See Also:
PROPERTY_MAX_CONNECTION_COUNT, PROPERTY_ALL_CONNECTIONS_LIMIT, Constant Field Values

PROPERTY_DRIVER_NAME

public static final java.lang.String PROPERTY_DRIVER_NAME
The driver property name: 'cm.connection.driver'.

The full name of the database driver class. Required.

See Also:
Constant Field Values

PROPERTY_DB_URL

public static final java.lang.String PROPERTY_DB_URL
The database url property name: 'cm.connection.url'.

The database url. Required.

See Also:
Constant Field Values

PROPERTY_CONNECTION_WAIT_TIMEOUT

public static final java.lang.String PROPERTY_CONNECTION_WAIT_TIMEOUT
The timeout waiting for connection property name: 'cm.connection.wait.timeout'.

After that time, 'connection timeout' exception will be thrown when waiting for connection. Specified in seconds.

See Also:
DEFAULT_CONNECTION_TIMEOUT, Constant Field Values

PROPERTY_CONNECTION_EXPIRE_TIME

public static final java.lang.String PROPERTY_CONNECTION_EXPIRE_TIME
The connection expire time property name: 'cm.connection.expire.time'.

Connection which was taken from the pool and not returned to the pool for expire time value will be closed. Specified in minutes.

See Also:
DEFAULT_CONNECTION_EXPIRE_TIME, Constant Field Values

PROPERTY_CONNECTION_IDLE_TIME

public static final java.lang.String PROPERTY_CONNECTION_IDLE_TIME
The connection idle time property name: 'cm.connection.idle.time'.

After idle time, the not-used, pooled connection will be closed and removed from the pool. Specified in minutes.

See Also:
DEFAULT_CONNECTION_IDLE_TIME, Constant Field Values

PROPERTY_DB_USER

public static final java.lang.String PROPERTY_DB_USER
The databse user name property name: 'cm.connection.user'.

The database user name.

See Also:
Constant Field Values

PROPERTY_DB_PASSWORD

public static final java.lang.String PROPERTY_DB_PASSWORD
The databse password property name: 'cm.connection.password'.

The database user password.

See Also:
Constant Field Values

PROPERTY_CONNECTION_TEST_QUERY

public static final java.lang.String PROPERTY_CONNECTION_TEST_QUERY
The query to ensure that conection is open: 'cm.connection.test.query'.

The test query is used before returning requested connection, to ensure if it's really open. The query shouldn't be resource consuming, but must connect database.

See Also:
Constant Field Values

DEFAULT_CONNECTION_TIMEOUT

public static final int DEFAULT_CONNECTION_TIMEOUT
The default connection wait timeout: 10 seconds. Specified in seconds.

See Also:
Constant Field Values

DEFAULT_CONNECTION_EXPIRE_TIME

public static final int DEFAULT_CONNECTION_EXPIRE_TIME
The default connection expire time: 60 minutes. Specified in minutes.

See Also:
Constant Field Values

DEFAULT_CONNECTION_IDLE_TIME

public static final int DEFAULT_CONNECTION_IDLE_TIME
The default connection idle time: 10 minutes. Specified in minutes.

See Also:
Constant Field Values

DEFAULT_MAXIMUM_CONNECTION_COUNT

public static final int DEFAULT_MAXIMUM_CONNECTION_COUNT
The default maximum number of the pooled connections: 5.

See Also:
Constant Field Values

DEFAULT_INITIAL_CONNECTION_COUNT

public static final int DEFAULT_INITIAL_CONNECTION_COUNT
The default initial number of the pooled connections: 1.

See Also:
Constant Field Values

DEFAULT_ALL_CONNECTIONS_LIMIT

public static final int DEFAULT_ALL_CONNECTIONS_LIMIT
The default number of the all connections: 15.

See Also:
Constant Field Values

databaseProperties

protected java.util.Properties databaseProperties
The data source properties

Constructor Detail

PoolingDataSource

public PoolingDataSource()
Default constructor


PoolingDataSource

public PoolingDataSource(java.lang.String driverClassName,
                         java.lang.String dbUrl,
                         java.lang.String dbUser,
                         java.lang.String dbPassword)
Creates new instance with all required properties specified.

Parameters:
driverClassName - - the full name of database driver class
dbUrl - - the database url
dbUser - - the database user name
dbPassword - - the database user password

PoolingDataSource

public PoolingDataSource(java.util.Properties properties)
Creates new instance with the given properties as pool configuration. All supported properties are defined as PROPERTY_XXX constants.

Parameters:
properties - - the connection pool configuration
Method Detail

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

getConnectionExpireTime

public int getConnectionExpireTime()
Gets the connection expire time.

Returns:
The connection expire time in minutes
See Also:
PROPERTY_CONNECTION_EXPIRE_TIME

setConnectionExpireTime

public void setConnectionExpireTime(int minutes)
Sets the connection expired time.

Parameters:
minutes - - the connection expired time in minutes
See Also:
PROPERTY_CONNECTION_EXPIRE_TIME

getConnectionIdleTime

public int getConnectionIdleTime()
Gets the connection idle time.

Returns:
The connection idle time in minutes
See Also:
PROPERTY_CONNECTION_IDLE_TIME

setConnectionIdleTime

public void setConnectionIdleTime(int minutes)
Sets the connection idle time.

Parameters:
minutes - - the connection idle time in minutes
See Also:
PROPERTY_CONNECTION_IDLE_TIME

getMaximumConnectionCount

public int getMaximumConnectionCount()
Gets the maximum connection count.

Returns:
The maximum connection count
See Also:
PROPERTY_MAX_CONNECTION_COUNT

setMaximumConnectionCount

public void setMaximumConnectionCount(int count)
Sets the maximum connection count.

Parameters:
count - - the maximum connection count to set
See Also:
PROPERTY_MAX_CONNECTION_COUNT

getInitialConnectionCount

public int getInitialConnectionCount()
Gets the initial connection count.

Returns:
The initial connection count
See Also:
PROPERTY_INITIAL_CONNECTION_COUNT

setInitialConnectionCount

public void setInitialConnectionCount(int count)
Sets the initial count of the connections.

Parameters:
count - - the initial count of the connections to set
See Also:
PROPERTY_INITIAL_CONNECTION_COUNT

getAllConnectionsLimit

public int getAllConnectionsLimit()
Gets the limit for all connections.

Returns:
The limit for all connections
See Also:
PROPERTY_ALL_CONNECTIONS_LIMIT

setAllConnectionsLimit

public void setAllConnectionsLimit(int count)
Sets the limit for all connections.

Parameters:
count - - the limit for all connections
See Also:
PROPERTY_ALL_CONNECTIONS_LIMIT

getValidationQuery

public java.lang.String getValidationQuery()
Gets the connection validation query

Returns:
The connection validation query
See Also:
PROPERTY_CONNECTION_TEST_QUERY

setValidationQuery

public void setValidationQuery(java.lang.String validationQuery)
Sets the connection validation query.

Parameters:
validationQuery - - the connection validation query to set
See Also:
PROPERTY_CONNECTION_TEST_QUERY

getDriverClassName

public java.lang.String getDriverClassName()
Gets the database driver class name.

Returns:
The database driver class name
See Also:
PROPERTY_DRIVER_NAME

setDriverClassName

public void setDriverClassName(java.lang.String driverClassName)
Sets the database driver class name.

Parameters:
driverClassName - - the database driver class name
See Also:
PROPERTY_DRIVER_NAME

getUrl

public java.lang.String getUrl()
Gets the database url.

Returns:
The database url
See Also:
PROPERTY_DB_URL

setUrl

public void setUrl(java.lang.String url)
Sets the database url

Parameters:
url - - the database url
See Also:
PROPERTY_DB_URL

getUserName

public java.lang.String getUserName()
Gets the data source user name

Returns:
The data source user name

setUserName

public void setUserName(java.lang.String userName)
Sets the data source user name.

Parameters:
userName - the data source user name to set.
See Also:
PROPERTY_DB_USER

setPassword

public void setPassword(java.lang.String password)
Sets the data source password

Parameters:
password - - the data source pasword to set
See Also:
PROPERTY_DB_PASSWORD

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

getConnection

public java.sql.Connection getConnection()
                                  throws java.sql.SQLException
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
Specified by:
getConnection in interface javax.sql.DataSource
Throws:
java.sql.SQLException

isInitialized

protected boolean isInitialized()
Tests if the data source is already initialized.

Returns:
true, if this data source is already initialized.