学习笔记 - 标准连接池

public class DBPool implements DataSource{
	private LinkedList<Connection> pool = new LinkedList<>();
	public DBPool() {
		try {
			Properties properties = new Properties();
			InputStream is = ConnectionUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
			properties.load(is);
			String driver = properties.getProperty("DRIVER");
			String url = properties.getProperty("URL");
			String user = properties.getProperty("USER");
			String password = properties.getProperty("PASSWORD");
			Class.forName(driver);
			for(int i =0;i<3;i++) {
				final Connection connection = DriverManager.getConnection(url, user, password);
				Object proxyObject = Proxy.newProxyInstance(DBPool.class.getClassLoader(), new Class[] {Connection.class}, new InvocationHandler() {
					@Override
					public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
						// TODO Auto-generated method stub
						if(method.getName().equals("close")) {
							synchronized (pool) {
								pool.add((Connection)proxy);
								pool.notify();
							}
							return null;
						}else {
							return method.invoke(connection,args);
						}
					}
				});
				pool.add((Connection)proxyObject);
			}
			
			
		}catch (Exception e) {
			// TODO: handle exception
			throw new RuntimeException();
		}
	}
	
	@Override
	public Connection getConnection() throws SQLException {
		synchronized(pool) {
			if(pool.size()==0) {
				try {
					pool.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return getConnection();
			}
		}
		
		return pool.removeFirst();
	}
}

自己实现一个连接池工具类

public class ConnectionUtils {

	//声明连接池
	private static LinkedList<Connection> pool = new LinkedList<>();
	//在静态代码块创建多个连接
	static {
		try {
			Properties properties = new Properties();
			InputStream is = ConnectionUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
			properties.load(is);
			String driver = properties.getProperty("DRIVER");
			String url = properties.getProperty("URL");
			String user = properties.getProperty("USER");
			String password = properties.getProperty("PASSWORD");
			Class.forName(driver);
			for(int i=0;i<3;i++) {
				//被代理的对象声明
				final Connection conn = DriverManager.getConnection(url, user, password);
				//创建代理类
				Object proxyObj = Proxy.newProxyInstance(ConnectionUtils.class.getClassLoader(), new Class[] {Connection.class}, 
						new InvocationHandler() {
							
							@Override
							public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
								// TODO Auto-generated method stub
							//拦截close方法
								if(method.getName().equals("close")) {
									//不关闭连接 而是放到pool里面
									synchronized (pool) {										
										pool.add((Connection)proxy);
										pool.notify();//唤醒
									}
									return null;
								}else {
									return method.invoke(conn, args);
								}
							}
						});
				
				//将代理的连接对象放到连接池
				pool.add((Connection)proxyObj);
			}

		}catch (Exception e) {
			throw new RuntimeException(e.getMessage(),e);
		}
	}
	
	/**
	 * 提供一个静态工厂 返回一个连接
	 * @return
	 */
	public static Connection getConn() {
		synchronized (pool) {
			if(pool.size()==0) {
				try {
					pool.wait();//等待直到还了一个后
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return getConn();
			}
			Connection connection = pool.removeFirst();
			System.out.println("size="+pool.size());
			return connection;
		}
	}
		
	
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wonder4work

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值