刚搞明白,仔细看了!呵呵:
//////////////////////////////////////////////////////////////////////////////
import java.io.*;
import java.sql.*;
import java.sql.Driver;
import java.util.*;
import java.util.Date;
//管理DBConnectionManager支持对一个或多个由属性文件定义的数据库连接池的
//访问客户程序可以调用getInstance()方法来访问本类的唯一实例
public class DBConnectionManager{
static private DBConnectionManager instance;//唯一实例
static private int clients;
private Vector drivers=new Vector();
private PrintWriter log;
private Hashtable pools=new Hashtable();
//返回实例,如果第一次调用次方法,则创建实例
//@return DBConnectionManager 唯一失例
public static void main(String args[])
{
} static synchronized public DBConnectionManager getInstance(){
if(instance==null){
instance=new DBConnectionManager();
}
clients++;
return instance;
}
//构造私有函数以防止其他对象创建本类实例
private DBConnectionManager(){
init();
}
//将连接对象返回给由名字指定的连接池
//@param name 在属性文件中定义的连接池名字 //@param con 连接对象 public void freeConnection(String name,Connection con){
DBConnectionPool pool=(DBConnectionPool)pools.get(name);
if(pool!=null){
pool.freeConnection(con);
}
} //获得一个可用的(空闲的)连接,如果没有可用连接,且已有连接数小于最大连接数
//限制、则创建并返回新连接 //@param name 在属性文件中定义的连接池名字
//return Connection 可用连接或null
public Connection getConnection(String name){
DBConnectionPool pool=(DBConnectionPool)pools.get(name);
if(pool!=null){
return pool.getConnection();
}
return null;
} //获得一个可用的(空闲的)连接,如果没有可用连接,且已有连接数小于最大连接数
//则创建并返回新连接,否则,在指定的时间内等待其他线程释放连接。
//@param name 连接池名字
//@param time 一毫秒记的等待时间
//@return Connection 可用连接或null
public Connection getConnection(String name,long time){
DBConnectionPool pool=(DBConnectionPool)pools.get(name);
if(pool!=null){
return pool.getConnection(time);
}
return null;
}
//关闭所有连接,撤消驱动程序的注册
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("撤消JDBC驱动程序"+drivers.getClass().getName()+"的注册");
}
catch(SQLException e){
log("无法撤消下列JDBC驱动程序的注册:"+driver.getClass().getName());
}
}
}
//根据指定属性创建连接池实例
//@param props 连接池属性 private void createPools(Properties props){
Enumeration propNames=props.propertyNames();
while(propNames.hasMoreElements()){
String name=(String)propNames.nextElement();
if(name.endsWith(".url")){
String poolName=name.substring(0,name.lastIndexOf("."));
String url=props.getProperty(poolName+".url");
if(url==null){
log("没有为连接池"+poolName+"指定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("错误的最大连接数限制:"+maxconn+".连接池:"+poolName);
max=0;
}
DBConnectionPool pool=new DBConnectionPool(poolName,url,user,password,max);
pools.put(poolName,pool);
log("成功创建连接池"+poolName);
}
}
} //读取属性完成初始化
private void init(){
InputStream is=getClass().getResourceAsStream("/db.properties"); Properties dbProps=new Properties();
try{
dbProps.load(is);
}
catch(Exception e){
System.err.println("不能读取属性文件."+"请确保db.properties在ClassPath指定的路径中");
return;
}
String logFile=dbProps.getProperty("logfile","DBConnectionManager.log");
try{
log=new PrintWriter(new FileWriter(logFile,true),true);
}
catch(IOException e){
System.err.println("无法打开日志文件:"+"logFile");
log=new PrintWriter(System.err);
}
loadDrivers(dbProps);
createPools(dbProps);
} //装载和注册所有JDBC驱动程序
//@param props 属性 private void loadDrivers(Properties props){
String driverClass=props.getProperty("drvers");
StringTokenizer st=new StringTokenizer(driverClass);
//Driver driver;
while(st.hasMoreElements()){
String driverClassName=st.nextToken().trim();
try{
Driver driver=(Driver)
Class.forName(driverClassName).newInstance();
DriverManager.registerDriver(driver);
String d=new String();
//drivers.addElemnet(d);
drivers.addElement(driver);
log("成功注册JDBC驱动程序"+driverClassName);
}
catch(Exception e){
log("无法注册JDBC驱动程序:"+driverClassName+",错误:"+e);
}
}
} //将文本信息写入日志文件
public void log(String msg){
log.println(new Date()+":"+msg);
} //将文本信息与异常写入日志文件
public void log(Throwable e,String msg){
log.println(new Date()+":"+msg);
e.printStackTrace(log);
}
}
//此类内部定义了一个连接池,他能够根据要求创建连接,直到预定的
//最大连接数为止,在返回连接给客户程序之前,他能够验证连接的有效性class DBConnectionPool
{
private int checkedOut;
private Vector freeConnections=new Vector();
private int maxConn;
private String name;
private String password;
private String URL;
private String user; private PrintWriter log;
//创建新连接池
//name连接池名字
//URL数据库JDBC URL
//user数据库帐好
//password密码
//maxConn最大连接数
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;
} //将不使用的连接返回给连接池
//param con 客户程序释放的连接 public synchronized void freeConnection(Connection con)
{
//将指定连接加入到向量末尾 freeConnections.addElement(con);
checkedOut--;
notifyAll();
}
//从连接池获得一个可用连接,如没有空闲的连接且当前连接数小于最大连接
//数限制,则创建新连接,如原来登记为可用的连接不在有效,则从向量中删除他
//然后递归调用自己尝试新的连接 public synchronized Connection getConnection()
{
Connection con=null;
if(freeConnections.size()>0){
//获取向量中第一个可用连接
con=(Connection)freeConnections.firstElement();
freeConnections.removeElementAt(0);
try{
if(con.isClosed()){
loga("从连接池删除一个无效连接");
//递归调用自己尝试再次获取可用连接
con=getConnection();
}
}
catch(SQLException e){
loga("从连接池"+name+"删除一个无效连接");
//递归调用自己尝试再次获取可用连接
con=getConnection();
}
}
else if(maxConn==0||checkedOut<maxConn){
con=newConnection();
}
if(con!=null){
checkedOut++;
}
return con;
}
//从连接池获取可用连接,可以指定客户程序能够等待的最长时间
//参见前一个getConnction()方法
//@param timeout 以毫秒的等待时间限制
public synchronized Connection getConnection(long timeout){
long startTime=new Date().getTime();
Connection con;
while((con=getConnection())==null){
try{
wait(timeout);
}
catch(InterruptedException e){}
if((new Date().getTime()-startTime)>=timeout){
//wait()返回的原因是超时
return null;
}
}
return con;
}
//关闭所有连接
public synchronized void release(){
Enumeration allConnections=freeConnections.elements();
while(allConnections.hasMoreElements()){
Connection con=(Connection)allConnections.nextElement();
try{
con.close();
loga("无法关闭连接池"+name+"中的连接");
}
catch(SQLException e){
loga(e,"无法关闭连接池"+name+"中的连接");
}
}
freeConnections.removeAllElements();
}
//创建新的连接
private Connection newConnection(){
Connection con=null;
try{
if(user==null){
con=DriverManager.getConnection(URL);
}
else{
con=DriverManager.getConnection(URL,user,password);
}
loga("连接池"+name+"创建一个新的连接");
}
catch(SQLException e){
loga(e,"无法创建下列URL的连接"+URL);
return null;
}
return con;
}
public void loga(String msg){
log.println(new Date()+":"+msg);
} //将文本信息与异常写入日志文件
public void loga(Throwable e,String msg){
log.println(new Date()+":"+msg);
e.printStackTrace(log);
}
}
//////////////////////////////////////////////////////////////////////////////
import java.io.*;
import java.sql.*;
import java.sql.Driver;
import java.util.*;
import java.util.Date;
//管理DBConnectionManager支持对一个或多个由属性文件定义的数据库连接池的
//访问客户程序可以调用getInstance()方法来访问本类的唯一实例
public class DBConnectionManager{
static private DBConnectionManager instance;//唯一实例
static private int clients;
private Vector drivers=new Vector();
private PrintWriter log;
private Hashtable pools=new Hashtable();
//返回实例,如果第一次调用次方法,则创建实例
//@return DBConnectionManager 唯一失例
public static void main(String args[])
{
} static synchronized public DBConnectionManager getInstance(){
if(instance==null){
instance=new DBConnectionManager();
}
clients++;
return instance;
}
//构造私有函数以防止其他对象创建本类实例
private DBConnectionManager(){
init();
}
//将连接对象返回给由名字指定的连接池
//@param name 在属性文件中定义的连接池名字 //@param con 连接对象 public void freeConnection(String name,Connection con){
DBConnectionPool pool=(DBConnectionPool)pools.get(name);
if(pool!=null){
pool.freeConnection(con);
}
} //获得一个可用的(空闲的)连接,如果没有可用连接,且已有连接数小于最大连接数
//限制、则创建并返回新连接 //@param name 在属性文件中定义的连接池名字
//return Connection 可用连接或null
public Connection getConnection(String name){
DBConnectionPool pool=(DBConnectionPool)pools.get(name);
if(pool!=null){
return pool.getConnection();
}
return null;
} //获得一个可用的(空闲的)连接,如果没有可用连接,且已有连接数小于最大连接数
//则创建并返回新连接,否则,在指定的时间内等待其他线程释放连接。
//@param name 连接池名字
//@param time 一毫秒记的等待时间
//@return Connection 可用连接或null
public Connection getConnection(String name,long time){
DBConnectionPool pool=(DBConnectionPool)pools.get(name);
if(pool!=null){
return pool.getConnection(time);
}
return null;
}
//关闭所有连接,撤消驱动程序的注册
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("撤消JDBC驱动程序"+drivers.getClass().getName()+"的注册");
}
catch(SQLException e){
log("无法撤消下列JDBC驱动程序的注册:"+driver.getClass().getName());
}
}
}
//根据指定属性创建连接池实例
//@param props 连接池属性 private void createPools(Properties props){
Enumeration propNames=props.propertyNames();
while(propNames.hasMoreElements()){
String name=(String)propNames.nextElement();
if(name.endsWith(".url")){
String poolName=name.substring(0,name.lastIndexOf("."));
String url=props.getProperty(poolName+".url");
if(url==null){
log("没有为连接池"+poolName+"指定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("错误的最大连接数限制:"+maxconn+".连接池:"+poolName);
max=0;
}
DBConnectionPool pool=new DBConnectionPool(poolName,url,user,password,max);
pools.put(poolName,pool);
log("成功创建连接池"+poolName);
}
}
} //读取属性完成初始化
private void init(){
InputStream is=getClass().getResourceAsStream("/db.properties"); Properties dbProps=new Properties();
try{
dbProps.load(is);
}
catch(Exception e){
System.err.println("不能读取属性文件."+"请确保db.properties在ClassPath指定的路径中");
return;
}
String logFile=dbProps.getProperty("logfile","DBConnectionManager.log");
try{
log=new PrintWriter(new FileWriter(logFile,true),true);
}
catch(IOException e){
System.err.println("无法打开日志文件:"+"logFile");
log=new PrintWriter(System.err);
}
loadDrivers(dbProps);
createPools(dbProps);
} //装载和注册所有JDBC驱动程序
//@param props 属性 private void loadDrivers(Properties props){
String driverClass=props.getProperty("drvers");
StringTokenizer st=new StringTokenizer(driverClass);
//Driver driver;
while(st.hasMoreElements()){
String driverClassName=st.nextToken().trim();
try{
Driver driver=(Driver)
Class.forName(driverClassName).newInstance();
DriverManager.registerDriver(driver);
String d=new String();
//drivers.addElemnet(d);
drivers.addElement(driver);
log("成功注册JDBC驱动程序"+driverClassName);
}
catch(Exception e){
log("无法注册JDBC驱动程序:"+driverClassName+",错误:"+e);
}
}
} //将文本信息写入日志文件
public void log(String msg){
log.println(new Date()+":"+msg);
} //将文本信息与异常写入日志文件
public void log(Throwable e,String msg){
log.println(new Date()+":"+msg);
e.printStackTrace(log);
}
}
//此类内部定义了一个连接池,他能够根据要求创建连接,直到预定的
//最大连接数为止,在返回连接给客户程序之前,他能够验证连接的有效性class DBConnectionPool
{
private int checkedOut;
private Vector freeConnections=new Vector();
private int maxConn;
private String name;
private String password;
private String URL;
private String user; private PrintWriter log;
//创建新连接池
//name连接池名字
//URL数据库JDBC URL
//user数据库帐好
//password密码
//maxConn最大连接数
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;
} //将不使用的连接返回给连接池
//param con 客户程序释放的连接 public synchronized void freeConnection(Connection con)
{
//将指定连接加入到向量末尾 freeConnections.addElement(con);
checkedOut--;
notifyAll();
}
//从连接池获得一个可用连接,如没有空闲的连接且当前连接数小于最大连接
//数限制,则创建新连接,如原来登记为可用的连接不在有效,则从向量中删除他
//然后递归调用自己尝试新的连接 public synchronized Connection getConnection()
{
Connection con=null;
if(freeConnections.size()>0){
//获取向量中第一个可用连接
con=(Connection)freeConnections.firstElement();
freeConnections.removeElementAt(0);
try{
if(con.isClosed()){
loga("从连接池删除一个无效连接");
//递归调用自己尝试再次获取可用连接
con=getConnection();
}
}
catch(SQLException e){
loga("从连接池"+name+"删除一个无效连接");
//递归调用自己尝试再次获取可用连接
con=getConnection();
}
}
else if(maxConn==0||checkedOut<maxConn){
con=newConnection();
}
if(con!=null){
checkedOut++;
}
return con;
}
//从连接池获取可用连接,可以指定客户程序能够等待的最长时间
//参见前一个getConnction()方法
//@param timeout 以毫秒的等待时间限制
public synchronized Connection getConnection(long timeout){
long startTime=new Date().getTime();
Connection con;
while((con=getConnection())==null){
try{
wait(timeout);
}
catch(InterruptedException e){}
if((new Date().getTime()-startTime)>=timeout){
//wait()返回的原因是超时
return null;
}
}
return con;
}
//关闭所有连接
public synchronized void release(){
Enumeration allConnections=freeConnections.elements();
while(allConnections.hasMoreElements()){
Connection con=(Connection)allConnections.nextElement();
try{
con.close();
loga("无法关闭连接池"+name+"中的连接");
}
catch(SQLException e){
loga(e,"无法关闭连接池"+name+"中的连接");
}
}
freeConnections.removeAllElements();
}
//创建新的连接
private Connection newConnection(){
Connection con=null;
try{
if(user==null){
con=DriverManager.getConnection(URL);
}
else{
con=DriverManager.getConnection(URL,user,password);
}
loga("连接池"+name+"创建一个新的连接");
}
catch(SQLException e){
loga(e,"无法创建下列URL的连接"+URL);
return null;
}
return con;
}
public void loga(String msg){
log.println(new Date()+":"+msg);
} //将文本信息与异常写入日志文件
public void loga(Throwable e,String msg){
log.println(new Date()+":"+msg);
e.printStackTrace(log);
}
}
比如weblogic中在console中设置几个参数后,服务器启动时会自动与数据库连接建立连接池。然后在程序中获得连接就行了。
songkaihli(泰山),你说的这个只是servlet调用才行,jsp如何调用它呢?
连接池原理使用唯一对象实现
对数据库连接共享,把构造方法私有化,
用getInstance方法返回唯一实例。
调用不会因为servlet或者jsp而不同
static synchronized public DBConnectionManager getInstance()
是个静态方法,你直接调用就可以了
But all of them are not as good as the one a application server banding with .
我就用PoolMan了,谢了各位我都加分!
不过您们有什么好的连接池不要忘了说一声喔,我一定会加分感谢的!!!