com.caucho.sql
Class DBPool

com.caucho.sql.DBPool
All Implemented Interfaces:
AlarmListener, ClassLoaderListener, java.sql.Driver, java.util.EventListener, java.io.Serializable

public class DBPool
implements AlarmListener, ClassLoaderListener, java.sql.Driver, java.io.Serializable

Manages a pool of database connections. In addition, DBPool configures the database connection from a configuration file.

Like JDBC 2.0 pooling, DBPool returns a wrapped Connection. Applications can use that connection just like an unpooled connection. It is more important than ever to close() the connection, because the close returns the connection to the connection pool.

Example using DataSource JNDI style (recommended)


 Context env = (Context) new InitialContext().lookup("java:comp/env");
 DataSource pool = (DataSource) env.lookup("jdbc/test");
 Connection conn = pool.getConnection();
 try {
   ... // normal connection stuff
 } finally {
   conn.close();
 }
 

Configuration


 <resource-ref
             res-ref-name='jdbc/test'
             res-ref-type='javax.sql.DataSource'
             driver="postgresql.Driver"
             url="jdbc:postgresql://localhost/test"
             user="ferg"
             password="foobar"/>
 

Pool limits and timeouts

The pool will only allow getMaxConnections() connections alive at a time. If getMaxConnection connections are already active, getPooledConnection will block waiting for an available connection. The wait is timed. If connection-wait-time passes and there is still no connection, getPooledConnection create a new connection anyway.

Connections will only stay in the pool for about 5 seconds. After that they will be removed and closed. This reduces the load on the DB and also protects against the database dropping old connections.

See Also:
Serialized Form

Field Summary
static java.lang.String PROPERTY_PASSWORD
          The key used to look into the properties passed to the connect method to find the password.
static java.lang.String PROPERTY_USER
          The key used to look into the properties passed to the connect method to find the username.
 
Constructor Summary
DBPool()
          Null constructor for the Driver interface; called by the JNDI configuration.
DBPool(java.lang.String poolName, java.lang.String url, java.lang.String user, java.lang.String password, java.lang.String driverName, java.lang.ClassLoader loader, int maxConnections)
          Deprecated.  
 
Method Summary
 boolean acceptsURL(java.lang.String url)
          Returns true if the driver thinks that it can open a connection to the given URL.
 void close()
          Close the pool, closing the connections.
static void closeGlobalPools()
          Closes the global pools.
static void closeLoaderPools()
          Closes the pools in this classloader.
 java.sql.Connection connect(java.lang.String url, java.util.Properties info)
          Attempts to make a database connection to the given URL.
 void forceClose()
          Close all the connections in the pool.
 int getActiveConnections()
          Get the total number of connections in use by the program.
 java.sql.Connection getConnection()
          Returns a new or pooled connection.
 java.sql.Connection getConnection(java.lang.String user, java.lang.String password)
          Return a connection.
 long getConnectionWaitTime()
          Gets the time to wait for a connection when all are used.
 java.lang.String getDataSource()
          Returns the underlying pooled or xa datasource
 java.sql.Driver getDriver()
          Returns the JDBC driver for the pooled object.
 java.lang.String getDriverName()
          Returns the JDBC driver class for the pooled object.
 int getLoginTimeout()
          Gets the timeout for a database login.
 java.io.PrintWriter getLogWriter()
          Sets the debugging log for the connection.
 int getMajorVersion()
          Gets the driver's major version number.
 long getMaxActiveTime()
          Get the time in milliseconds a connection can remain active.
 int getMaxConnections()
          Get the maximum number of pooled connections.
 long getMaxIdleTime()
          Get the time in seconds a connection will remain in the pool before being closed.
 int getMaxOverflowConnections()
          The number of connections to overflow if the connection pool fills and there's a timeout.
 long getMaxPoolTime()
          Get the time in milliseconds a connection will remain in the pool before being closed.
 int getMinorVersion()
          Gets the driver's minor version number.
 java.lang.String getName()
          Returns the Pool's name
 java.lang.String getPassword()
          Returns the connection's password
 boolean getPingOnFree()
          If true, the pool will ping when attempting to free a connection.
 boolean getPingOnIdle()
          If true, the pool will ping in the idle pool.
 boolean getPingOnReuse()
          If true, the pool will ping when attempting to reuse a connection.
 java.lang.String getPingTable()
          Get the table to 'ping' to see if the connection is still live.
