JDBC ??????????
???????????? ???????[ 2013/8/6 10:21:11 ] ????????
????Code
package xgn.jdbc;
import java.io.InputStream;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.LinkedList;
import java.util.Properties;
/***
* ???????????????
* @author dream
*
*/
public class JDBCPool {
//???????????
private static LinkedList<Connection> dblist=new LinkedList<Connection>();
//?????????
private static final int number=100;
static{
try{
Properties config=new Properties();
InputStream in= JDBCPool.class.getClassLoader().getResourceAsStream("config.properties");
config.load(in);
for(int i=0;i<number;i++){
Connection cn= DriverManager.getConnection(config.getProperty("Connstr")??config.getProperty("user")??config.getProperty("pwd"));
ConnectionProxy conn=new ConnectionProxy(cn);
Connection proxycn=conn.getConnectionProxy(cn??new Class[]{Connection.class}??conn);
dblist.addFirst(proxycn);
}
}catch(Exception ex){
throw new RuntimeException(ex);
}
}
public static void recoverConnection(Connection cn){
dblist.addFirst(cn);
}
public static Connection getConnection(){
if(dblist.size()<=0){
throw new RuntimeException("??????????????????????");
}
return dblist.removeFirst();
}
public static int getConnectionNumber(){
return dblist.size();
}
/***
* ??????????????
* @author dream
*
*/
public static class ConnectionProxy implements InvocationHandler {
private Object obj=null;
private Object proxyobj=null;
@Override
public Object invoke(Object arg0?? Method arg1?? Object[] arg2)throws Throwable {
Object ret=null;//?????????
if(arg1.getName()=="close"){
System.out.println("close");
JDBCPool.recoverConnection((Connection)this.proxyobj);
return ret;
}
ret=arg1.invoke(this.obj?? arg2);
return ret;
}
public ConnectionProxy(Connection cn){
this.obj=cn;
}
public Connection getConnectionProxy(Connection cn??Class[] cls??InvocationHandler h){
this.proxyobj= Proxy.newProxyInstance(cn.getClass().getClassLoader()??cls??h);
return (Connection)this.proxyobj;
}
}
}
????????????????????У?DBCP??tomcat ????????????(??????DBCP)??C3P0???
??????

???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11