jdk 动态代理 数据连接池

package com.itheima.datasource;

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.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import com.itheima.utils.JdbcUtil;

/**
 * 引入Sun标准数据源接口   为了更多的人去用
 *   DataSource  :javax.sql.DataSource
 * 
 * @author wangli
 *
 */
public class MyDataSource3 implements DataSource {
    public static  List<Connection> pool = new ArrayList<Connection>();//池子
    private static int size=10;
    public static void setSize(int size) {
        MyDataSource3.size = size;
    }
    
    //初始化池中的连接
    static{
        try {
            for (int i = 0; i < size; i++) {
                pool.add(JdbcUtil.getConnection());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }
    
    //统一提供方法,用于从池中获取连接
    public  synchronized Connection getConnection(){
        if(pool.size()>0){
            final Connection con =  pool.remove(0);//删除这个连接,因为别人不能此时不能再用它 final 用匿名内部类里 con代表数据连接池里的池连接接口
            
            //代理模式   JDK自带的相应代理相关类
            //Proxy动态代理相关类
            //Proxy
            //        newProxyInstance(ClassLoader cl,Class<?>[] interfaces,InvocationHandler ih)
            // ClassLoader  :代表类加载器   被代理的对象用什么类加载器,代理对象就要用什么加载器
            //interfaces    :代表所实现的接口,被代理对象实现什么接口,代理对象就要实现什么接口
            //InvocationHandler  处理器  就是要处理相关的緢节问题  一般有一个匿名内部类
                             //InvocationHandler 策略模式
            Connection proxyConn = (Connection) Proxy.newProxyInstance(con.getClass().getClassLoader(), con.getClass().getInterfaces(),
                     new InvocationHandler() {
                        
                        @Override
                        public Object invoke(Object proxy, Method method, Object[] args)
                                throws Throwable {
                            if("close".equals(method.getName())){
                                //对于close方法要改写   添加到池中
                                return pool.add(con);//加入池中
                            }else{
                                //对于其它方法,用mysql的connection对象的底层实现
                                return method.invoke(con, args); 被代理对象和,被代理对象的方法里的参数
                            }
                        }
                    }
                    );
            return proxyConn;
        }else{
            throw new RuntimeException("池中无连接");
        }
        
    }
    
    //还回一个连接到池中
    public static void close(Connection con){
        pool.add(con);//将这个连接回收到池中
    }
    
    @Override
    public PrintWriter getLogWriter() throws SQLException {
        // TODO Auto-generated method stub
        return null;
    }

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

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

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

    @Override
    public <T> T unwrap(Class<T> iface) throws SQLException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        // TODO Auto-generated method stub
        return false;
    }



    @Override
    public Connection getConnection(String username, String password)
            throws SQLException {
        // TODO Auto-generated method stub
        return null;
    }
    
    
    
}
原文地址:https://www.cnblogs.com/baijin05/p/5075011.html