net.sf.jso.sql
Class DynamicPool

java.lang.Object
  extended bynet.sf.jso.sql.DynamicPool
All Implemented Interfaces:
ConnectionPool, ConnectionPoolInfo

public class DynamicPool
extends java.lang.Object
implements ConnectionPool, ConnectionPoolInfo

Manages a dynamic pool of database connections that may grow or shrink according to demand. The growth, or shrinkage, of the pool size is, whenever possible, made by minSize connections, where minSize is the minimum size of the pool.


Field Summary
static int DEFAULT_MAX_SIZE
          Default maximum size for pool.
static int DEFAULT_MIN_SIZE
          Default minimum size for pool. (5 connections)
 
Constructor Summary
DynamicPool()
          Constructs the pool.
DynamicPool(java.io.InputStream aStream)
          Constructs the pool.
DynamicPool(int aMin, int aMax)
          Constructs the pool.
DynamicPool(java.util.Properties properties)
          Constructs the pool.
 
Method Summary
 java.sql.Connection borrowConnection()
          Borrows a connection from the pool.
 int capacity()
          Returns the maximum pool size.
 void close()
          Closes the pool.
protected  void finalize()
          Cleans the pool before it's garbage collected.
 ConnectionPoolInfo getPoolInfo()
          Gets connection pool information.
 int idle()
          Returns the number of idle connections.
 int inUse()
          Returns the number of used connections.
 boolean isOpen()
          Indicates if the pool is open.
 int maxSize()
          Returns the pool maximum size.
 int minSize()
          Returns the pool minimum size.
 void open(java.lang.String anUrl, java.lang.String anUser, java.lang.String aPassword)
          Opens the pool.
 void open(java.lang.String aDriverClass, java.lang.String anUrl, java.lang.String anUser, java.lang.String aPassword)
          Opens the pool.
 void returnConnection(java.sql.Connection aConnection)
          Returns a connection to the pool.
 int size()
          Returns the current pool size.
 java.lang.String url()
          Returns the current database url.
 java.lang.String user()
          Returns the current database user.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_MIN_SIZE

public static final int DEFAULT_MIN_SIZE
Default minimum size for pool. (5 connections)

See Also:
Constant Field Values

DEFAULT_MAX_SIZE

public static final int DEFAULT_MAX_SIZE
Default maximum size for pool. (Integer.MAX_VALUE)

See Also:
Constant Field Values
Constructor Detail

DynamicPool

public DynamicPool()
Constructs the pool. If a property file named net.sf.jso.sql.DynamicPool.properties exists in the current directory or in the classpath it is used.

See Also:
DynamicPool(Properties)

DynamicPool

public DynamicPool(java.io.InputStream aStream)
            throws java.io.IOException
Constructs the pool.

Parameters:
aStream - Input stream of a properties file with parameters.
See Also:
Properties.load(InputStream), DynamicPool(Properties)

DynamicPool

public DynamicPool(java.util.Properties properties)
Constructs the pool.

Parameters:
properties - Parameters for the pool. Properties minSize and maxSize can be used. Those values are used as the pool minimun and maximum size, respectively. Otherwise a default minimun size and a default maximun size are used.

DynamicPool

public DynamicPool(int aMin,
                   int aMax)
            throws java.lang.IllegalArgumentException
Constructs the pool.

Parameters:
aMin - The minimum number of connections the pool will manage.
aMax - The maximum number of connections the pool will manage.
Method Detail

url

public java.lang.String url()
Returns the current database url.

Returns:
The current database url.

user

public java.lang.String user()
Returns the current database user.

Returns:
The current database user.

minSize

public int minSize()
Returns the pool minimum size.

Returns:
The minimum number of connections the pool will manage.

maxSize

public int maxSize()
Returns the pool maximum size.

Returns:
The maximum number of connections the pool will manage.

capacity

public int capacity()
Returns the maximum pool size.

Specified by:
capacity in interface ConnectionPoolInfo
Returns:
The maxmimum number of connections the pool is able to manage.

size

public int size()
Returns the current pool size.

Specified by:
size in interface ConnectionPoolInfo
Returns:
The number of connections the pool is managing.

idle

public int idle()
Returns the number of idle connections.

Specified by:
idle in interface ConnectionPoolInfo
Returns:
The number of idle connections.

inUse

public int inUse()
Returns the number of used connections.

Specified by:
inUse in interface ConnectionPoolInfo
Returns:
The number of used connections.

open

public void open(java.lang.String anUrl,
                 java.lang.String anUser,
                 java.lang.String aPassword)
          throws java.sql.SQLException
Opens the pool.

Specified by:
open in interface ConnectionPool
Parameters:
anUrl - A database url of the form jdbc:subprotocol:subname.
anUser - The database user on whose behalf the connections will be made.
aPassword - The user's password.
Throws:
java.sql.SQLException

open

public void open(java.lang.String aDriverClass,
                 java.lang.String anUrl,
                 java.lang.String anUser,
                 java.lang.String aPassword)
          throws java.sql.SQLException
Opens the pool.

Specified by:
open in interface ConnectionPool
Parameters:
aDriverClass - Fully qualified name for the JDBC driver.
anUrl - A database url of the form jdbc:subprotocol:subname.
anUser - The database user on whose behalf the connections will be made.
aPassword - The user's password.
Throws:
java.sql.SQLException

borrowConnection

public java.sql.Connection borrowConnection()
                                     throws java.sql.SQLException
Borrows a connection from the pool.

Specified by:
borrowConnection in interface ConnectionPool
Returns:
A connection to the database.
Throws:
java.sql.SQLException

returnConnection

public void returnConnection(java.sql.Connection aConnection)
                      throws java.sql.SQLException
Returns a connection to the pool.

Specified by:
returnConnection in interface ConnectionPool
Parameters:
aConnection - A connection previously borrowed from the database.
Throws:
java.sql.SQLException

close

public void close()
           throws java.sql.SQLException
Closes the pool. Closes all open connections, even those not returned to the pool.

Specified by:
close in interface ConnectionPool
Throws:
java.sql.SQLException

isOpen

public boolean isOpen()
Indicates if the pool is open.

Specified by:
isOpen in interface ConnectionPool
Returns:
true if the pool is open; false otherwise.

finalize

protected void finalize()
                 throws java.lang.Throwable
Cleans the pool before it's garbage collected.

Throws:
java.lang.Throwable

getPoolInfo

public ConnectionPoolInfo getPoolInfo()
Gets connection pool information.

Specified by:
getPoolInfo in interface ConnectionPool
Returns:
Information about the connection pool.