JDBC连接池

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
public class ConnUtils {
	private static LinkedList<Connection> pool = new LinkedList<Connection>();
	static{
		try {
			//声明资源器类 - 
			Properties prop = new Properties();
			//获取这个文件的路径
			URL url = ConnUtils.class.getClassLoader().getResource("jdbc.properties");
			String path = url.getPath();
			//为了防止有中文或是空格
			path = URLDecoder.decode(path,"UTf-8");
			File file = new File(path);
			//加载jdbc.properties这个文件
			prop.load(new FileInputStream(file));
			//获取信息
			String driver = prop.getProperty("driver");
			Class.forName(driver);
			String jdbcurl = prop.getProperty("url");
			String nm = prop.getProperty("name");
			String pwd = prop.getProperty("pwd");
			//创建三个原生的连接,都将它们代理
			String poolSize = prop.getProperty("poolSize");
			int size = Integer.parseInt(poolSize);
			for(int i=0;i<size;i++){
				final Connection con = DriverManager.getConnection(jdbcurl,nm,pwd);
				//对con进行动态代理
				Object proxyedObj = 
						Proxy.newProxyInstance(ConnUtils.class.getClassLoader(),
									new Class[]{Connection.class},
									new InvocationHandler() {
										public Object invoke(Object proxy, Method method, Object[] args)
												throws Throwable {
											//是否是close方法
											if(method.getName().equals("close")){
												synchronized (pool) {
													pool.addLast((Connection) proxy);
													pool.notify();
												}
												return null;//如果调用的是close则不会调用被代理类的方法。
											}
											return method.invoke(con, args);
										}
									});
				//将代理对象放到pool中
				pool.add((Connection) proxyedObj);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static Connection getConn(){
		synchronized (pool) {
			if(pool.size()==0){
				try {
					pool.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				return getConn();
			}else{
				Connection con = pool.removeFirst();
				System.err.println("还有几个:"+pool.size());
				return con;
			}
		}
	}
	
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值