• MySQL之数据库连接池(Druid)


    数据库连接池

    每次创建数据库连接的问题

    获取数据库连接需要消耗比较多的资源,而每次操作都要重新获取新的连接对象,执
    行一次操作就把连接关闭,而数据库创建连接通常需要消耗相对较多的资源。这样数据库连接对象的使用率低。

    连接池的概念

    :连接池就是一个容器,连接池中保存了一些数据库连接,这些连接是可以重复使用的。

    连接池的原理

    启动连接池,连接池就会初始化一些连接

    当用户需要使用数据库连接,直接从连接池中取出

    当用户使用完连接,会将连接重新放回连接池中

    连接池好处

    连接池中会保存一些连接,这些连接可以重复使用,降低数据资源的消耗

    Druid

    Druid是阿里巴巴开发的号称为监控而生的数据库连接池,Druid是目前最好的数据库连接池。

    在功能、性能、扩展性方面,都超过其他数据库连接池,同时加入了日志监控,可以很好的监控数据库连接池和SQL的执行情况。

    Druid已经在阿里巴巴部署了超过600个应用,经过一年多生产环境大规模部署的严苛考验

    Druid地址:https://github.com/alibaba/druid

    Druid常用的配置参数

    initialSize 刚启动连接池时,连接池中包含连接的数量
    maxActive 连接池中最多可以放多少个连接
    maxWait 获取连接时最大等待时间,单位毫秒(超时则报错)

    Druid连接池使用步骤

    1.导入druid-1.0.0.jar的jar包

    2.编辑druid.properties

    3.加载properties文件的内容到Properties对象中

    4.创建Druid连接池,使用配置文件中的参数

    5.从Druid连接池中取出连接

    6.执行SQL语句

    7.关闭资源

    public static void main(String[] args) throws Exception {
    		Properties properties = new Properties();
    		//加载properties文件的内容到Properties对象中
    		properties.load(new FileReader("study\\src\\druid.properties"));
    		//创建Druid连接池,使用配置文件中的参数
    		DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
    		//从Druid连接池中取出连接
    		Connection connection = dataSource.getConnection();
    		//执行SQL语句
    		String sql = "INSERT INTO tb_user VALUES (null, ?, ?);";
    		PreparedStatement statement = connection.prepareStatement(sql);
    		statement.setString(1,"dog");
    		statement.setString(2,"8888");
    		int i = statement.executeUpdate();
    		System.out.println(i);
    		statement.close();
    		connection.close();
    
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    将获取数据库连接封装成工具类

    public class DruidDataSourceUtils {
        private static DataSource dataSource = null;
    
        static {
            try (FileReader fir = new FileReader("study\\src\\druid.properties")) {
                Properties properties = new Properties();
                properties.load(fir);
    
                dataSource = DruidDataSourceFactory.createDataSource(properties);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
    
        //对外暴露一个获取连接的方法
        //返回的结果可以尽量选择范围更大的
        //提供公共方法的时候,谁调用谁负责
        public static Connection getConnection() throws SQLException {
            return dataSource.getConnection();
        }
    }
    
    • 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

    根据工具类来获取连接

    public static void main(String[] args) throws SQLException {
            Connection connection = DruidDataSourceUtils.getConnection();
    
            String sql = "INSERT INTO tb_user VALUES (null, ?, ?);";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,"pig");
            preparedStatement.setString(2,"7777");
    
            int i = preparedStatement.executeUpdate();
            System.out.println(i);
    
            preparedStatement.close();
            connection.close();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    Druid中三个参数

    首先前5个连接是初始连接数,直接拿到

    第二个循环去获取连接,不会等待,直接去拿最大连接

    超过最大连接数再去获取连接,超过等待时间然后报错。

    若在等待时间内有连接释放,会获取到释放的连接

    public static void main(String[] args) throws SQLException {
            Connection con = null;
            //initialSize=5 初始化连接数
            for (int i = 0; i < 5; i++) {
    
                con = DruidDataSourceUtils.getConnection();
                System.out.println(con);
            }
            //maxActive=10 最大化连接10 先使用最大连接
            for (int i = 0; i < 5; i++) {
                con = DruidDataSourceUtils.getConnection();
                System.out.println(con);
            }
    
            con.close();
            // maxWait=2000 等待时间2000毫秒 10个连接都用完了,就没有连接可以用,等待之后直接报错
            con = DruidDataSourceUtils.getConnection();
            System.out.println(con);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    最后

    如果你对本文有疑问,你可以在文章下方对我留言,敬请指正,对于每个留言我都会认真查看。

  • 相关阅读:
    微信公众平台怎么添加秒杀活动
    面对以太坊合并,投资者有哪些参与策略?
    香港服务器一定比美国服务器好吗?
    智能微型断路器的功能有哪些?和网关搭配的作用在哪?
    设计模式:装饰器模式Decorator
    高速人工智能无人机首次击败世界冠军赛车手
    Java多并发(五)| JUC包下的并发容器(ConcurrentHashMap)& 并发工具类 & 原子变量类
    【Python编程】八、Python函数详解
    day04_java基础
    面试难题:Redis 分布式锁,真的完美无缺吗?
  • 原文地址:https://blog.csdn.net/weixin_47543906/article/details/128002978