4.连接池_手写连接池_动态代理
二、数据库连接池
很多很多的连接
放进一个池子里用集合来存取这些连接
手写连接池:
改造conn的close方法
1.继承
2.装饰
3.动态代理
package com.itheima.pool; 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.DriverManager; import java.sql.SQLException; import java.util.LinkedList; import java.util.List; import javax.sql.DataSource; public class MyPool implements DataSource { private static List<Connection> pool = new LinkedList<Connection>(); static{ try{ Class.forName("com.mysql.jdbc.Driver"); for(int i=0;i<5;i++){ Connection conn = DriverManager.getConnection("jdbc:mysql:///day11","root","root"); pool.add(conn); } }catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } public Connection getConnection() throws SQLException { if(pool.size()==0){ for(int i=0;i<3;i++){ Connection conn = DriverManager.getConnection("jdbc:mysql:///day11","root","root"); pool.add(conn); } } final Connection conn = pool.remove(0); //--利用动态代理改造close方法 Connection proxy = (Connection) Proxy.newProxyInstance(conn.getClass().getClassLoader(), conn.getClass().getInterfaces(), new InvocationHandler(){ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if("close".equals(method.getName())){ //对于想要改造的close方法,我们自己写 retConn(conn); return null; }else{ //对于不想改造的方法调用被代理者身上相同的方法 return method.invoke(conn, args); } } }); System.out.println("获取了一个连接,池里还剩余"+pool.size()+"个连接"); return proxy; } private void retConn(Connection conn){ try { if(conn!=null && !conn.isClosed()){ pool.add(conn); System.out.println("还回了一个连接,池里还剩余"+pool.size()+"个连接"); } } catch (SQLException e) { e.printStackTrace(); } } public Connection getConnection(String username, String password) throws SQLException { // TODO Auto-generated method stub return null; } public PrintWriter getLogWriter() throws SQLException { // TODO Auto-generated method stub return null; } public int getLoginTimeout() throws SQLException { // TODO Auto-generated method stub return 0; } public void setLogWriter(PrintWriter out) throws SQLException { // TODO Auto-generated method stub } public void setLoginTimeout(int seconds) throws SQLException { // TODO Auto-generated method stub } public boolean isWrapperFor(Class<?> iface) throws SQLException { // TODO Auto-generated method stub return false; } public <T> T unwrap(Class<T> iface) throws SQLException { // TODO Auto-generated method stub return null; } }
时间: 2024-10-07 20:17:43