• 动态代理练习3自定义数据库连接池[connection动态代理]


    自定义数据库连接池[connection动态代理]

      

    1、代理类

     1 import java.lang.reflect.InvocationHandler;
     2 import java.lang.reflect.Method;
     3 import java.lang.reflect.Proxy;
     4 import java.sql.Connection;
     5 import java.sql.DriverManager;
     6 import java.sql.SQLException;
     7 import java.util.LinkedList;
     8 
     9 //    自定义连接池
    10 public class Pool {
    11 
    12     private static LinkedList<Connection> linkedList = new LinkedList<Connection>();
    13     static{
    14 //        在加载Pool类时,创建10个连接,并加入到连接池中
    15         for (int i = 0; i < 10; i++) {
    16             try {
    17                 Class.forName("com.mysql.jdbc.Driver");
    18                 String url = "jdbc:mysql://localhost:3306/bbs";
    19                 String user = "root";
    20                 String password = "123456";
    21                 Connection conn = DriverManager.getConnection(url, user, password);
    22 //                将连接添加到末尾
    23                 linkedList.addLast(conn);
    24             } catch (ClassNotFoundException e) {
    25                 e.printStackTrace();
    26             } catch (SQLException e) {
    27                 e.printStackTrace();
    28             }
    29         }
    30     }
    31 //    取得连接池中连接的个数
    32     public int getSize(){
    33         return linkedList.size();
    34     }
    35 //    取得一个空闲的连接,只能返回Connection的动态代理对象
    36     public Connection getConnection(){
    37         final Connection conn =linkedList.removeFirst();
    38         Class<?>[] interfaces =conn.getClass().getInterfaces();
    39         for(Class<?> clazz:interfaces){
    40             System.out.println(clazz.getName());
    41         }
    42         return (Connection) Proxy.newProxyInstance(
    43                 Pool.class.getClassLoader(), 
    44                 new Class[]{Connection.class}, 
    45                 new InvocationHandler() {
    46                     
    47                     public Object invoke(Object proxy, Method method, Object[] args)
    48                             throws Throwable {
    49                         
    50 //                        如果调用的是close()方法
    51                         if("close".equals(method.getName())){
    52 //                            将连接放回连接池
    53                             linkedList.addLast(conn);
    54 //                            放回null
    55                             return null;
    56                         }else{
    57                             return method.invoke(conn, args);
    58                         }
    59                     }
    60                 });
    61     }
    62     
    63 //    返回真实的Connection
    64     /*public Connection getConnection(){
    65         Connection conn = linkedList.removeFirst();
    66         return conn;//返回真实的Connection
    67     }*/
    68     
    69 }

    2、测试类

     1 import java.sql.Connection;
     2 import java.sql.SQLException;
     3 
     4 public class TestPool {
     5 
     6     public static void main(String[] args) throws SQLException {
     7         
     8 //        创建连接池
     9         Pool pool = new Pool();
    10 //        取得连接池中的连接个数
    11         System.out.println("连接个数为:"+pool.getSize());
    12 //        取得一个空闲的连接
    13         Connection conn = pool.getConnection();
    14 //        取得连接池中的连接个数
    15         System.out.println("连接个数为:"+pool.getSize());
    16 //        关闭连接对象,本质是将连接放回连接池
    17         conn.close();
    18         System.out.println("连接个数为:"+pool.getSize());
    19     }
    20 
    21 }
  • 相关阅读:
    BZOJ 1269 文本编辑器editor(伸展树)
    NOI 2017 整数(线段树)
    HAOI 2018 染色(容斥+NTT)
    HDU 5279 YJC plays Minecraft(NTT+分治)
    HDU 6088 Rikka with Rock-paper-scissors(NTT+欧拉函数)
    HDU 5552 Bus Routes(NTT+分治)
    HDU 4656 Evaluation(MTT)
    HDU 5829 Rikka with Subset(NTT)
    HDU 6061 RXD and functions(NTT)
    JOISC 2014 邮戳拉力赛(DP)
  • 原文地址:https://www.cnblogs.com/hacket/p/3053504.html
Copyright © 2020-2023  润新知