static DBPool getPool(java.lang.String name)
          Returns the pool object with the given name.
 java.lang.String getProperty(java.lang.String name)
          Gets a property from the underlying driver.
 java.sql.DriverPropertyInfo[] getPropertyInfo(java.lang.String url, java.util.Properties info)
          Gets information about the possible properties for this driver.
 RegistryNode getRegistry()
          Return registry.
 int getTotalConnections()
          Get the total number of connections
 TransactionManagerImpl getTransactionManager()
          Are transactions allowed on this pool?
 java.lang.String getURL()
          Returns the connection's JDBC url.
 java.lang.String getUser()
          Returns the connection's user.
 void handleAlarm(Alarm alarm)
          At the alarm, close all connections which have been sitting in the pool for too long.
 void handleUnload(DynamicClassLoader loader)
          Callback when the class loader dies.
 boolean jdbcCompliant()
          Reports whether this driver is a genuine JDBC COMPLIANTTM driver.
 void setConnectionWaitTime(long waitTime)
          Sets the time to wait for a connection when all are used.
 void setDataSource(java.lang.String name)
          Returns the underlying pooled or xa datasource
 void setDriver(java.lang.String name)
          DriverName alias for backward compatibility.
 void setDriverName(java.lang.String name)
          Sets the JDBC driver class underlying the pooled object.
 void setLoginTimeout(int seconds)
          Sets the timeout for a database login.
 void setLogWriter(java.io.PrintWriter out)
          Sets the debugging log for the connection.
 void setMaxActiveTime(long maxActiveTime)
          Set the time in milliseconds a connection can remain active.
 void setMaxConnections(int maxConnections)
          Sets the maximum number of pooled connections.
 void setMaxIdleTime(long idleTime)
          Set the time in seconds a connection will remain in the pool before being closed.
 void setMaxOverflowConnections(int maxOverflowConnections)
          The number of connections to overflow if the connection pool fills and there's a timeout.
 void setMaxPoolTime(long maxPoolTime)
          Set the time in milliseconds a connection will remain in the pool before being closed.
 void setName(java.lang.String name)
          Sets the Pool's name.
 void setPassword(java.lang.String password)
          Sets the connection's password
 void setPingOnFree(boolean pingOnFree)
          Set the table to 'ping' to see if the connection is still live.
 void setPingOnIdle(boolean pingOnIdle)
          Set the table to 'ping' to see if the connection is still live.
 void setPingOnReuse(boolean pingOnReuse)
          Set the table to 'ping' to see if the connection is still live.
 void setPingTable(java.lang.String pingTable)
          Set the table to 'ping' to see if the connection is still live.
 void setProperty(java.lang.String name, java.lang.String value)
          Sets a property from the underlying driver.
 void setRegistry(RegistryNode registry)
          Set registry.
 void setTransactionManager(TransactionManagerImpl tm)
          Enable transactions on this pool.
 void setURL(java.lang.String url)
          Sets the connection's JDBC url.
 void setUser(java.lang.String user)
          Sets the connection's user.
 java.lang.String toString()
          Returns a string description of the pool.
 

Field Detail

PROPERTY_USER

public static final java.lang.String PROPERTY_USER
The key used to look into the properties passed to the connect method to find the username.

PROPERTY_PASSWORD

public static final java.lang.String PROPERTY_PASSWORD
The key used to look into the properties passed to the connect method to find the password.
Constructor Detail

DBPool

public DBPool()
Null constructor for the Driver interface; called by the JNDI configuration. Applications should not call this directly.

DBPool

public DBPool(java.lang.String poolName,
              java.lang.String url,
              java.lang.String user,
              java.lang.String password,
              java.lang.String driverName,
              java.lang.ClassLoader loader,
              int maxConnections)
       throws java.sql.SQLException
Deprecated.  

Creates a new DBPool. This is now obsolete and the bean interface should be used instead.
Parameters:
poolName - the name of the pool
url - the JDBC url
user - the JDBC user
password - the JDBC password
driverName - the JDBC driver
loader - the classloader used to load the driver
maxConnections - the maximum connections allowed by the driver
Method Detail

getName

public java.lang.String getName()
Returns the Pool's name

setName

public void setName(java.lang.String name)
Sets the Pool's name. Also puts the pool in the classloader's list of pools.

getDataSource

public java.lang.String getDataSource()
Returns the underlying pooled or xa datasource

setDataSource

public void setDataSource(java.lang.String name)
Returns the underlying pooled or xa datasource

getDriverName

public java.lang.String getDriverName()
Returns the JDBC driver class for the pooled object.

setDriverName

public void setDriverName(java.lang.String name)
Sets the JDBC driver class underlying the pooled object.

setDriver

public void setDriver(java.lang.String name)
DriverName alias for backward compatibility.

getURL

public java.lang.String getURL()
Returns the connection's JDBC url.

