• C3P0和Druid数据库连接池的使用


    本次博客带领大家学习C3P0和Druid数据库连接池的使用。

    传统方式连接数据库

    • 连接5000次数据库的耗时。
    public void testCon(){
    
        //看看连接 - 关闭 connection 会耗用多久
        System.out.println("开始执行");
        long start = System.currentTimeMillis();
        for (int i=0;i<5000;i++){
            //使用传统的jdbc方式,得到连接
            Connection connection = JDBCUtils.getConnection();
    
            //关闭
            JDBCUtils.close(null,null,connection);
        }
        long end = System.currentTimeMillis();
        System.out.println("传统方式连接 5000次数据库 耗时="+(end-start));
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    C3P0数据库连接池的使用

    • 方式一:传入相关参数,在程序中指定user,url,password等。
    public void testC3P0_01() throws Exception {
        //1.创建一个数据源对象
        ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
        //2.通过配置文件mysql.properties 获取相关连接的信息。
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\mysql.properites"));
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String driver = properties.getProperty("driver");
        String url = properties.getProperty("url");
    
        //给数据源  comboPooledDataSource 设置相关的参数
        //注意:连接管理是由 comboPooledDataSource 来管理
        comboPooledDataSource.setDriverClass(driver);
        comboPooledDataSource.setJdbcUrl(url);
        comboPooledDataSource.setUser(user);
        comboPooledDataSource.setPassword(password);
        //设置初始化连接数
        comboPooledDataSource.setInitialPoolSize(10);
        comboPooledDataSource.setMaxPoolSize(50);
        //测试连接池的效率,测试对mysql 5000次操作
        System.out.println("开始执行");
        long start = System.currentTimeMillis();
        for (int i=0;i<5000;i++){
            Connection connection = comboPooledDataSource.getConnection();//这个方法就是从 DataSource 来实现
            connection.close();
        }
        long end = System.currentTimeMillis();
        System.out.println("c3p0 5000次连接mysql 耗时="+(end-start));
    }
    
    • 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
    • 方式二:使用配置文件模板来完成。
    
    <--!>xml配置文件--!>
    <c3p0-config>
      <default-config>
        <property name="driverClass">com.mysql.jdbc.Driverproperty>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/ld_db01property>
        <property name="user">rootproperty>
        <property name="password">rootproperty>
        
        <property name="initialPoolSize">5property>
        <property name="maxPoolSize">10property>
        <property name="checkoutTimeout">3000property>
      default-config>
    
      <named-config name="otherc3p0"> 
      named-config>
    c3p0-config>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    //1.将c3p0 提供c3p0-config.xml 拷贝到 src目录下
    //2. 该文件指定了连接数据库和连接池的相关参数
    @Test
    public void testC3P0_02() throws Exception {
        ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
        System.out.println("开始执行");
        long start = System.currentTimeMillis();
        for (int i=0;i<5000;i++){
            Connection connection = comboPooledDataSource.getConnection();
            connection.close();
        }
        long end = System.currentTimeMillis();
        System.out.println("c3p0 第二次方式 5000次连接mysql 耗时="+(end-start));
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    Druid数据库连接池的使用

    1. 加入 Druid jar包。

    2. 加入配置文件 druid.properties,将该文件拷贝项目的src目录。

    3. 创建Properties对象,读取配置文件。

    4. 创建一个指定参数的数据库连接池。

    driverClassName = com.mysql.jdbc.Driver
    url = jdbc:mysql://localhost:3306/ld_db01?rewriteBatchedStatements=true
    username = root
    password = root
    initialSize=10
    minIdle=5
    maxActive=20
    maxWait=5000
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    public void testDruid() throws Exception {
        //1.加入 Druid jar包
        //2. 加入配置文件 druid.properties,将该文件拷贝项目的src目录。
        //3. 创建Properties对象,读取配置文件。
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\druid.properties"));
    
        //4.创建一个指定参数的数据库连接池,Druid连接池
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
        System.out.println("开始执行");
        long start = System.currentTimeMillis();
        for (int i=0;i<500000;i++){
            Connection connection = dataSource.getConnection();
            connection.close();
        }
        long end = System.currentTimeMillis();
        System.out.println("druid连接池 500000次连接mysql 耗时="+(end-start));
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    Druid工具类

    • 将JDBCUtils工具类改成Druid(德鲁伊)实现。
    public class JDBCUtilsByDruid {
        private  static DataSource ds;
    
        //在静态代码块完成 ds初始化
        static {
            Properties properties = new Properties();
            try {
                properties.load(new FileInputStream("src\\druid.properties"));
                ds = DruidDataSourceFactory.createDataSource(properties);
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        //编写getConnection 方法
        public static Connection getConnection() throws SQLException {
            return ds.getConnection();
        }
    
        //关闭连接,在数据库连接池技术中,close不是真的断掉连接,而是把使用的Connection对象放回连接池
        public static void close(ResultSet resultSet, Statement statement,Connection connection){
            try {
                if (resultSet != null){
                    resultSet.close();
                }
                if (statement !=null){
                    statement.close();
                }
                if (connection !=null){
                    connection.close();
                }
            } catch (SQLException e) {
                throw  new RuntimeException(e);
            }
        }
    }
    
    • 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
    public class JDBCUtilsByDruid_USE {
        @Test
        public void testSelect(){
            System.out.println("使用Druid数据库连接池查询:");
            //1. 得到连接
            Connection connection = null;
    
            // 2.组织一个sql
            String sql = "select * from actor";
            PreparedStatement preparedStatement = null;
            ResultSet resultSet = null;
            //3.创建PrepareStatement对象
            try {
                connection = JDBCUtilsByDruid.getConnection();
                preparedStatement = connection.prepareStatement(sql);
                resultSet = preparedStatement.executeQuery();
                while(resultSet.next()){
                    int id = resultSet.getInt("id");
                    String name = resultSet.getString("name");
                    String sex = resultSet.getString("sex");
                    Date borndate = resultSet.getDate("borndate");
                    String phone = resultSet.getString("phone");
                    System.out.println(id+"\t"+name+"\t"+sex+"\t"+borndate+"\t"+phone);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                //关闭资源
                JDBCUtilsByDruid.close(null,preparedStatement,connection);
            }
        }
    }
    
    • 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
  • 相关阅读:
    JAVA 字节运算 取低5位 获取低位第一位
    AWS SAP-C02教程11-解决方案
    基于开源流批一体数据同步引擎ChunJun数据还原—DDL解析模块的实战分享
    STL- 函数对象
    告别if else,试试这款轻量级流程引擎吧,自带IDEA插件真香
    17、学习MySQL 事务
    排序算法:堆排序
    Playbook条件语句
    没那么简单的单例模式
    STM32使用PB3, PB4引脚的注意事项
  • 原文地址:https://blog.csdn.net/lidong777777/article/details/126797530