Package zpxx; import java.io. *; import java.sql. *; import java.util. *; import java.util.date;
// Establish DBConnectionManagerPublic Class DbconnectionManager {Static Private DBConnectionManager Instance; Static Private Int Clients
Private Vector Drivers = New Vector (); Private PrintWriter Log; Private HashTable Pools = New HashTable ();
/ / Return the only list of STATIC SYNCHRONIZED PUBLIC DBCONNECTIONMANAGER GETITANCE () {if (instance == null) {instance = new dbconnectionmanager ();} clients ; return instance;}
//Constructor! private DBConnectionManager () {init ();} // // constructor completes a connection release public void freeConnection (String name, Connection con) {DBConnectionPool pool = (DBConnectionPool) pools.get (name);! if (pool = null ) {Pool.freeConnection (con);}} // End release a connection
// A connection of public connection getConnection (String Name) {dbconnectionPool Pool = (DBConnectionPool) pools.get (Name); if (pool! = Null) {Return pool.getConnection ();} return null;}
public Connection getConnection (String name, String customer) {DBConnectionPool pool = (DBConnectionPool) pools.get (name); if (pool = null!) {return pool.getConnection (customer);} return null;}
public Connection getConnection (String name, long time) {DBConnectionPool pool = (DBConnectionPool) pools.get (name); if (! pool = null) {return pool.getConnection (time);} return null;} // End getconnection / / Turn off all connections () {// if (- clients! = 0) // Return;
Enumeration allPools = pools.elements (); while (allPools.hasMoreElements ()) {DBConnectionPool pool = (DBConnectionPool) allPools.nextElement (); pool.release ();} Enumeration allDrivers = drivers.elements (); while (allDrivers. hasMoreElements ()) {driver driver = (driver) allDrivers.nextElement (); try {DriverManager.deregisterDriver (driver); log ( "undo JDBC driver" driver.getClass () getName ());.} catch (SQLException e) {log (e, "Registration of the JDBC driver" driver.getClass () driver.getClass (). GetName ());}}}}}}}} public synchronized void release (String Customer) {// if (- Clients! = 0 ) // Return;
Enumeration allPools = pools.elements (); while (allPools.hasMoreElements ()) {DBConnectionPool pool = (DBConnectionPool) allPools.nextElement (); pool.release (customer);} Enumeration allDrivers = drivers.elements (); while (allDrivers .hasMoreElements ()) {driver driver = (driver) allDrivers.nextElement (); try {DriverManager.deregisterDriver (driver); log (customer "undo JDBC driver" driver.getClass () getName ());.} catch (SQLEXCEPTION E) {log (e, "Registration of the JDBC driver" driver.getClass () driver.getClass () DRIVER.GETCLASS ();}}}}}}}}} prOPs) {Enumeration PropNames = props.propertyNames (); While (propNames.hasMoreElements ()) {string name = (string) propNames.nexTelement (); if (name.endswith (". URL")) {string poolname = name.substring (0, name.lastIndexof " )); String url = props.getProperty (poolname "); if (url == null) {log (" No connection pool " poolname " Specified URL "); Continue;} String user = Props.getProperty (Poolname ". User"); string password = props.getpropertser y (poolName "password."); String maxconn = props.getProperty (poolName ". maxconn", "0"); int max; try {max = Integer.valueOf (maxconn) .intValue ();} catch (NumberFormatException e ) {Log ("The maximum number of errors:" MaxConn ". Connection pool" poolname); max = 0;} DBConnectionPool Pool = New DbconnectionPool (PoolName, URL, User, Password, Max); Pools.Put (Poolname , pool; log ("successfully created connection pool" poolname);}}}
private void init () {InputStream is = getClass () getResourceAsStream ( "db.properties");. Properties dbProps = new Properties (); try {dbProps.load (is);} catch (Exception e) {System.err. Println ("You cannot read the properties file. Make sure DB.Properties in your classpath"); return;} string logfile = dbprops.getProperty ("logfile", "dbconnectionmanager.log"); try {log = new PrintWriter New FileWriter (Logfile, True), True;} catch (ooexception e) {system.err.println ("Unable to open the log file:" logfile); log = new printwriter (system.err);} loadingdriver (dbprops) ; createPools (dbProps);} private void loadDriver (Properties props) {String driverClasses = props.getProperty ( "drivers"); StringTokenizer st = new StringTokenizer (driverClasses); while (st.hasMoreElements ()) {String driverClassName = st. NextToken (). TRIM (); try {driver = (driver) Class.Forname (driverclassname) .newinstance (); driverManager.registerDriver; driver.addelement (driver); log ("successful registration driver" Driverclassname);} catch (Exception E) { LOG ("Unable to register the driver:" driverclassname ", error" e);}}}
Private void log (string msg) {log.println (new date () ":" msg);} private void log (throwable e, string msg) {log.println (New Date () ": msg ); e.printStackTrace (log);} class DBConnectionPool {private int checkOut; private Vector freeConnections = new Vector (); private int maxconn; private String name; private String password; private String URL; private String user;
public DBConnectionPool (String name, String URL, String user, String password, int maxconn) {this.name = name; this.URL = URL; this.password = password; this.user = user; this.maxconn = maxconn;} public synchronized void freeConnection (Connection con) {freeConnections.addElement (con); checkOut--; notifyAll ();} public synchronized Connection getConnection () {Connection con = null; if (freeConnections.size ()> 0) {con = () FREECONNECTIONS.FIRSTELEMENT (); FreeConnections.RemoveEleMentat (0); try {if ("" "from the connection pool" Name "Delete a connection"); con = getConnection (); } Catch ("SQLEXCEPTION E) {log (" From Connection Pool " Name " Delete a connection "); con = getConnection ();}} else if (MaxConn == 0 || Checkout public synchronized Connection getConnection (long timeout) {long startTime = new Date () getTime ();. Connection con; while ((con = getConnection ()) == null) {try {wait (timeout);} catch (InterruptedException e ) {} IF ((NEW DATE (). GetTime () - startTime)> = timeout) {Return Null;}} Return Con;}} Return Con; () {Enumeration AllConnections = freeConnections.efficient (); While (AllConnections. HasmoreElements ()) {Connection Con = (Connection) AllConnections.nextelement (); try {con.close (); log ("Close connection pool" Name "connection");} catch (sqlexception e) {log e, "can not close the connection pool" name "in connection");}} freeConnections.removeAllElements ();} public void release (String customer) {Enumeration allConnections = freeConnections.elements (); while (allConnections.hasMoreElements () ) {Connection Con = () AllConnections.nexTelement (); try {con.close (); log (Customer "Close connection pool" Name ");} catch (SQLException E {Log (e, "cannot close the connection in the connection pool" Name "");}} FreeConnections.removeAllelements ();} private connection newconnection () {Connection Con = NULL; try {con = drivermanager.getConnection (URL , user, password); log ("Connection pool" Name "creates a new connection");} catch (SQLException E) {log (e, "Unable to create the connection" URL) of the following URL; Return Null; Return con;