com.caucho.sql
Class DBPool

java.lang.Object
  |
  +--com.caucho.sql.DBPool

public class DBPool
extends java.lang.Object
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_NAME
          The key used to look into the properties passed to the connect method to find the username.
 
Constructor Summary
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)
           
 
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()
           
 java.sql.Connection connect(java.lang.String url, java.util.Properties info)
          Attempts to make a database connection to the given URL.
 int getActiveConnections()
          Get the total number of connections in use by the program.
 java.sql.Connection getConnection()
          Returns a 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.
 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 getMinorVersion()
          Gets the driver's minor version number.
 java.lang.String getPassword()
          Returns the password for the connection.
static DBPool getPool(java.lang.String name)
          Returns the pool object with the given name.
 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 JDBC url for the connection.
 java.lang.String getUser()
          Returns the user for the connection.
 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 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PROPERTY_USER_NAME

public static final java.lang.String PROPERTY_USER_NAME
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(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
Method Detail

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.
Parameters:
name - name of a database configuration.

getDriverName

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

getDriver

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

getURL

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

getUser

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

getPassword

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

setMaxConnections

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

getMaxConnections

public int getMaxConnections()
Get 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.

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

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

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

getConnection

public java.sql.Connection getConnection()
                                  throws java.sql.SQLException
Returns a pooled connection. It will be returned to the pool when the close method is called.
Returns:
a pooled database connection.

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.

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

close

public void close()