创建数据库池实战
数据库池
package com.pool.utils; /** * 数据库连接属性 */ public class DBConstant { public final static String DRIVER_CLASS ="com.mysql.jdbc.Driver"; public final static String URL ="jdbc:mysql:///dev_act"; public final static String USERNAME ="root"; public final static String PASSWORD = "123456"; public final static String INIT_SIZE = "4"; } package com.pool.utils; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; /** * 数据库连接工具类 * 获取数据库连接对象 */ public class DBConnUtil { static { try { Class.forName(DBConstant.DRIVER_CLASS); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public static Connection getConnection(){ Connection connection =null; //1、通过DriverManager创建连接对象 try { connection = DriverManager.getConnection(DBConstant.URL,DBConstant.USERNAME,DBConstant.PASSWORD); } catch (SQLException e) { e.printStackTrace(); } return connection; } } package com.pool.utils; import java.sql.*; import java.util.LinkedList; import java.util.Map; import java.util.Properties; import java.util.concurrent.Executor; /** * 对原始Connection对象进行包装 * 对close方法进行改写,调用它不是关闭连接而是归还连接到数据库池 */ public class MyConnectionWarpper implements Connection { private Connection conn; private LinkedList<Connection> pool; MyConnectionWarpper(Connection conn, LinkedList<Connection> pool){ this.conn = conn; this.pool = pool; } @Override public void close() throws SQLException { System.out.println("归还前:"+pool.size()); System.out.println(conn); pool.addLast(conn); System.out.println("归还后:"+pool.size()); } @Override public void commit() throws SQLException { conn.commit(); } //省略Connection接口其它方法,其余方法调用原始Connection接口方法就行了......... } package com.pool.utils; import java.sql.Connection; import java.util.LinkedList; /** * 什么时候释放连接池资源? * 通过监听器容器关闭时候清空MyDataSourcePool.pool =null; */ public class MyDataSourcePool { //多个连接对象用什么容器存储呢? private static LinkedList<java.sql.Connection> pool = new LinkedList<>(); private static Integer initSize; //第一次类加载到JVM上的时候 static{ initSize = Integer.valueOf(DBConstant.INIT_SIZE);; for(int i=0;i<initSize;i++){ pool.addLast(DBConnUtil.getConnection()); } } /** * 返回封装/装饰后的Connection对象 * 对原始Connection对象的close方法重新 * @return */ public Connection getConnection(){ if(pool.isEmpty()){ for(int i=0;i<initSize;i++){ pool.addLast(DBConnUtil.getConnection()); } } Connection conn = pool.removeFirst(); MyConnectionWarpper connWarpper = new MyConnectionWarpper(conn,pool); return connWarpper; } /** * 归还连接方法 */ public static void releaseConnection(Connection conn){ System.out.println("归还前:"+pool.size()); System.out.println(conn); pool.addLast(conn); System.out.println("归还后:"+pool.size()); } } package com.pool.utils; import org.junit.Test; import java.sql.Connection; import java.sql.SQLException; /** * 数据库池测试类 */ public class TestConn { @Test public void testConn(){ System.out.println(DBConnUtil.getConnection()); } public static void main(String[] args) { System.out.println(DBConnUtil.getConnection()); //创建连接池 MyDataSourcePool pool = new MyDataSourcePool(); Connection conn = pool.getConnection(); //pool.releaseConnection(conn); try { conn.close(); //不再是关闭连接对象,而是归还连接对象给数据库连接池 } catch (SQLException e) { e.printStackTrace(); } } }
感谢您的阅读,您的支持是我写博客动力。