setURL

public void setURL(java.lang.String url)
Sets the connection's JDBC url.

getUser

public java.lang.String getUser()
Returns the connection's user.

setUser

public void setUser(java.lang.String user)
Sets the connection's user.

getPassword

public java.lang.String getPassword()
Returns the connection's password

setPassword

public void setPassword(java.lang.String password)
Sets the connection's password

getMaxConnections

public int getMaxConnections()
Get the maximum number of pooled connections.

setMaxConnections

public void setMaxConnections(int maxConnections)
Sets the maximum number of pooled connections.

getTotalConnections

public int getTotalConnections()
Get the total number of connections

setConnectionWaitTime

public void setConnectionWaitTime(long waitTime)
Sets the time to wait for a connection when all are used.

getConnectionWaitTime

public long getConnectionWaitTime()
Gets the time to wait for a connection when all are used.

setMaxOverflowConnections

public void setMaxOverflowConnections(int maxOverflowConnections)
The number of connections to overflow if the connection pool fills and there's a timeout.

getMaxOverflowConnections

public int getMaxOverflowConnections()
The number of connections to overflow if the connection pool fills and there's a timeout.

getActiveConnections

public int getActiveConnections()
Get the total number of connections in use by the program.

getMaxIdleTime

public long getMaxIdleTime()
Get the time in seconds a connection will remain in the pool before being closed.

setMaxIdleTime

public void setMaxIdleTime(long idleTime)
Set the time in seconds a connection will remain in the pool before being closed.

getMaxPoolTime

public long getMaxPoolTime()
Get the time in milliseconds a connection will remain in the pool before being closed.

setMaxPoolTime

public void setMaxPoolTime(long maxPoolTime)
Set the time in milliseconds a connection will remain in the pool before being closed.

getMaxActiveTime

public long getMaxActiveTime()
Get the time in milliseconds a connection can remain active.

setMaxActiveTime

public void setMaxActiveTime(long maxActiveTime)
Set the time in milliseconds a connection can remain active.

getPingTable

public java.lang.String getPingTable()
Get the table to 'ping' to see if the connection is still live.

setPingTable

public void setPingTable(java.lang.String pingTable)
Set the table to 'ping' to see if the connection is still live.
Parameters:
pingTable - name of the SQL table to ping.

getPingOnReuse

public boolean getPingOnReuse()
If true, the pool will ping when attempting to reuse a connection.

setPingOnReuse

public void setPingOnReuse(boolean pingOnReuse)
Set the table to 'ping' to see if the connection is still live.

getPingOnFree

public boolean getPingOnFree()
If true, the pool will ping when attempting to free a connection.

setPingOnFree

public void setPingOnFree(boolean pingOnFree)
Set the table to 'ping' to see if the connection is still live.

getPingOnIdle

public boolean getPingOnIdle()
If true, the pool will ping in the idle pool.

setPingOnIdle

public void setPingOnIdle(boolean pingOnIdle)
Set the table to 'ping' to see if the connection is still live.

setTransactionManager

public void setTransactionManager(TransactionManagerImpl tm)
Enable transactions on this pool.

getTransactionManager

public TransactionManagerImpl getTransactionManager()
Are transactions allowed on this pool?

getRegistry

public RegistryNode getRegistry()
Return registry.

setRegistry

public void setRegistry(RegistryNode registry)
Set registry.

getMajorVersion

public int getMajorVersion()
Gets the driver's major version number. Initially this should be 1.
Specified by:
getMajorVersion in interface java.sql.Driver
Returns:
this driver's major version number

getMinorVersion

public int getMinorVersion()
Gets the driver's minor version number. Initially this should be 0.
Specified by:
getMinorVersion in interface java.sql.Driver
Returns:
this driver's minor version number

getProperty

public java.lang.String getProperty(java.lang.String name)
Gets a property from the underlying driver. Used to set driver properties not handled by DBPool.
Parameters:
name - property name for the driver
Returns:
the driver's value of the property name

setProperty

public void setProperty(java.lang.String name,
                        java.lang.String value)
Sets a property from the underlying driver. Used to set driver properties not handled by DBPool.
Parameters:
name - property name for the driver
value - the driver's value of the property name

getDriver

public java.sql.Driver getDriver()
                          throws java.sql.SQLException
Returns the JDBC driver for the pooled object.

getPropertyInfo

public java.sql.DriverPropertyInfo[] getPropertyInfo(java.lang.String url,
                                                     java.util.Properties info)
                                              throws java.sql.SQLException
Gets information about the possible properties for this driver.

