com.knowgate.jdc
Class JDCConnectionPool

java.lang.Object
  extended by com.knowgate.jdc.JDCConnectionPool
All Implemented Interfaces:
javax.sql.CommonDataSource, javax.sql.ConnectionPoolDataSource

public final class JDCConnectionPool
extends java.lang.Object
implements javax.sql.ConnectionPoolDataSource

JDBC Connection Pool

Implementation of a standard JDBC connection pool.


Constructor Summary
JDCConnectionPool(java.lang.Object bind, java.lang.String url, java.lang.String user, java.lang.String password, int maxpoolsize, int maxconnections)
          Constructor This method sets a default login timeout of 20 seconds
JDCConnectionPool(java.lang.Object bind, java.lang.String url, java.lang.String user, java.lang.String password, int maxpoolsize, int maxconnections, int logintimeout)
          Constructor This method sets a default login timeout of 20 seconds
JDCConnectionPool(java.lang.Object bind, java.lang.String url, java.lang.String user, java.lang.String password, int maxpoolsize, int maxconnections, int logintimeout, long connectiontimeout)
          Constructor This method sets a default login timeout of 20 seconds
JDCConnectionPool(java.lang.String url, java.lang.String user, java.lang.String password)
          Constructor By default, maximum pool size is set to 32, maximum opened connections is 100, login timeout is 20 seconds, connection timeout is 5 minutes.
JDCConnectionPool(java.lang.String url, java.lang.String user, java.lang.String password, int maxpoolsize, int maxconnections)
          Constructor This method sets a default login timeout of 20 seconds
JDCConnectionPool(java.lang.String url, java.lang.String user, java.lang.String password, int maxpoolsize, int maxconnections, int logintimeout)
          Constructor
 
Method Summary
 void close()
          Close all connections and stop connection reaper
 void closeConnections()
          Close all connections from the pool regardless of their current state
 int closeStaledConnections()
          Close connections from the pool not used for a longer time
 java.lang.String dumpStatistics()
          Human readable usage statistics
 JDCActivityInfo getActivityInfo()
          Get information of current activity at database to which this pool is connected
 JDCConnection[] getAllConnections()
          Get an array with references to all pooled connections
 JDCConnection getConnection(java.lang.String sCaller)
          Get a connection from the pool
 JDCConnection getConnectionForPId(java.lang.String sPId)
          Get conenction for a server process identifier
 java.lang.Object getDatabaseBinding()
          Get the DBbind object owner of this conenction pool
 int getLoginTimeout()
          Get login timeout from java.sql.DriverManager
 java.io.PrintWriter getLogWriter()
          Get LogWriter from java.sql.DriverManager
 int getMaxPoolSize()
          Absolute maximum allowed for concurrent opened connections.
 javax.sql.PooledConnection getPooledConnection()
          Get a connection from the pool
 javax.sql.PooledConnection getPooledConnection(java.lang.String sUser, java.lang.String sPasswd)
          Get a connection bypassing the pool and connection directly to the database with the given user and password
 int getPoolSize()
          Get prefered open connections limit Additional connections beyond PoolSize may be opened but they will closed inmediately after use and not pooled.
 long getReaperDaemonDelay()
          Delay betwwen connection reaper executions
 long getTimeout()
          Get staled connection threshold The default value is 600000ms (10 mins.)
 void returnConnection(JDCConnection conn)
          Return a connection to the pool
 void returnConnection(JDCConnection conn, java.lang.String sCaller)
          Return a connection to the pool
 void setLoginTimeout(int seconds)
          Set login timeout for java.sql.DriverManager
 void setLogWriter(java.io.PrintWriter printwrt)
          Set LogWriter for java.sql.DriverManager
 void setMaxPoolSize(int iMaxConnections)
          Set maximum concurrent open connections limit The default value is 100.
 void setPoolSize(int iPoolSize)
          Set prefered open connections limit Additional connections beyond PoolSize may be opened but they will closed inmediately after use and not pooled.
 void setReaperDaemonDelay(long lDelayMs)
          Set delay betwwen connection reaper executions (default value is 5 mins)
 void setTimeout(long miliseconds)
          Set staled connection threshold
 int size()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JDCConnectionPool

public JDCConnectionPool(java.lang.String url,
                         java.lang.String user,
                         java.lang.String password)
Constructor By default, maximum pool size is set to 32, maximum opened connections is 100, login timeout is 20 seconds, connection timeout is 5 minutes.

Parameters:
url - JDBC URL string
user - Database user
password - Password for user

JDCConnectionPool

public JDCConnectionPool(java.lang.Object bind,
                         java.lang.String url,
                         java.lang.String user,
                         java.lang.String password,
                         int maxpoolsize,
                         int maxconnections,
                         int logintimeout,
                         long connectiontimeout)

