org.opensha.refFaultParamDb.dao.db
Class DB_ConnectionPool

java.lang.Object
  extended by org.opensha.refFaultParamDb.dao.db.DB_ConnectionPool
All Implemented Interfaces:
Runnable, DB_AccessAPI

public class DB_ConnectionPool
extends Object
implements Runnable, DB_AccessAPI

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

Version:
1.0.13 3/12/02
Author:
Marc A. Mnich modified by Vipin Gupta , Nitin Gupta

Field Summary
static String db_prop_2_file
           
static String db_prop_3_ro_file
           
 
Fields inherited from interface org.opensha.refFaultParamDb.dao.db.DB_AccessAPI
INSERT_UPDATE_QUERY, INSERT_UPDATE_SPATIAL, RESET_PASSWORD, SELECT_QUERY, SELECT_QUERY_SPATIAL, SEQUENCE_NUMBER
 
Constructor Summary
DB_ConnectionPool()
           
DB_ConnectionPool(String resourceName)
          Class default constructor Creates a new Connection Broker after reading the JDBC info from the data file.
DB_ConnectionPool(String resourceName, String login, String pass)
          Class default constructor Creates a new Connection Broker after reading the JDBC info from the data file, except with the specified username and password.
DB_ConnectionPool(String dbDriver, String dbServer, String dbLogin, String dbPassword, int minConns, int maxConns, String logFileString, double maxConnTime)
          Creates a new Connection Broker
dbDriver: JDBC driver.
DB_ConnectionPool(String dbDriver, String dbServer, String dbLogin, String dbPassword, int minConns, int maxConns, String logFileString, double maxConnTime, boolean logAppend)
           
DB_ConnectionPool(String dbDriver, String dbServer, String dbLogin, String dbPassword, int minConns, int maxConns, String logFileString, double maxConnTime, boolean logAppend, int maxCheckoutSeconds, int debugLevel)
           
 
Method Summary
static boolean authenticateDBConnection(boolean exitOnCancel, boolean allowReadOnly)
           
 void destroy()
          Less safe shutdown.
 void destroy(int millis)
          Multi-phase shutdown.
 String freeConnection(Connection conn)
          Frees a connection.
 long getAge(Connection conn)
          Returns the age of a connection -- the time since it was handed out to an application.
 Connection getConnection()
          This method hands out the connections in round-robin order.
static DB_AccessAPI getDB2ReadOnlyConn()
          Gets a read only connection to Fault DB version 2
static DB_AccessAPI getDB2ReadWriteConn()
          Gets a read/write capable connection to Fault DB version 2
static DB_AccessAPI getDB3ReadOnlyConn()
          Gets a read only connection to Fault DB version 3
static DB_AccessAPI getDB3ReadWriteConn()
          Gets a read/write capable connection to Fault DB version 3
static DB_AccessAPI getDirectLatestReadWriteConnection()
           
static DB_AccessAPI getLatestReadOnlyConn()
          Gets a read only connection to the latest development version of the fault database
static DB_AccessAPI getLatestReadWriteConn()
          Gets a read/write capable connection to the latest development version of the fault database
 int getNextSequenceNumber(String sequenceName)
          Gets the Unique sequence number from Database, so that it can inserted as auto increment number in the data tables.
 int getSize()
          Returns the number of connections in the dynamic pool.
 String getSystemDate()
          Get the system date
 int getUseCount()
          Returns the number of connections in use.
 int idOfConnection(Connection conn)
          Returns the local JDBC ID for a connection.
 int[] insertUpdateOrDeleteBatch(ArrayList<String> sqls, boolean rollbackOnFail)
          Executes a batch of sql statements.
 int insertUpdateOrDeleteData(String sql)
          Inserts the data into the database
 int insertUpdateOrDeleteData(String sql, ArrayList<oracle.spatial.geometry.JGeometry> geometryList)
          Insert/Update/Delete record in the database.
 oracle.jdbc.rowset.OracleCachedRowSet queryData(String sql)
          Runs the select query on the database
 SpatialQueryResult queryData(String sqlWithSpatialColumnNames, String sqlWithNoSpatialColumnNames, ArrayList<String> spatialColumnNames)
          Query the databse and returns the Results in a object which contains CachedRowSet as well as JGeomtery objects.
 int resetPasswordByEmail(String sql)
          Reset the password in the database for the provided email address
 void run()
          Housekeeping thread.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

db_prop_3_ro_file

public static final String db_prop_3_ro_file
See Also:
Constant Field Values

db_prop_2_file

public static final String db_prop_2_file
See Also:
Constant Field Values
Constructor Detail

DB_ConnectionPool

public DB_ConnectionPool()

DB_ConnectionPool

public DB_ConnectionPool(String resourceName)
Class default constructor Creates a new Connection Broker after reading the JDBC info from the data file.


DB_ConnectionPool

public DB_ConnectionPool(String resourceName,
                         String login,
                         String pass)
Class default constructor Creates a new Connection Broker after reading the JDBC info from the data file, except with the specified username and password.


DB_ConnectionPool

public DB_ConnectionPool(String dbDriver,
                         String dbServer,
                         String dbLogin,
                         String dbPassword,
                         int minConns,
                         int maxConns,
                         String logFileString,
                         double maxConnTime)
                  throws 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)
logAppend: Append to logfile (optional)
maxCheckoutSeconds: Max time a connection can be checked out before being recycled. Zero value turns option off, default is 60 seconds. debugLevel: Level of debug messages output to the log file. 0 -> no messages, 1 -> Errors, 2 -> Warnings, 3 -> Information

