java链接池实现
package com.siven.connpool;
import java.io.IOException;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Vector;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class ConnectionPool {
public static ConnectionPool connectionPool = null;
public int clients = 10;
public Vector<Connection> freeConnections = new Vector<Connection>();
public Driver driver;
public int max = 20;
private ConnectionPool() throws SAXException, IOException, ParserConfigurationException, InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException{
init();
}
public static ConnectionPool getInstance() throws SAXException, IOException, ParserConfigurationException, InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException{
if(connectionPool==null){
connectionPool = new ConnectionPool();
}
return connectionPool;
}
public void init() throws SAXException, IOException, ParserConfigurationException, InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException{
driver = (Driver) Class.forName(getConfig("driver")).newInstance();
max=Integer.valueOf(getConfig("max")).intValue();
Class.forName(getConfig("driver"));
String connections = getConfig("connections");
String username=getConfig("username");
String password=getConfig("password");
for(int i=0;i<clients;i++){
Connection conn = DriverManager.getConnection(connections,username,password);
freeConnections.add(conn);
}
}
public Connection getConnection() throws ClassNotFoundException, SQLException, SAXException, IOException, ParserConfigurationException{
Connection conn = null;
if(freeConnections.size()>0){
conn = (Connection)freeConnections.firstElement();
freeConnections.removeElementAt(0);
}else{
if(clients<max){
conn = newConnection();
}
}
return conn;
}
public void freeConnection(Connection conn) throws SQLException{
freeConnections.add(conn);
release();
}
public void release() throws SQLException{
Enumeration<Connection> allConnections = freeConnections.elements();
while(allConnections.hasMoreElements()){
Connection con = (Connection) allConnections.nextElement();
con.close();
}
if(freeConnections.size()>10){
for(int i=10;i<freeConnections.size();i++){
Connection con = freeConnections.remove(i);
con = null;
}
}
DriverManager.deregisterDriver(driver);
}
public Connection newConnection() throws ClassNotFoundException, SQLException, SAXException, IOException, ParserConfigurationException{
Class.forName(getConfig("driver"));
Connection conn = DriverManager.getConnection(getConfig("connections"),getConfig("username"),getConfig("password"));
clients++;
return conn;
}
public String getConfig(String attribute) throws SAXException, IOException, ParserConfigurationException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse("config.xml");
NodeList nl = doc.getElementsByTagName("config");
Element e=(Element)nl.item(0);
return e.getAttribute(attribute);
}
public Vector<Connection> getFreeConnections() {
return freeConnections;
}
public void setFreeConnections(Vector<Connection> freeConnections) {
this.freeConnections = freeConnections;
}
}