sophware.sql
Class ConnectionPool

java.lang.Object
  extended bysophware.sql.ConnectionPool

public class ConnectionPool
extends java.lang.Object

ConnectionPool maintains a pool of database connetions to the given database URL. Connections are extracted using the getConnection() method and returned using returnConnection(). Connections that timeout are automatically retired by the pool. All connections are guaranteed to be in their default state when extracted.

It is assumed that all drivers were loaded prior to the first call to getConnection();


Constructor Summary
ConnectionPool(java.lang.String URL, java.lang.String user, java.lang.String password, int maxConn)
          Creates new connection pool.
 
Method Summary
 void dispose()
          Closes all available connections.
 java.sql.Connection getConnection()
          Checks out a connection from the pool.
 java.sql.Connection getConnection(long timeout)
          Checks out a connection from the pool.
 int getIdleCount()
          Get the number of connections currently idle
 int getSize()
          Get the number of connections in the pool
 void returnConnection(java.sql.Connection con)
          Checks in a connection to the pool.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ConnectionPool

public ConnectionPool(java.lang.String URL,
                      java.lang.String user,
                      java.lang.String password,
                      int maxConn)
Creates new connection pool.

Parameters:
URL - The JDBC URL for the database
user - The database user, or null
password - The database user password, or null
maxConn - The maximal number of connections, or 0 for no limit
Method Detail

getConnection

public java.sql.Connection getConnection()
                                  throws java.sql.SQLException
Checks out a connection from the pool. If no free connection is available, a new connection is created. If the max number of connections has been reached null is returned.

Throws:
java.sql.SQLException

getConnection

public java.sql.Connection getConnection(long timeout)
                                  throws java.sql.SQLException
Checks out a connection from the pool. If no free connection is available, a new connection is created unless the max number of connections has been reached.

If no connection is available and the max number has been reached, this method waits the specified time for one to be checked in. A timeout value of zero waits indefinitely

Parameters:
timeout - The timeout value in milliseconds
Throws:
java.sql.SQLException

returnConnection

public void returnConnection(java.sql.Connection con)
Checks in a connection to the pool. Notify other Threads that may be waiting for a connection.

Parameters:
con - The connection to check in

getIdleCount

public int getIdleCount()
Get the number of connections currently idle

Returns:
idle connections

getSize

public int getSize()
Get the number of connections in the pool

Returns:
total connections (active and idle) in the pool

dispose

public void dispose()
Closes all available connections.