org.liquidsite.util.db
Class DatabaseConnectionPool

java.lang.Object
  extended byorg.liquidsite.util.db.DatabaseConnectionPool

public class DatabaseConnectionPool
extends java.lang.Object

A database connection pool.


Constructor Summary
DatabaseConnectionPool(DatabaseConnector db)
          Creates a new database connection pool.
 
Method Summary
 DatabaseConnection getConnection()
          Returns a database connection from the pool.
 int getCurrentSize()
          Returns the current connection pool size.
 int getMaximumSize()
          Returns the maximum connection pool size.
 int getMinimumSize()
          Returns the minium connection pool size.
 void returnConnection(DatabaseConnection con)
          Returns a database connection to the pool.
 void setMaximumSize(int size)
          Sets the maximum connection pool size.
 void setMinimumSize(int size)
          Sets the minimum connection pool size.
 void update()
          Updates the connection pool.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DatabaseConnectionPool

public DatabaseConnectionPool(DatabaseConnector db)
Creates a new database connection pool. The JDBC driver should have been loaded prior to calling this constructor, as no database connections can be created otherwise.

Parameters:
db - the database connector to use
Method Detail

getCurrentSize

public int getCurrentSize()
Returns the current connection pool size. This method is synchronized to guarantee that no concurrent operation is being made to the connection list.

Returns:
the current connection pool size

getMinimumSize

public int getMinimumSize()
Returns the minium connection pool size. By default the connection pool minimum size is zero (0).

Returns:
the minimum connection pool size
See Also:
setMinimumSize(int)

setMinimumSize

public void setMinimumSize(int size)
Sets the minimum connection pool size. This method will not create new database connections, but only register the new minimum count.

Parameters:
size - the new minimum pool size
See Also:
getMinimumSize(), update()

getMaximumSize

public int getMaximumSize()
Returns the maximum connection pool size. By default the connection pool has no maximum size.

Returns:
the maximum connection pool size, or a negative value for unlimited
See Also:
setMaximumSize(int)

setMaximumSize

public void setMaximumSize(int size)
Sets the maximum connection pool size. This method will not close any previously open database connections, but only register the new maximum count.

Parameters:
size - the new maximum pool size, or a negative value for unlimited
See Also:
getMaximumSize(), update()

getConnection

public DatabaseConnection getConnection()
                                 throws DatabaseConnectionException
Returns a database connection from the pool. If there is none available, a new connection will be created.

Returns:
the database connection
Throws:
DatabaseConnectionException - if a new database connection couldn't be created
See Also:
returnConnection(org.liquidsite.util.db.DatabaseConnection)

returnConnection

public void returnConnection(DatabaseConnection con)
Returns a database connection to the pool. If the connection isn't already in the pool, nothing happens.

Parameters:
con - the database connection
See Also:
getConnection()

update

public void update()
            throws DatabaseConnectionException
Updates the connection pool. This method will step through all available database connections in the pool, removing all broken or timed out connections. The connection pool size may also be adjusted to fit in between the minimum and maximum sizes. Note that any call to this method should be made from a background thread, as this method may get stuck waiting for I/O timeouts.

Throws:
DatabaseConnectionException - if new database connections couldn't be created