[转] servlet connection pool code (2)

xiaoxiao2021-03-06  32

/ * * DBConnectionPool.java * created on 2005-1-29 * / package db;

Import java.sql. *; import java.util. *; import java.util.date; import org.apache.log4j.logger; // import org.apache.log4j.propertyConfigurator;

/ ** * @author Yewberry * / public class DBConnectionPool {private int checkedOut; // array variable private Vector freeConnections = new Vector (); private int maxConn; private String name; private String password; private String URL; private String user ; // log4j class object private static logger log; static {try {log = logger.getlogger (DBConnectionPool.class.getName ()); log.info ("Log4J log class successfully loaded."); // Specify log4j.properties The location of the file // PropertyConfigurator.configure ("c: //test//log4j.properties);} catch (noclassdeffounderror ee) {system.out.println (" Log4J log class load failed. ");}} / ** * Create a new connection pool * @Param Name connection pool name * @Param URL database JDBC URL * @Param User database account, or null * @Param password password, or null * @Param MaxConn This connection pool is allowed to establish Maximum number of connections * / public dbconnectionpool (String Name, String Url, String User, String Password, Int MaxConn) {this.name = name; this.url = url; this.user = user; this.password = password; THIS .maxConn = maxconn;

} / ** * Return the connection that is no longer used to the connection pool * @Param con client release * / public synchronized void freeconnection (Connection Con) {// Add the specified connection to the end of the vector FREECONNACTIONS.Addelement (Conne CHECKEDOUT -; NOTIFYAll ();} / ** * Get a available connection from the connection pool. If there is no idle connection and the current connection is less than the maximum connection * number limit, the new connection is created. If it is registered as available The connection is no longer valid, then from the vector, * then recursively call yourself to try new available connections. * / Public synchronized connection getConnection () {connection con = NULL; if (freeConnections.size ()> 0) {// Gets the first available connection Con = (connection) freeConnections.FirstElement (); freeConnections.removeEleMentat (0); try {if (con?isclosed ()) {if (log.isinfoenabled ()) {Log.info "From the connection pool" Name "to delete an invalid connection");} // log ("" From the connection pool " Name " delete an invalid connection "); // Remissive call yourself, try again to get the available connection CON = getConnection );}}} Catch (sqlexception e) {if (log.infoenabled ()) {log.info ("From the connection pool" Name ");} // log (" From Connection Pool Name "Delete an invalid connection"); // Remissively call yourself, try again to get the available connection CON = getConnection ();}} else f (MAXCONN == 0 | Checkedout = timeout) {// Wait () Returns The reason is timeout Return Null;}} Return Con

转载请注明原文地址:https://www.9cbs.com/read-62571.html

New Post(0)