• 1 jdbc连接池原理


    package com.msb.dao;
    
    import com.msb.util.PropertiesUtil;
    import org.apache.log4j.Logger;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.util.LinkedList;
    
    
    public class MyConnectionPool {
        private static String driver;
        private static String url;
        private static String user;
        private static String password;
        private static int initSize;
        private static int maxSize;
        private static Logger logger;
        private static LinkedList<Connection> pool;
    
        static{
            logger=Logger.getLogger(MyConnectionPool.class);
            // 初始化参数
            PropertiesUtil propertiesUtil=new PropertiesUtil("/jdbc.properties");
            driver=propertiesUtil.getProperties("driver");
            url=propertiesUtil.getProperties("url");
            user=propertiesUtil.getProperties("user");
            password=propertiesUtil.getProperties("password");
            initSize=Integer.parseInt(propertiesUtil.getProperties("initSize"));
            maxSize=Integer.parseInt(propertiesUtil.getProperties("maxSize"));
            // 加载驱动
            try {
                Class.forName(driver);
            } catch (ClassNotFoundException e) {
                logger.fatal("找不到数据库驱动类"+driver,e);
            }
            // 初始化pool
            pool=new LinkedList<Connection>();
            // 创建5个链接对象
            for (int i = 0; i <initSize ; i++) {
                Connection connection = initConnection();
                if(null != connection){
                    pool.add(connection);
                    logger.info("初始化连接"+connection.hashCode()+"放入连接池");
    
                }
            }
        }
    
        // 私有的初始化一个链接对象的方法
        private static Connection initConnection(){
            try {
                return DriverManager.getConnection(url,user,password);
            } catch (SQLException e) {
                logger.fatal("初始化连接异常",e);
            }
            return null;
        }
        // 共有的向外界提供链接对象的
        public static Connection getConnection(){
            Connection connection =null;
            if(pool.size()>0){
                connection= pool.removeFirst();// 移除集合中的第一个元素
                logger.info("连接池中还有连接:"+connection.hashCode());
            }else{
                connection = initConnection();
                logger.info("连接池空,创建新连接:"+connection.hashCode());
            }
            return connection;
        }
    
        // 共有的向连接池归还连接对象的方法
        public static void returnConnection(Connection connection){
            if(null != connection){
                try {
                    if(!connection.isClosed()){
    
                        if(pool.size()<maxSize){
                            try {
                                connection.setAutoCommit(true);// 调整事务状态
                                logger.debug("设置连接:"+connection.hashCode()+"自动提交为true");
                            } catch (SQLException e) {
                                e.printStackTrace();
                            }
                            pool.addLast(connection);
                            logger.info("连接池未满,归还连接:"+connection.hashCode());
                        }else{
                            try {
                                connection.close();
                               logger.info("连接池满了,关闭连接:"+connection.hashCode());
                            } catch (SQLException e) {
                                e.printStackTrace();
                            }
                        }
                    }else{
                       logger.info("连接:"+connection.hashCode()+"已经关闭,无需归还");
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }else{
               logger.warn("传入的连接为null,不可归还");
            }
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    package com.msb.util;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;
    
    public class PropertiesUtil {
        private Properties properties;
    
        public PropertiesUtil(String path){
            properties=new Properties();
            InputStream inputStream = this.getClass().getResourceAsStream(path);
            try {
                properties.load(inputStream);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public String getProperties(String key){
            return properties.getProperty(key);
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    提前创建的多个连接池connection对象放在队列中

  • 相关阅读:
    【Jetpack】Room 预填充数据 ( 安装 DB Browser for SQLite 工具 | 创建数据库文件 | 应用中设预填充数据对应的数据库文件 | 预填充数据库表字段属性必须一致 )
    简述事务隔离级别
    STM32MP157 I2C和SPI总线实验
    MySQL进阶_3.性能分析工具的使用
    TensorFlow之文本分类算法-3
    gitlab环境准备
    Android驱动框架整理之KERNEL框架
    按关键字搜索lazada商品接口(支持高并发请求多个国家站数据),代码对接教程
    Verilog功能模块——读写位宽不同的异步FIFO
    【从java到Go】Vue3打包发布到OSS
  • 原文地址:https://blog.csdn.net/weixin_39563769/article/details/133953533