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);
        }
    }
  • 相关阅读:
    C++ handle(句柄类) part2
    C++代理类的使用
    第一个blog
    C++ Handle(句柄) part1
    关于理想团队的构建和对软件流程的理解
    提供就医帮助的安卓APP
    上海地铁游移动APP需求分析
    关于学习了《构建之法》的若干存在疑惑的问题
    安卓APP开发简单实例 结对编程心得
    Vue修改Vue项目运行端口号(CLI2)
  • 原文地址:https://www.cnblogs.com/xiaozhang666/p/13470887.html
Copyright © 2011-2022 走看看