Constructor

This method sets a default login timeout of 20 seconds

Parameters:
bind - DBBind owner of the connection pool (may be null)
url - JDBC URL string
user - Database user
password - Password for user
maxpoolsize - Maximum pool size
maxconnections - Maximum opened connections
connectiontimeout - Maximum time that a
logintimeout - Maximum time, in seconds, to wait for connection
Since:
v2.2

JDCConnectionPool

public JDCConnectionPool(java.lang.Object bind,
                         java.lang.String url,
                         java.lang.String user,
                         java.lang.String password,
                         int maxpoolsize,
                         int maxconnections,
                         int logintimeout)

Constructor

This method sets a default login timeout of 20 seconds

Parameters:
bind - DBBind owner of the connection pool (may be null)
url - JDBC URL string
user - Database user
password - Password for user
maxpoolsize - Maximum pool size
maxconnections - Maximum opened connections
logintimeout - Maximum time, in seconds, to wait for connection
Since:
v2.2

JDCConnectionPool

public JDCConnectionPool(java.lang.Object bind,
                         java.lang.String url,
                         java.lang.String user,
                         java.lang.String password,
                         int maxpoolsize,
                         int maxconnections)

Constructor

This method sets a default login timeout of 20 seconds

Parameters:
bind - DBBind owner of the connection pool (may be null)
url - JDBC URL string
user - Database user
password - Password for user
maxpoolsize - Maximum pool size (Default 32)
maxconnections - Maximum opened connections (Default 100)

JDCConnectionPool

public JDCConnectionPool(java.lang.String url,
                         java.lang.String user,
                         java.lang.String password,
                         int maxpoolsize,
                         int maxconnections,
                         int logintimeout)

Constructor

Parameters:
url - JDBC URL string
user - Database user
password - Password for user
maxpoolsize - Maximum pool size (Default 32)
maxconnections - Maximum opened connections (Default 100)
logintimeout - Maximum time, in seconds, to wait for connection
Since:
v2.2

JDCConnectionPool

public JDCConnectionPool(java.lang.String url,
                         java.lang.String user,
                         java.lang.String password,
                         int maxpoolsize,
                         int maxconnections)

Constructor

This method sets a default login timeout of 20 seconds

Parameters:
url - JDBC URL string
user - Database user
password - Password for user
maxpoolsize - Maximum pool size (Default 32)
maxconnections - Maximum opened connections (Default 100)
Method Detail

close

public void close()
Close all connections and stop connection reaper


getPoolSize

public int getPoolSize()

Get prefered open connections limit

Additional connections beyond PoolSize may be opened but they will closed inmediately after use and not pooled.
The default value is 32.

Returns:
open connections soft limit

setPoolSize

public void setPoolSize(int iPoolSize)

Set prefered open connections limit

Additional connections beyond PoolSize may be opened but they will closed inmediately after use and not pooled.
The default value is 32.
Connections not being used can only be in the pool for a maximum of five minutes.
After a connection is not used for over 5 minutes it will be closed so the actual pool size will eventually go down to zero after a period of inactivity.

Parameters:
iPoolSize - Maximum pooled connections

setMaxPoolSize

public void setMaxPoolSize(int iMaxConnections)

Set maximum concurrent open connections limit

The default value is 100.
If iMaxConnections is set to zero then the connection pool is effectively turned off and no pooling occurs.

Parameters:
iMaxConnections - Absolute maximum for opened connections

getMaxPoolSize

public int getMaxPoolSize()

Absolute maximum allowed for concurrent opened connections.

The default value is 100.


getLogWriter

public java.io.PrintWriter getLogWriter()
                                 throws java.sql.SQLException
Get LogWriter from java.sql.DriverManager

Specified by:
getLogWriter in interface javax.sql.CommonDataSource
Throws:
java.sql.SQLException
Since:
4.0

setLogWriter

public void setLogWriter(java.io.PrintWriter printwrt)
                  throws java.sql.SQLException
Set LogWriter for java.sql.DriverManager

Specified by:
setLogWriter in interface javax.sql.CommonDataSource
Throws:
java.sql.SQLException
Since:
4.0

getLoginTimeout

public int getLoginTimeout()
                    throws java.sql.SQLException
Get login timeout from java.sql.DriverManager

Specified by:
getLoginTimeout in interface javax.sql.CommonDataSource
Throws:
java.sql.SQLException
Since:
4.0

setLoginTimeout

public void setLoginTimeout(int seconds)
                     throws java.sql.SQLException
Set login timeout for java.sql.DriverManager

