zoukankan      html  css  js  c++  java
  • 创建数据库池实战

    数据库池

    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();
            }
        }
    }
    

      

  • 相关阅读:
    微信支付-微信公众号支付,微信H5支付,微信APP支付,微信扫码支付
    微信公众号支付开发当前URL未注册解决办法
    程序员头像
    jQuery简单实现iframe的高度根据页面内容自适应的方法(转)
    MySQL大表优化方案 Mysql的row_format(fixed与dynamic)
    PHP 测试杂项
    【转】MySQL的学习--触发器
    MYSQL PROCEDURE 测试用例
    CSS好看的按钮
    表格可在线编辑效果
  • 原文地址:https://www.cnblogs.com/chenweichu/p/8343761.html
Copyright © 2011-2022 走看看