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

来源:互联网 发布:淘宝招牌怎么上传 编辑:程序博客网 时间:2024/04/27 18:28

自定义数据库连接池[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 //                            放回null55                             return null;56                         }else{57                             return method.invoke(conn, args);58                         }59                     }60                 });61     }62     63 //    返回真实的Connection64     /*public Connection getConnection(){65         Connection conn = linkedList.removeFirst();66         return conn;//返回真实的Connection67     }*/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 }

 

 

0 0