com.jfw.database
Class ConnectionPool

java.lang.Object
  extended by com.jfw.database.ConnectionPool
All Implemented Interfaces:
java.lang.Runnable, java.util.Observer

public final class ConnectionPool
extends java.lang.Object
implements java.lang.Runnable, java.util.Observer

Manage a connection pool.
Initialy is opened a number the number of connections defined by key cp_max_connections_db-name of jfw.properties. If more connections are requested then the pool open them when is needed and close them when is released.
For get a conection pool the class ConnectionPoolProxy must be used.
A thread of this class, created in the constructor, is be used for clean connections blocked for a time or closed. The key cp_dbclean_maxtime_db-name of jfw.properties is used for define the max time before to close a blocked connection.
Is an observer of UpdateObservable.


Method Summary
 boolean exit()
          Close all opened connections, permanent and temporary.
protected  void finalize()
          Call super.finalize() and exit().
 java.sql.Connection getConnection()
          Return a free permanent connection.
 boolean isClosed(java.sql.Connection connection)
          Check the status of a connection.
 void releaseConnection(java.sql.Connection thisConnection)
          Release a connection.
 void run()
          The thread that clean the connections.
 java.util.ArrayList status()
          Return detail infos of the connection pool.
 java.lang.String toString()
          Return log informations relative to the connection pool.
 void update(java.util.Observable o, java.lang.Object arg)
          Called by UpdateObservable.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Method Detail

getConnection

public java.sql.Connection getConnection()
                                  throws DatabaseException
Return a free permanent connection. If no permanent connection is free then a new temporaty connection is opened and returned.

Returns:
a connection.
Throws:
DatabaseException

releaseConnection

public void releaseConnection(java.sql.Connection thisConnection)
Release a connection. The permanent connections are released, the temporary connections are closed.

Parameters:
thisConnection - the Connection object to release.

isClosed

public boolean isClosed(java.sql.Connection connection)
Check the status of a connection.

Parameters:
connection - the Connection to check.
Returns:
true if is possible to execute the test query, false in the other cases.

toString

public java.lang.String toString()
Return log informations relative to the connection pool.

Overrides:
toString in class java.lang.Object
Returns:
log informations relative to the connection pool.

status

public java.util.ArrayList status()
Return detail infos of the connection pool.

Returns:
connection pool infos. The infos are saved in an ArraList in this order:
  1. driver
  2. url
  3. username
  4. password
  5. max connections
  6. the toString of connectionHash
  7. the toString of tempConnectionHash
  8. connection infos get it form infoHash

exit

public boolean exit()
Close all opened connections, permanent and temporary. The connections are rollbacked before close.

Returns:
true if no error occur during close, false in other case.

finalize

protected void finalize()
                 throws java.lang.Throwable
Call super.finalize() and exit().

Overrides:
finalize in class java.lang.Object
Throws:
java.lang.Throwable

run

public void run()
The thread that clean the connections.

Specified by:
run in interface java.lang.Runnable

update

public void update(java.util.Observable o,
                   java.lang.Object arg)
Called by UpdateObservable. The initialize() method is called.

Specified by:
update in interface java.util.Observer