The getPropertyInfo method is intended to allow a generic GUI tool to discover what properties it should prompt a human for in order to get enough information to connect to a database. Note that depending on the values the human has supplied so far, additional values may become necessary, so it may be necessary to iterate though several calls to getPropertyInfo.

Specified by:
getPropertyInfo in interface java.sql.Driver
Parameters:
url - the URL of the database to which to connect
info - a proposed list of tag/value pairs that will be sent on connect open
Returns:
an array of DriverPropertyInfo objects describing possible properties. This array may be an empty array if no properties are required.
Throws:
java.sql.SQLException - if a database access error occurs

acceptsURL

public boolean acceptsURL(java.lang.String url)
                   throws java.sql.SQLException
Returns true if the driver thinks that it can open a connection to the given URL. Typically drivers will return true if they understand the subprotocol specified in the URL and false if they don't.
Specified by:
acceptsURL in interface java.sql.Driver
Parameters:
url - the URL of the database
Returns:
true if this driver can connect to the given URL
Throws:
java.sql.SQLException - if a database access error occurs

connect

public java.sql.Connection connect(java.lang.String url,
                                   java.util.Properties info)
                            throws java.sql.SQLException
Attempts to make a database connection to the given URL. The driver should return "null" if it realizes it is the wrong kind of driver to connect to the given URL. This will be common, as when the JDBC driver manager is asked to connect to a given URL it passes the URL to each loaded driver in turn.

The driver should raise a SQLException if it is the right driver to connect to the given URL, but has trouble connecting to the database.

The java.util.Properties argument can be used to passed arbitrary string tag/value pairs as connection arguments. Normally at least "user" and "password" properties should be included in the Properties.

Specified by:
connect in interface java.sql.Driver
Parameters:
url - the URL of the database to which to connect
info - a list of arbitrary string tag/value pairs as connection arguments. Normally at least a "user" and "password" property should be included.
Returns:
a Connection object that represents a connection to the URL
Throws:
java.sql.SQLException - if a database access error occurs

getPool

public static DBPool getPool(java.lang.String name)
                      throws java.sql.SQLException
Returns the pool object with the given name. Lookup priority: jndi:comp/env, jndi:comp/env/jdbc, static DBPool table.
Parameters:
name - name of a database configuration.

jdbcCompliant

public boolean jdbcCompliant()
Reports whether this driver is a genuine JDBC COMPLIANTTM driver. A driver may only report true here if it passes the JDBC compliance tests; otherwise it is required to return false. JDBC compliance requires full support for the JDBC API and full support for SQL 92 Entry Level. It is expected that JDBC compliant drivers will be available for all the major commercial databases. This method is not intended to encourage the development of non-JDBC compliant drivers, but is a recognition of the fact that some vendors are interested in using the JDBC API and framework for lightweight databases that do not support full database functionality, or for special databases such as document information retrieval where a SQL implementation may not be feasible.
Specified by:
jdbcCompliant in interface java.sql.Driver

setLoginTimeout

public void setLoginTimeout(int seconds)
                     throws java.sql.SQLException
Sets the timeout for a database login.

getLoginTimeout

public int getLoginTimeout()
                    throws java.sql.SQLException
Gets the timeout for a database login.

setLogWriter

public void setLogWriter(java.io.PrintWriter out)
                  throws java.sql.SQLException
Sets the debugging log for the connection.

getLogWriter

public java.io.PrintWriter getLogWriter()
                                 throws java.sql.SQLException
Sets the debugging log for the connection.

getConnection

public java.sql.Connection getConnection()
                                  throws java.sql.SQLException
Returns a new or pooled connection.

getConnection

public java.sql.Connection getConnection(java.lang.String user,
                                         java.lang.String password)
                                  throws java.sql.SQLException
Return a connection. The connection will only be pooled if user and password match the configuration. In general, applications should use the null-argument getConnection().
Parameters:
user - database user
password - database password
Returns:
a database connection

handleAlarm

public void handleAlarm(Alarm alarm)
At the alarm, close all connections which have been sitting in the pool for too long.
Specified by:
handleAlarm in interface AlarmListener
Parameters:
alarm - the alarm event.

closeLoaderPools

public static void closeLoaderPools()
Closes the pools in this classloader.

closeGlobalPools

public static void closeGlobalPools()
Closes the global pools. Only should be called by testing.

handleUnload

public void handleUnload(DynamicClassLoader loader)
Callback when the class loader dies.
Specified by:
handleUnload in interface ClassLoaderListener

close

public void close()
Close the pool, closing the connections.

forceClose

public void forceClose()
Close all the connections in the pool.

toString

public java.lang.String toString()
Returns a string description of the pool.