Complete program -> database connection pool (the classic kind) Software Name: database connection pool (the classic kind) Submitted: 2002-06-08 Author: jewes author: This site offers Links: jewes.sharella .com Clicks: 4628 package com.jewes; 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, long time) {DBConnectionPool pool = (DBConnectionPool) pools.get (name); if (! pool = null) {return pool.getConnection (time);} return null;} // End getconnection / / close all connections public synchronized void release () {{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 () );}}} private void createPools (Properties props) {Enumeration propNames = props.propertyNames (); while (propNames.hasMoreElements ()) {String name = (String) propNames.nextElement (); if (name.endsWith ( " .ur ")) {string poolname = name.substring (0, name.lastindex Of (".")); 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.getProperty (poolName "password."); String maxconn = props.getProperty (poolName ". maxconn", "0"); int max; try { Max = integer.Valueof (MaxConn) .intValue ();} catch ("NumberFormatexception E) {log (" Error's maximum number of connections: " 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 ()}} private void init ()}} .getResourceceASstream ("/ db.properties"); Properties dbprops = new profment (); try {dbprops.load (IS);} catch (exception e) {system.err.println ("You cannot read the property file. Please 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 (ioException 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 = (driver) Class.forName (driverClassName) .newInstance (); DriverManager.registerDriver (driver); drivers.addElement (driver); log ( "successfully registered 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 = (Connection) freeConnections .firstelement (); freeConnections.removeElelementat (0); try {if ("" From the connection pool " Name " Delete a connection "); con = getConnection ();}} catch (SQLException e) {log ("from even Pool " Name " Delete a connection "); con = getConnection ();}} else if (MaxConn == 0 || Checkout