com.caucho.sql
Class DBPool

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

public class DBPool
implements AlarmListener, java.sql.Driver

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 style


 DBPool pool = DBPool.getPool("test");
 Connection conn = pool.getConnection();
 try {
   ... // normal connection stuff
 } finally {
   conn.close();
 }
 

Example using Driver style


 Connection conn = DriverManager.getConnection("jdbc:caucho:test");
 try {
   ... // normal connection stuff
 } finally {
   conn.close();
 }
 

Configuration


 <dbpool.sql id='test'
             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. Once the connections run out, DBPool will throw an exception.

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.


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)
          Creates a new DBPool.
 
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.
 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.
 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.
 boolean getEnableTransaction()
          Are transactions allowed on this pool?
 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.
 int getMaxConnections()
          Get the maximum number of pooled connections.
 int getMaxIdleTime()
          Get the time in seconds a connection will remain in the pool before being closed.
 int getMinorVersion()
          Gets the driver's minor version number.
 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.
 int getTotalConnections()
          Get the total number of connections
 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.
 boolean jdbcCompliant()
          Reports whether this driver is a genuine JDBC COMPLIANTTM driver.
 void setDriverName(java.lang.String name)
          Sets the JDBC driver class underlying the pooled object.
 void setEnableTransaction(boolean allowTransaction)
          Enable transactions on this pool.
 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 setMaxConnections(int maxConnections)
          Sets the maximum number of pooled connections.
 void setMaxIdleTime(int maxIdleTime)
          Set the time in seconds a connection will remain in the pool before being closed.
 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 setURL(java.lang.String url)
          Sets the connection's JDBC url.
 void setUser(java.lang.String user)
          Sets the connection's user.
 

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
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

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.

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

getActiveConnections

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

getMaxIdleTime

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

setMaxIdleTime

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

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.

setEnableTransaction

public void setEnableTransaction(boolean allowTransaction)
Enable transactions on this pool.

getEnableTransaction

public boolean getEnableTransaction()
Are transactions allowed on this pool?

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.

close

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

forceClose

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