一个连接池的实现,很不错!
下面是一个连接池的简单实现: 虽然很简单,但是和网上流行的连接池的实现思路大体上都是相近的! ConnPool.java: package bbs;
import java.sql.; import java.util.;
public class ConnPool { private int ConNow=0; private Vector connections = new Vector(); private String PoolName; private String DriverName; private String DbId; private String UserName; private String Password; private int MaxConn;
public ConnPool(String PoolName, String DriverName, String DbId, String UserName, String Password, intMaxConn) {
this. PoolName = PoolName;
this. DriverName = DriverName;
this. DbId = DbId;
this. UserName = UserName;
this. Password = Password;
this. MaxConn = MaxConn;
}
public synchronized void releaseConnection(Connection con) {
connections.addElement(con);
ConNow--;
}
public synchronized Connection getConnection() {
Connection con = null;
if (connections.size() > 0) {
con = (Connection) connections.elementAt(0);
connections.removeElementAt(0);
try {
if (con.isClosed())
con = getConnection();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
else if (MaxConn == 0 || ConNow <MaxConn) {
con = new Connection();
}
if (con != null) {
ConNow++;
}
return con;
}
private Connection newConnection() {
Connection con = null;
try {
Class.forName(DriverName);
con = DriverManager.getConnection(DbId,UserName, Password);
}
catch (Exception e) {
e.printStackTrace();
return null;
}
return con;
}
public synchronized void closeConn() {
Enumeration allConnections = connections.elements();
while (allConnections.hasMoreElements()) {
Connection con = (Connection) allConnections.nextElement();
try {
con.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
connections.removeAllElements();
}
} ConnManager.java: package bbs;
import java.sql.; import java.util.;
public class ConnManager { private Vector PoolNames = new Vector(); private Vector DriverNames = new Vector(); private Vector DbIds = new Vector(); private Vector UserNames = new Vector(); private Vector Passwords = new Vector(); private Vector MaxConns = new Vector(); private Hashtable connPools = new Hashtable();
public ConnManager() {
PoolNames.addElement("bbs");
DriverNames.addElement("org.gjt.mm.mysql.Driver");
DbIds.addElement("jdbc:mysql://localhost:3306/bbs");
UserNames.addElement("root");
Passwords.addElement("123");
MaxConns.addElement("10");
createPool();
}
private void createPool() { for(int i = 0; i<PoolNames.size();i++){ String PoolName = PoolNames.elementAt(i).toString(); String DriverName = DriverNames.elementAt(i).toString(); String DbId = DbIds.elementAt(i).toString(); String UserName = UserNames.elementAt(i).toString(); String Password = Passwords.elementAt(i).toString(); int Maxconn=0; try { Maxconn = Integer.parseInt(MaxConns.elementAt(i).toString()); } catch (NumberFormatException e) { e.printStackTrace(); } ConnPool Pool = new ConnPool(PoolName, DriverName, DbId,UseName, Password, Maxconn); connPools.put(PoolName, Pool); } }
public Connection getConnection(String name) {
ConnPool Pool = (ConnPool) connPools.get(name);
if (Pool != null)
return Pool.getConnection();
return null;
}
public void releaseConnection(String name, Connection con) {
ConnPool pool = (ConnPool) connPools.get(name);
if (pool != null)
pool.releaseConnection(con);
}
public synchronized void destroyPool () {
Enumeration allPools = connPools.elements();
while (allPools.hasMoreElements()) {
ConnPool Pool = (ConnPool) allPools.nextElement();
Pool. destroyPool ();
}
}
}