zoukankan      html  css  js  c++  java
  • Druid连接池入门使用和工具类封装

    Druid(德鲁伊):阿里巴巴提供的数据库连接池技术,是目前最好的数据库连接池。
    600+项目中使用,支持sql日志监控

    Java为数据库连接池提供了公共的接口: DataSource ,各个连接池厂商去实现这套接口,提供jar包。

    DataSource
        功能
           * 获取连接:
               Connection getConnection()  
           * 归还连接:
               connction.close()
                连接池厂商对connection对象的close()方法进行(增强),执行该方法时不是销毁对象,而是归还到连接池中
                    使用的动态代理技术进行增强的....

    Druid连接池快速入门

    // 1.创建druid数据源(连接池)对象
    DruidDataSource dataSource = new DruidDataSource();
    // 2.配置数据库基本四项,创建连接,初始化连接池容器
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://localhost:3306/web");
    dataSource.setUsername("root");
    dataSource.setPassword("root");
    
    // 指定初始化个数
    dataSource.setInitialSize(5);
    // 指定最大个数
    dataSource.setMaxActive(100);
    // 指定低峰期个数
    dataSource.setMinIdle(20);
    // 指定等待时间
    dataSource.setMaxWait(3000);
    
    // 3.从连接池中获得连接对象
    Connection connection = dataSource.getConnection();
    System.out.println(connection);
    
    // 4.归还到连接池
    connection.close(); // 此对象是druid提供的子类,实现了对close方法的增强,不再是销毁对象,而是归还到连接池

    Druid连接池配置文件读取

    driverClassName=com.mysql.jdbc.Driver
    url=jdbc:mysql://127.0.0.1:3306/databasename
    username=root
    password=root
    #初始化个数
    initialSize=5
    #最大连接数
    maxActive=10
    #等待时间,毫秒
    maxWait=3000
    #最少连接数
    minIdle=3
    // 通过类加载器,加载 druid.properties文件,获取io流
    InputStream in = DruidDemo2.class.getClassLoader().getResourceAsStream("druid.properties");
    Properties properties = new Properties();
    properties.load(in);
    
    // 通过druid工厂,创建druid连接池对象
    DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
    
    // 从连接池获取连接
    for (int i = 1; i <= 11; i++) {
       Connection connection = dataSource.getConnection();
       // 进行jdbc的 crud操作
       System.out.println(connection);
       // 模拟归还
       if(i== 10){
          connection.close();
       }
       // 连接池使用详情
       System.out.println(dataSource);
    }

    连接池工具类

    /*
        mybatis内置连接初始化代码....
     */
    public class JdbcUtils {
    
        // 声明连接池对象
        private static DataSource dataSource;
    
        // 1. 初始化连接池容器  static{}
        static {
            try {
                // 1.加载druid.properties 配置文件
                InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("druid.properties");
                Properties properties = new Properties();
                properties.load(in);
    
                // 2.通过druid的工厂,创建连接池对象
                dataSource = DruidDataSourceFactory.createDataSource(properties);
            } catch (Exception e) {
                throw new RuntimeException("druid连接池初始化失败...");
    
            }
        }
    
        // 2. 提供获取连接池对象的静态方法
        public static DataSource getDataSource() {
            return dataSource;
        }
    
        // 3. 提供获取连接对象的静态方法
        public static Connection getConnection() throws Exception {
            return dataSource.getConnection();
        }
    
        // 4. 提供释放资源的方法(conn对象不是销毁,而是归还到连接池)
        public static void release(ResultSet resultSet, Statement statement, Connection connection) {
            // 关闭ResultSet
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            // 关闭Statement
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            // 关闭Connection
            if (connection != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        // 方法重载
        public static void release(Statement statement, Connection connection) {
            release(null, statement, connection);
        }
    }
  • 相关阅读:
    网络编程实验一 win socket基础 获取服务器时间
    ASP.NET Core入门
    vue-element-template 获取后端路由表动态生成权限
    vue-admin-template只有一个子菜单时父级菜单不显示问题
    vue-element-template 本地使用proxy解决跨域问题
    已成功与服务器建立连接,但是在登录过程中发生错误。 (provider: SSL Provider, error: 0
    系统还未初始化,还不能审核期间以后的单据
    金蝶初始化问题
    jqueryMobile 动态添加元素,展示刷新视图方法
    链接数据库 远程事务的处理方式
  • 原文地址:https://www.cnblogs.com/xiaozhang666/p/13470887.html
Copyright © 2011-2022 走看看