Specified by:
setLoginTimeout in interface javax.sql.CommonDataSource
Throws:
java.sql.SQLException
Since:
4.0

getTimeout

public long getTimeout()

Get staled connection threshold

The default value is 600000ms (10 mins.)
This implies that all database operations done using connections obtained from the pool must be completed before 10 minutes or else they can be closed by the connection reaper before their normal finish.

Returns:
The maximum amount of time in miliseconds that a JDCConnection can be opened and not used before considering it staled.

setTimeout

public void setTimeout(long miliseconds)
                throws java.lang.IllegalArgumentException

Set staled connection threshold

Parameters:
miliseconds - The maximum amount of time in miliseconds that a JDCConnection can be opened and not used before considering it staled.
Default value is 600000ms (10 mins.) Minimum value is 1000.
Throws:
java.lang.IllegalArgumentException - If miliseconds<1000

getReaperDaemonDelay

public long getReaperDaemonDelay()
Delay betwwen connection reaper executions

Returns:
long Number of miliseconds
Since:
3.0

setReaperDaemonDelay

public void setReaperDaemonDelay(long lDelayMs)
                          throws java.lang.IllegalArgumentException
Set delay betwwen connection reaper executions (default value is 5 mins)

Parameters:
lDelayMs - long Miliseconds
Throws:
java.lang.IllegalArgumentException - if lDelayMs is less than 1000
Since:
3.0

closeConnections

public void closeConnections()
Close all connections from the pool regardless of their current state


closeStaledConnections

public int closeStaledConnections()
Close connections from the pool not used for a longer time

Returns:
Count of staled connections closed

getAllConnections

public JDCConnection[] getAllConnections()
Get an array with references to all pooled connections


getDatabaseBinding

public java.lang.Object getDatabaseBinding()
Get the DBbind object owner of this conenction pool

Returns:
DBBind instance or null if this connection pool has no owner

getConnection

public JDCConnection getConnection(java.lang.String sCaller)
                            throws java.sql.SQLException
Get a connection from the pool

Parameters:
sCaller - This is just an information parameter used for open/closed mismatch tracking and other benchmarking and statistical purposes.
Returns:
Opened JDCConnection
Throws:
java.sql.SQLException - If getMaxPoolSize() opened connections is reached an SQLException with SQLState="08004" will be raised upon calling getConnection().
Microsoft SQL Server: Connection reuse requires that SelectMethod=cursor was specified at connection string.

getPooledConnection

public javax.sql.PooledConnection getPooledConnection()
                                               throws java.sql.SQLException
Get a connection from the pool

Specified by:
getPooledConnection in interface javax.sql.ConnectionPoolDataSource
Returns:
Opened PooledConnection
Throws:
java.sql.SQLException - If getMaxPoolSize() opened connections is reached an SQLException with SQLState="08004" will be raised upon calling getConnection().
Microsoft SQL Server: Connection reuse requires that SelectMethod=cursor was specified at connection string.
Since:
4.0

getPooledConnection

public javax.sql.PooledConnection getPooledConnection(java.lang.String sUser,
                                                      java.lang.String sPasswd)
                                               throws java.sql.SQLException
Get a connection bypassing the pool and connection directly to the database with the given user and password

Specified by:
getPooledConnection in interface javax.sql.ConnectionPoolDataSource
Parameters:
sUser -
sPasswd -
Returns:
Opened Connection
Throws:
java.sql.SQLException
Since:
4.0

getConnectionForPId

public JDCConnection getConnectionForPId(java.lang.String sPId)
                                  throws java.sql.SQLException
Get conenction for a server process identifier

Parameters:
sPId - String Operating system process identifier at server side
Returns:
JDCConnection or null if no connection for such pid was found
Throws:
java.sql.SQLException
Since:
2.2

returnConnection

public void returnConnection(JDCConnection conn)
Return a connection to the pool

Parameters:
conn - JDCConnection returned to the pool

returnConnection

public void returnConnection(JDCConnection conn,
                             java.lang.String sCaller)
Return a connection to the pool

Parameters:
conn - JDCConnection returned to the pool
sCaller - Must be the same String passed as parameter at getConnection()

size

public int size()
Returns:
Actual connection pool size

getActivityInfo

public JDCActivityInfo getActivityInfo()
                                throws java.sql.SQLException
Get information of current activity at database to which this pool is connected

Returns:
JDCActivityInfo
Throws:
java.sql.SQLException
Since:
3.0

dumpStatistics

public java.lang.String dumpStatistics()
                                throws java.util.ConcurrentModificationException
Human readable usage statistics

Returns:
Connection pool usage statistics string
Throws:
java.util.ConcurrentModificationException - If pool is modified while iterating throught connection collection