手写数据库连接池(动态代理)

package com.zhangbz.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.zhangbz.pool.MyPool;

public class JDBCDemo1 {
	public static void main(String[] args) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		MyPool pool = new MyPool();
		try {
			conn = pool.getConnection();
			ps = conn.prepareStatement("select * from account");
			rs = ps.executeQuery();
			while(rs.next()) {
				String name = rs.getString("name");
				System.out.println(name);
			}
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				} finally {
					rs = null;
				}
			}
			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
					e.printStackTrace();
				} finally {
					ps = null;
				}
			}
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				} finally {
					conn = null;
				}
			}
		}
	}
}

MyPool.java

package com.zhangbz.pool;


import java.io.PrintWriter;
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.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import javax.sql.DataSource;

public class MyPool implements DataSource{
	private static List<Connection> pool = new LinkedList<Connection>();
	static {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			for (int i = 0; i < 5; i++) {
				Connection conn = DriverManager.getConnection("jdbc:mysql:///Day11", "root", "root");
				pool.add(conn);
			}
		} catch (Exception e) {
			 e.printStackTrace();
			 throw new RuntimeException(e);
		}
	}
	public Connection getConnection() throws SQLException {
		if (pool.size() == 0) {
			for(int i = 0; i < 3; i++) {
				Connection conn = DriverManager.getConnection("jdbc:mysql:///Day11", "root", "root");
				pool.add(conn);
			}
		}
		final Connection conn = pool.remove(0);
		//--利用动态代理改造close方法(还可以使用继承和装饰)
		Connection proxy = (Connection) Proxy.newProxyInstance(conn.getClass().getClassLoader(), conn.getClass().getInterfaces(), new  InvocationHandler() {

			public Object invoke(Object proxy, Method method, Object[] args)
					throws Throwable {
				if ("close".equals(method.getName())) {
					//对于想要改造的方法我们自己写
					retConn(conn);
					return null;
				} else {
					//对于不想改造的方法,调用被代理者身上的方法
					return method.invoke(conn, args);
				}
			}
		});
		System.out.println("获取了一个连接,池里还剩余" +pool.size()+ "个连接");
		return proxy;
	}
	
	private  void retConn(Connection conn) {
		try {
			if (conn != null && !conn.isClosed()) {
				pool.add(conn);
				System.out.println("换回来一个连接,池里还剩余" +pool.size()+ "个连接");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public Connection getConnection(String arg0, String arg1)
			throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	public PrintWriter getLogWriter() throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	public int getLoginTimeout() throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}

	public void setLogWriter(PrintWriter arg0) throws SQLException {
		// TODO Auto-generated method stub
		
	}

	public void setLoginTimeout(int arg0) throws SQLException {
		// TODO Auto-generated method stub
		
	}

}
原文地址:https://www.cnblogs.com/happyhacking/p/4310995.html