Package org.conical.common.bbl.db
Class DbConnectionBroker
java.lang.Object
org.conical.common.bbl.db.DbConnectionBroker
- All Implemented Interfaces:
Runnable,Wrapper,CommonDataSource,DataSource
Pooled Data Source
Creates and manages a pool of database connections.
Modified by Ryan Doherty to implement javax.sql.DataSource, from an original work by Marc Mnich
- Version:
- 1.0.13 3/12/02
- Author:
- Marc A. Mnich, Ryan R. Doherty
-
Constructor Summary
ConstructorsConstructorDescriptionDbConnectionBroker(String dbDriver, String dbServer, String dbLogin, String dbPassword, int minConns, int maxConns, double maxConnTime) Creates a new Connection Broker
dbDriver: JDBC driver.DbConnectionBroker(String dbDriver, String dbServer, String dbLogin, String dbPassword, int minConns, int maxConns, double maxConnTime, boolean logAppend) DbConnectionBroker(String dbDriver, String dbServer, String dbLogin, String dbPassword, int minConns, int maxConns, double maxConnTime, boolean logAppend, int maxCheckoutSeconds, int debugLevel) -
Method Summary
Modifier and TypeMethodDescriptionvoiddestroy()Less safe shutdown.voiddestroy(int millis) Multi-phase shutdown.freeConnection(Connection conn) Frees a connection.longgetAge(Connection conn) Returns the age of a connection -- the time since it was handed out to an application.This method hands out the connections in round-robin order.getConnection(String username, String password) Ignores the username and password passed in.intintgetSize()Returns the number of connections in the dynamic pool.booleanisWrapperFor(Class<?> iface) voidrun()Housekeeping thread.voidsetLoginTimeout(int seconds) voidsetLogWriter(PrintWriter out) <T> TMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface javax.sql.CommonDataSource
createShardingKeyBuilderMethods inherited from interface javax.sql.DataSource
createConnectionBuilder
-
Constructor Details
-
DbConnectionBroker
public DbConnectionBroker(String dbDriver, String dbServer, String dbLogin, String dbPassword, int minConns, int maxConns, 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
-
DbConnectionBroker
public DbConnectionBroker(String dbDriver, String dbServer, String dbLogin, String dbPassword, int minConns, int maxConns, double maxConnTime, boolean logAppend) throws IOException - Throws:
IOException
-
DbConnectionBroker
public DbConnectionBroker(String dbDriver, String dbServer, String dbLogin, String dbPassword, int minConns, int maxConns, double maxConnTime, boolean logAppend, int maxCheckoutSeconds, int debugLevel) throws IOException - Throws:
IOException
-
-
Method Details
-
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. -
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.- Specified by:
getConnectionin interfaceDataSource
-
freeConnection
Frees a connection. Replaces connection back into the main pool for reuse. -
getAge
Returns the age of a connection -- the time since it was handed out to an application. -
destroy
Multi-phase shutdown. having following sequence:getConnection()will refuse to return connections.- The housekeeping thread is shut down.
Up to the time ofmillismilliseconds after shutdown of the housekeeping thread,freeConnection()can still be called to return used connections. - After
millismilliseconds after the shutdown of the housekeeping thread, all connections in the pool are closed. - If any connections were in use while being closed then a
SQLExceptionis thrown. - The log is closed.
Call this method from a servlet destroy() method.- Parameters:
millis- the time to wait in milliseconds.- Throws:
SQLException- if connections were in use aftermillis.
-
destroy
public void destroy()Less safe shutdown. Uses default timeout value. This method simply calls thedestroy()method with amillisvalue of 10000 (10 seconds) and ignoresSQLExceptionthrown by that method.- See Also:
-
getSize
public int getSize()Returns the number of connections in the dynamic pool. -
getConnection
Ignores the username and password passed in. Once created, this datasource will only connect as a single user.- Specified by:
getConnectionin interfaceDataSource- Throws:
SQLException
-
getLoginTimeout
- Specified by:
getLoginTimeoutin interfaceCommonDataSource- Specified by:
getLoginTimeoutin interfaceDataSource- Throws:
SQLException
-
setLoginTimeout
- Specified by:
setLoginTimeoutin interfaceCommonDataSource- Specified by:
setLoginTimeoutin interfaceDataSource- Throws:
SQLException
-
isWrapperFor
- Specified by:
isWrapperForin interfaceWrapper- Throws:
SQLException
-
unwrap
- Specified by:
unwrapin interfaceWrapper- Throws:
SQLException
-
getLogWriter
- Specified by:
getLogWriterin interfaceCommonDataSource- Specified by:
getLogWriterin interfaceDataSource- Throws:
SQLException
-
setLogWriter
- Specified by:
setLogWriterin interfaceCommonDataSource- Specified by:
setLogWriterin interfaceDataSource- Throws:
SQLException
-
getParentLogger
- Specified by:
getParentLoggerin interfaceCommonDataSource- Throws:
SQLFeatureNotSupportedException
-