Throws:
IOException

DB_ConnectionPool

public DB_ConnectionPool(String dbDriver,
                         String dbServer,
                         String dbLogin,
                         String dbPassword,
                         int minConns,
                         int maxConns,
                         String logFileString,
                         double maxConnTime,
                         boolean logAppend)
                  throws IOException
Throws:
IOException

DB_ConnectionPool

public DB_ConnectionPool(String dbDriver,
                         String dbServer,
                         String dbLogin,
                         String dbPassword,
                         int minConns,
                         int maxConns,
                         String logFileString,
                         double maxConnTime,
                         boolean logAppend,
                         int maxCheckoutSeconds,
                         int debugLevel)
                  throws IOException
Throws:
IOException
Method Detail

getDB2ReadOnlyConn

public static DB_AccessAPI getDB2ReadOnlyConn()
Gets a read only connection to Fault DB version 2

Returns:

getDB2ReadWriteConn

public static DB_AccessAPI getDB2ReadWriteConn()
Gets a read/write capable connection to Fault DB version 2

Returns:

getDB3ReadOnlyConn

public static DB_AccessAPI getDB3ReadOnlyConn()
Gets a read only connection to Fault DB version 3

Returns:

getDB3ReadWriteConn

public static DB_AccessAPI getDB3ReadWriteConn()
Gets a read/write capable connection to Fault DB version 3

Returns:

authenticateDBConnection

public static boolean authenticateDBConnection(boolean exitOnCancel,
                                               boolean allowReadOnly)

getLatestReadOnlyConn

public static DB_AccessAPI getLatestReadOnlyConn()
Gets a read only connection to the latest development version of the fault database

Returns:

getLatestReadWriteConn

public static DB_AccessAPI getLatestReadWriteConn()
Gets a read/write capable connection to the latest development version of the fault database

Returns:

getDirectLatestReadWriteConnection

public static DB_AccessAPI getDirectLatestReadWriteConnection()

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 Runnable

getConnection

public 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.


idOfConnection

public int idOfConnection(Connection conn)
Returns the local JDBC ID for a connection.


freeConnection

public String freeConnection(Connection conn)
Frees a connection. Replaces connection back into the main pool for reuse.


getAge

public long getAge(Connection conn)
Returns the age of a connection -- the time since it was handed out to an application.


destroy

public void destroy(int millis)
             throws SQLException
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.

Specified by:
destroy in interface DB_AccessAPI
Parameters:
millis - the time to wait in milliseconds.
Throws:
SQLException - if connections were in use after millis.

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.

Specified by:
destroy in interface DB_AccessAPI
See Also:
destroy(int)

getUseCount

public int getUseCount()
Returns the number of connections in use.


getSize

public int getSize()
Returns the number of connections in the dynamic pool.


resetPasswordByEmail

public int resetPasswordByEmail(String sql)
                         throws SQLException
Reset the password in the database for the provided email address

Specified by:
resetPasswordByEmail in interface DB_AccessAPI
Parameters:
sql -
email -
Returns:
Throws:
SQLException

insertUpdateOrDeleteData

public int insertUpdateOrDeleteData(String sql)
                             throws SQLException
Inserts the data into the database

Specified by:
insertUpdateOrDeleteData in interface DB_AccessAPI
Parameters:
query -
Returns:
int
Throws:
SQLException

insertUpdateOrDeleteBatch

public int[] insertUpdateOrDeleteBatch(ArrayList<String> sqls,
                                       boolean rollbackOnFail)
                                throws SQLException
Description copied from interface: DB_AccessAPI
Executes a batch of sql statements. if any fail, then you can optionally roll back any changes if rollbackOnFail is true.

Specified by:
insertUpdateOrDeleteBatch in interface DB_AccessAPI
Returns:
array of update counts, or null if update failed
Throws:
SQLException

getSystemDate

public String getSystemDate()
                     throws SQLException
Get the system date

Specified by:
getSystemDate in interface DB_AccessAPI
Returns:
Throws:
SQLException

getNextSequenceNumber

public int getNextSequenceNumber(String sequenceName)
                          throws SQLException
Gets the Unique sequence number from Database, so that it can inserted as auto increment number in the data tables.

Specified by:
getNextSequenceNumber in interface DB_AccessAPI
Parameters:
sequenceName - String
Returns:
int
Throws:
SQLException

queryData

public oracle.jdbc.rowset.OracleCachedRowSet queryData(String sql)
                                                throws SQLException
Runs the select query on the database

Specified by:
queryData in interface DB_AccessAPI
Parameters:
query -
Returns:
Throws:
SQLException

queryData

public SpatialQueryResult queryData(String sqlWithSpatialColumnNames,
                                    String sqlWithNoSpatialColumnNames,
                                    ArrayList<String> spatialColumnNames)
                             throws SQLException
Query the databse and returns the Results in a object which contains CachedRowSet as well as JGeomtery objects.

Specified by:
queryData in interface DB_AccessAPI
Parameters:
sql - String
Returns:
OracleCachedRowSet
Throws:
SQLException

insertUpdateOrDeleteData

public int insertUpdateOrDeleteData(String sql,
                                    ArrayList<oracle.spatial.geometry.JGeometry> geometryList)
                             throws SQLException
Insert/Update/Delete record in the database. This method should be used when one of the columns in the database is a spatial column

Specified by:
insertUpdateOrDeleteData in interface DB_AccessAPI
Parameters:
sql - String
Returns:
int
Throws:
SQLException