- package my.util;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.util.Date;
- import java.util.Vector;
- public class MyConnectionPool
- {
- private static MyConnectionPool myPool; //自身静态成员 用于实现单例
- private static Vector<Connection> connPool; //连接池缓存容器
- private static int poolMaxSize; //连接池最大缓存数
- private String username; //用户名
- private String password; //密码
- private String driverClass; //连接驱动
- private String url; //连接字符串
- private MyConnectionPool() {
- String tempSize = PropertiesUtil.getValueByKey("poolMaxSize");
- if(null != tempSize && !"".equals(tempSize)) {
- poolMaxSize = Integer.parseInt(tempSize);
- }
- username = PropertiesUtil.getValueByKey("username");
- password = PropertiesUtil.getValueByKey("password");
- driverClass = PropertiesUtil.getValueByKey("driverClass");
- url = PropertiesUtil.getValueByKey("url");
- connPool = new Vector<Connection>();
- int size = 0;
- if(poolMaxSize > 5) {
- size = 5;
- } else {
- size = poolMaxSize;
- }
- for(int i = 0; i < size; i++) {
- //预填充连接池
- connPool.add(createConnection());
- }
- // for(Connection conn : connPool) {
- // System.out.println(conn);
- // }
- }
- public static MyConnectionPool newInstance() {
- if(null == myPool)
- myPool = new MyConnectionPool();
- return myPool;
- }
- public Connection createConnection() {
- Connection conn = null;
- try
- {
- Class.forName(driverClass);
- }
- catch (ClassNotFoundException e)
- {
- e.printStackTrace();
- }
- try
- {
- conn = DriverManager.getConnection(url, username, password);
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- return conn;
- }
- public static void releaseConnection(Connection conn) {
- if(connPool.size() < poolMaxSize)
- connPool.add(conn);
- }
- public synchronized Connection getConnection() {
- //long startTime = new Date().getTime();
- ConnectionProxy proxy = new ConnectionProxy();
- int size = connPool.size();
- if(0 == size) {
- Connection conn = createConnection();
- System.out.println("getConn conn = " + conn);
- proxy.setConn(conn);
- long endTime = new Date().getTime();
- //System.out.println("Total time = " + (endTime - startTime));
- return proxy.getProxyObject();
- }
- Connection conn = connPool.get(size - 1);
- //System.out.println("getConnection index = " + (size - 1) + " conn = " + conn);
- proxy.setConn(conn);
- connPool.remove(size - 1);
- long endTime = new Date().getTime();
- //System.out.println("Total time = " + (endTime - startTime));
- return proxy.getProxyObject();
- }
- }
- package my.util;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.util.Properties;
- public class PropertiesUtil
- {
- private static Properties prop = new Properties();
- private static FileInputStream inputStream;
- public static String getValueByKey(String key) {
- if(null == inputStream) {
- try
- {
- inputStream = new FileInputStream("src/dbcp.properties");
- }
- catch (FileNotFoundException e)
- {
- e.printStackTrace();
- }
- }
- try
- {
- prop.load(inputStream);
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- return prop.getProperty(key);
- }
- }
- package my.util;
- import java.lang.reflect.InvocationHandler;
- import java.lang.reflect.Method;
- import java.lang.reflect.Proxy;
- import java.sql.Connection;
- public class ConnectionProxy implements InvocationHandler
- {
- private Connection conn;
- public Connection getConn()
- {
- return conn;
- }
- public void setConn(Connection conn)
- {
- this.conn = conn;
- }
- public ConnectionProxy() {}
- public ConnectionProxy(Connection conn) {
- this.conn = conn;
- }
- public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
- {
- if("close".equals(method.getName())) {
- System.out.println("invoke " + conn +" before.");
- MyConnectionPool.releaseConnection(conn);
- System.out.println("invoke " + conn +" after.");
- } else {
- return method.invoke(conn, args);
- }
- return null;
- }
- public Connection getProxyObject() {
- // Class[] clazzs = conn.getClass().getInterfaces();
- //
- // for(Class clazz : clazzs) {
- // System.out.println(clazz);
- // }
- Connection proxyConn = (Connection) Proxy.newProxyInstance(this.conn.getClass().getClassLoader(),
- new Class[]{Connection.class}, this);
- return proxyConn;
- }
- }
Test.Class
- package my.util;
- import java.sql.Connection;
- public class TestConnPool
- {
- public static void main(String[] args) throws Exception
- {
- MyConnectionPool myPool = MyConnectionPool.newInstance();
- Connection conn = myPool.getConnection();
- Connection conn2 = myPool.getConnection();
- Connection conn3 = myPool.getConnection();
- Connection conn4 = myPool.getConnection();
- Connection conn5 = myPool.getConnection();
- System.out.println("conn : " + conn);
- System.out.println("conn2 : " + conn2);
- System.out.println("conn3 : " + conn3);
- System.out.println("conn4 : " + conn4);
- System.out.println("conn5 : " + conn5);
- conn.close();
- conn2.close();
- conn3.close();
- System.out.println(" ------- ");
- conn = myPool.getConnection();
- conn4.close();
- conn5.close();
- conn.close();
- }
- }