org.gjt.tw.dbobjects
Class DbConnectionBroker

java.lang.Object
  |
  +--org.gjt.tw.dbobjects.DbConnectionBroker
All Implemented Interfaces:
java.lang.Runnable

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

DbConnectionBroker A servlet-based broker for database connections. Creates and manages a pool of database connections.


Constructor Summary
DbConnectionBroker(java.lang.String dbDriver, java.lang.String dbServer, java.lang.String dbLogin, java.lang.String dbPassword, int minConns, int maxConns, java.lang.String logFileString, double maxConnTime)
          Creates a new Connection Broker
dbDriver: JDBC driver.
 
Method Summary
 void destroy()
          Less safe shutdown.
 void destroy(int millis)
          Shuts down the housekeeping thread and closes all connections in the pool.
 java.lang.String freeConnection(java.sql.Connection conn)
          Frees a connection.
 long getAge(java.sql.Connection conn)
          Returns the age of a connection -- the time since it was handed out to an application.
 java.sql.Connection getConnection()
          This method hands out the connections in round-robin order.
 int getSize()
          Returns the number of connections in the dynamic pool.
 int getUseCount()
          Returns the number of connections in use.
 int idOfConnection(java.sql.Connection conn)
          Returns the local JDBC ID for a connection.
 void run()
          Housekeeping thread.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DbConnectionBroker

public DbConnectionBroker(java.lang.String dbDriver,
                          java.lang.String dbServer,
                          java.lang.String dbLogin,
                          java.lang.String dbPassword,
                          int minConns,
                          int maxConns,
                          java.lang.String logFileString,
                          double maxConnTime)
                   throws java.io.IOException
Creates a new Connection Broker
dbDriver: JDBC driver. e.g. 'oracle.jdbc.driver.OracleDriver'
dbServer: JDBC connect string. e.g. 'jdbc:oracle:thin:@203.92.21.109:1526:orcl'
dbLogin: Database login name. e.g. 'Scott'
dbPassword: Database password. e.g. 'Tiger'
minConns: Minimum number of connections to start with.
maxConns: Maximum number of connections in dynamic pool.
logFileString: Absolute path name for log file. e.g. 'c:/temp/mylog.log'
maxConnTime: Time in days between connection resets. (Reset does a basic cleanup)
Parameters:
dbDriver - Description of Parameter
dbServer - Description of Parameter
dbLogin - Description of Parameter
dbPassword - Description of Parameter
minConns - Description of Parameter
maxConns - Description of Parameter
logFileString - Description of Parameter
maxConnTime - Description of Parameter
Throws:
java.io.IOException - Description of Exception
Method Detail

getAge

public long getAge(java.sql.Connection conn)
Returns the age of a connection -- the time since it was handed out to an application.
Parameters:
conn - Description of Parameter
Returns:
The age value

getConnection

public java.sql.Connection getConnection()
This method hands out the connections in round-robin order. This prevents a faulty connection from locking up an application entirely. A browser 'refresh' will get the next connection while the faulty connection is cleaned up by the housekeeping thread. If the min number of threads are ever exhausted, new threads are added up the the max thread count. Finally, if all threads are in use, this method waits 2 seconds and tries again, up to ten times. After that, it returns a null.
Returns:
The connection value

getSize

public int getSize()
Returns the number of connections in the dynamic pool.
Returns:
The size value

getUseCount

public int getUseCount()
Returns the number of connections in use.
Returns:
The useCount value

destroy

public void destroy()
Less safe shutdown. Uses default timeout value. This method simply calls the destroy() method with a millis value of 10000 (10 seconds) and ignores SQLException thrown by that method.
See Also:
destroy(int)

destroy

public void destroy(int millis)
             throws java.sql.SQLException
Shuts down the housekeeping thread and closes all connections in the pool. Call this method from the destroy() method of the servlet. Multi-phase shutdown. having following sequence:
  1. getConnection() will refuse to return connections.
  2. The housekeeping thread is shut down.
    Up to the time of millis milliseconds after shutdown of the housekeeping thread, freeConnection() can still be called to return used connections.
  3. After millis milliseconds after the shutdown of the housekeeping thread, all connections in the pool are closed.
  4. If any connections were in use while being closed then a SQLException is thrown.
  5. The log is closed.

Call this method from a servlet destroy() method.
Parameters:
millis - Description of Parameter
Throws:
java.sql.SQLException - Description of Exception

freeConnection

public java.lang.String freeConnection(java.sql.Connection conn)
Frees a connection. Replaces connection back into the main pool for reuse.
Parameters:
conn - Description of Parameter
Returns:
Description of the Returned Value

idOfConnection

public int idOfConnection(java.sql.Connection conn)
Returns the local JDBC ID for a connection.
Parameters:
conn - Description of Parameter
Returns:
Description of the Returned Value

run

public void run()
Housekeeping thread. Runs in the background with low CPU overhead. Connections are checked for warnings and closure and are periodically restarted. This thread is a catchall for corrupted connections and prevents the buildup of open cursors. (Open cursors result when the application fails to close a Statement). This method acts as fault tolerance for bad connection/statement programming.
Specified by:
run in interface java.lang.Runnable