Code description, reference article and operational environment Please refer to the series article (5) / * * DBConnectionManager.java * created on 2005-1-29 * / package db;
Import java.io. *; import java.sql. *; import java.util. *; import org.apache.log4j.logger; // import org.apache.log4j.propertyConfigurator;
/ ** * Management class DBConnectionManager supports access to the database connection * pool defined by the properties file. The client can call the GetInstance () method to access the only instance of this class. * * @Author Yewberry * / public Class DBConnectionManager {private static DBConnectionManager instance; // unique instance private static int clients; private Vector drivers = new Vector (); // private PrintWriter log; private Hashtable pools = new Hashtable (); private static Logger log; static {try {log = Logger.getlogger (DBConnectionManager.class.getName ()); log.info ("Log4J log class successfully loaded."); // Specify the location of the log4j.properties file // PropertyConfigurator.configure ("C: // Test / /Log4j.properties ");} catch (NoclassDeffounderror EE) {system.out.println (" Log4J log class load failed.
");}} / ** * Returns the only instance. If this method is called, create an instance * * @return DBConnectionManager unique instance * / static synchronized public dbconnectionManager getInstance () {if (instance == null) { Instance = new dbconnectionManager ();} clients ; returnial;} / ** * Structure function privately prevented from creating this class instance * / private dbconnectionManager () {init ();} / ** * Return the connection object to Connection pool * @Param Name in the Property file Name * @Param CON connection object * / public void freeconnection (String name, connection con) {dbconnectionpool pool = (dbconnectionpool) pools.get (name) Pools.get ); If (pool! = Null) {pool.freeConnection (con);}} / ** * Get a available (idle) connection. If there is no connection available, and the number of connections is less than the maximum number of connections * limit, Create and return new connections * * @Param Name Defined connection pool name in the properties file * @return connection Available Connection or null * / public connection getConnection (String name) {dbconnectionPool pool = (dbconnectionpool) pools.get (name) IF (pool! = Null) {return pool.getConnection ();} return null;} / ** * Get a available connection. If no connection is available, the number of connections is less than the maximum number of connections, * is created and Return new connection. Otherwise, within the specified time Waiting for the other thread to release the connection. * * @Param name connection pool name * @param time latency in milliseconds * @return Connection available connection or null * / public Connection getConnection (String name, long time) {DBConnectionPool pool = (DBConnectionPool ) Pools.get (Name); if (pool! = null) {return pool.getConnection (Time);} return null;} / ** * Close all connections, revoke drivers * / public synchronized void release () {// wait until the last clients call if (--clients = 0!) {return;} Enumeration allPools = pools.elements (); while (allPools.hasMoreElements ()) {DBConnectionPool pool = (DBConnectionPool) allPools.nextElement (); Pool.release (); ENUMERATION alldrivers = drivers.ements ();
while (allDrivers.hasMoreElements ()) {Driver driver = (Driver) allDrivers.nextElement (); try {DriverManager.deregisterDriver (driver); if (log.isInfoEnabled ()) {log.info ( "undo JDBC driver" Driver.getClass (). GetName () "registration.");} // log ("Undo JDBC Driver" Driver.getClass (). GetName () "Registration //////////");} catch (SQLEXCEPTION E) {if (log.isinfoenabled ()) {log.info ("Unable to undo the following JDBC driver registration:" Driver.getClass (). Getname (), e);} // log (e, "Registration of the following JDBC driver unable to undo:" driver.getClass (). GetName ());}}} / ** * Read Properties Finish Initial * / Private Void INIT () {INPUTSTREAM IS = getClass (). getResourceAsStream ("/ db.properties"); Properties dbprops = new profment (); try {dbprops.load (IS); // list all the elements in dbprops // dbprops.list (system.out);} catch (Exception E) {System.err.Println ("You cannot read the property file." "Please ensure that db.properties is in the path specified by the classpath.
"); Return;} / * string logfile = dbprops.getProperty (" logfile "," dbconnectionmanager.log "); try {// log = new PrintWriter (logfile, true), true);} catch (IOException e) {system.err.println ("Unable to open the log file:" logfile); // log = new printwriter (system.err);} * / loadDrivers (dbprops); createPools (dbprops);} / ** * load and register all JDBC drivers * * @param props attribute * / private void loadDrivers (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); / /System.out.println ("successfully registered JDBC driver:" driverclassname); if (log.isinfoenabled ()) {log.info ("successfully registered JDBC driver:" driverclassname);} // log (" Successfully registered JDBC driver /// " Driverc Lassname);} catch (Exception E) {//system.out.println ("Unable to register JDBC driver:" driverclassname); if (log.isinfoenabled ()) {log.info ("Unable to register JDBC driver: " DriverClassName, E);} // log (" Unable to register the JDBC driver: " // driverclassname ", error: " e);}}} / ** * Create a connection pool instance according to the specified property. * * @param props connection pool attribute * / private void createPools (properties props) {Enumeration propNames = props.propertyNames (); while (propNames.hasMoreElements ()) {String name = (String) propNames.nextElement (); if (name .endswith (". URL"