JDBC连接池

来源:互联网 发布:java多线程数据传递 编辑:程序博客网 时间:2024/05/19 16:38
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;}}}}

0 0
原创粉丝点击