zoukankan      html  css  js  c++  java
  • 动态代理练习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 }
  • 相关阅读:
    java 在线网络考试系统源码 springboot mybaits vue.js 前后分离跨域
    springboot 整合flowable 项目源码 mybiats vue.js 前后分离 跨域
    flowable Springboot vue.js 前后分离 跨域 有代码生成器 工作流
    Flowable 工作流 Springboot vue.js 前后分离 跨域 有代码生成器
    java 企业 网站源码 后台 springmvc SSM 前台 静态化 代码生成器
    java 进销存 商户管理 系统 管理 库存管理 销售报表springmvc SSM项目
    基于FPGA的电子计算器设计(中)
    基于FPGA的电子计算器设计(上)
    FPGA零基础学习:SPI 协议驱动设计
    Signal tap 逻辑分析仪使用教程
  • 原文地址:https://www.cnblogs.com/hacket/p/3053504.html
Copyright © 2011-2022 走看看