fedora.server.storage
Class ConnectionPool

java.lang.Object
  extended byfedora.server.storage.ConnectionPool
All Implemented Interfaces:
java.lang.Runnable

public class ConnectionPool
extends java.lang.Object
implements java.lang.Runnable

Title: ConnectionPool.java

Description: A class for preallocating, recycling, and managing JDBC connections.

Taken/adapted from Core Servlets and JavaServer Pages from Prentice Hall and Sun Microsystems Press, http://www.coreservlets.com/ © 2000 Marty Hall; may be freely used or adapted

-----------------------------------------------------------------------------

License and Copyright: The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.mozilla.org/MPL/.

Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License.

The original code is Copyright © 2000 Marty Hall. All rights reserved. The current project homepage for the original code may be found at: http://www.coreservlets.com/.

Portions created for the Fedora Repository System are Copyright © 2002-2004 by The Rector and Visitors of the University of Virginia and Cornell University. All rights reserved."

-----------------------------------------------------------------------------

Version:
$Id: ConnectionPool.java,v 1.11 2005/01/07 07:47:49 cwilper Exp $
Author:
Marty Hall, rlw@virginia.edu, cwilper@cs.cornell.edu

Constructor Summary
ConnectionPool(java.lang.String driver, java.lang.String url, java.lang.String username, java.lang.String password, int initialConnections, int maxConnections, boolean waitIfBusy)
          Constructs a ConnectionPool based on the calling arguments.
ConnectionPool(java.lang.String driver, java.lang.String url, java.lang.String username, java.lang.String password, int initialConnections, int maxConnections, boolean waitIfBusy, DDLConverter ddlConverter)
          Constructs a ConnectionPool that can provide TableCreatingConnections.
 
Method Summary
 void closeAllConnections()
          Closes all the connections.
 void free(java.sql.Connection connection)
          Releases the specified connection and returns it to the active pool.
 java.sql.Connection getConnection()
          Gets the next available connection.
 TableCreatingConnection getTableCreatingConnection()
          Gets a TableCreatingConnection.
 void run()
           
 java.lang.String toString()
          Converts this class object into a meaningful string.
 int totalConnections()
          Provides the total number of connections present including those that are busy and those that are available.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ConnectionPool

public ConnectionPool(java.lang.String driver,
                      java.lang.String url,
                      java.lang.String username,
                      java.lang.String password,
                      int initialConnections,
                      int maxConnections,
                      boolean waitIfBusy)
               throws java.sql.SQLException

Constructs a ConnectionPool based on the calling arguments.

Parameters:
driver - The JDBC driver class name.
url - The JDBC connection URL.
username - The database user name.
password - The database password.
initialConnections - The minimum number of connections possible.
maxConnections - The maximum number of connections possible.
waitIfBusy - Boolean flag that determines whether to wait if there are no more available connections. If set to true, it will wait until a connection becomes available. If set to false, it will throw SQLException when a connection is requested and there are no more available connections.
Throws:
java.sql.SQLException - If the connection pool cannot be established for any reason.

ConnectionPool

public ConnectionPool(java.lang.String driver,
                      java.lang.String url,
                      java.lang.String username,
                      java.lang.String password,
                      int initialConnections,
                      int maxConnections,
                      boolean waitIfBusy,
                      DDLConverter ddlConverter)
               throws java.sql.SQLException
Constructs a ConnectionPool that can provide TableCreatingConnections.

Parameters:
driver - The JDBC driver class name.
url - The JDBC connection URL.
username - The database user name.
password - The he database password.
initialConnections - The minimum number of connections possible.
maxConnections - The maximum number of connections possible.
waitIfBusy - Boolean flag that determines whether to wait if there are no more available connections. If set to true, it will wait until a connection becomes available. If set to false, it will throw SQLException when a connection is requested and there are no more available connections.
ddlConverter - The DDLConverter that the TableCreatingConnections should use when createTable(TableSpec) is called.
Throws:
java.sql.SQLException - If the connection pool cannot be established for any reason.
Method Detail

getTableCreatingConnection

public TableCreatingConnection getTableCreatingConnection()
                                                   throws java.sql.SQLException
Gets a TableCreatingConnection.

This derives from the same pool, but wraps the Connection in an appropriate TableCreatingConnection before returning it.

Returns:
The next available Connection from the pool, wrapped as a TableCreatingException, or null if this ConnectionPool hasn't been configured with a DDLConverter (see constructor).
Throws:
java.sql.SQLException - If there is any propblem in getting the SQL connection.

getConnection

public java.sql.Connection getConnection()
                                  throws java.sql.SQLException

Gets the next available connection.

Returns:
The next available connection.
Throws:
java.sql.SQLException - If the maximum number of connections has been reached or there is some other problem in obtaining the connection.

run

public void run()
Specified by:
run in interface java.lang.Runnable

free

public void free(java.sql.Connection connection)

Releases the specified connection and returns it to the active pool.

Parameters:
connection - A JDBC connection.

totalConnections

public int totalConnections()

Provides the total number of connections present including those that are busy and those that are available.

Returns:
The number of total connections present.

closeAllConnections

public void closeAllConnections()

Closes all the connections. Use with caution: be sure no connections are in use before calling. Note that you are not required to call this when done with a ConnectionPool, since connections are guaranteed to be closed when garbage collected. But this method gives more control regarding when the connections are closed.


toString

public java.lang.String toString()

Converts this class object into a meaningful string.

Returns:
A string describing the connection pool.