JAVA-JDBC连接池

315 阅读3分钟

连接池

  1. 预先在缓冲池中放入一定数量的连接,当需要连接数据库连接时,只需要从“缓冲池”中取出一个,使用完后再放回
  2. 数据库连接池负责分配,管理和释放数据库连接,它允许应用程序重复使用一个先用的数据库连接,而不是重新建立一个
  3. 当应用程序向连接池请求的连接数超过最大连接数量时,这些请求将会被加入到等待队列中
  4. 使用完后释放连接,并非是取消连接池某个连接跟数据库的连接,而是将该连接重新放回连接池。

多种连接池

  1. JDBC的数据库连接池使用javax.sql.DataSource来表示,DataSource只是一个接口,该接口通常需要由第三方提供实现(导入jar包)
  2. C3P0 数据库连接池,速度相对较慢,稳定性不错(hibernate,spring框架使用)
  3. DBCP数据库连接池,速度相对于C3P0较快,但不稳定
  4. Proxool数据库连接池,由监控连接池状态的功能,稳定性较C3P0差一点
  5. BoneCP数据库连接池,速度快
  6. Druid(德鲁伊)是阿里提供的数据库连接池,集DBCP、C3P0、Proxool 优点集一身的数据库连接池

C3P0连接池

使用时,需要到导jar包到项目中,并添加到库中

QQ图片20220906221634.png

使用方式

@Test
public void testC3P0_01() throws Exception {
    //1. 创建一个数据源对象
    ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
    Properties properties = new Properties();
    properties.load(new FileInputStream("src\\mysql.properties"));
    String user = properties.getProperty("user");
    String password = properties.getProperty("password");
    String url = properties.getProperty("url");
    String driver = properties.getProperty("driver");

    // 2. 设置相应参数
    comboPooledDataSource.setDriverClass(driver);
    comboPooledDataSource.setJdbcUrl(url);
    comboPooledDataSource.setUser(user);
    comboPooledDataSource.setPassword(password);

    // 初始化连接数
    comboPooledDataSource.setInitialPoolSize(10);
    // 最大连接数
    comboPooledDataSource.setMaxPoolSize(50);

    // 测试
    long start = System.currentTimeMillis();
    for (int i = 0; i < 5000; i++) {
        Connection connection = comboPooledDataSource.getConnection();

        //关闭连接
        connection.close();
    }
    long end = System.currentTimeMillis();

    System.out.println("连接5000次的耗时:"+(end-start));
    // 关闭连接池
    comboPooledDataSource.close();

    // 传统方式
    start = System.currentTimeMillis();
    for (int i = 0; i < 5000; i++) {
        Connection conn = JDBCUtils.getConn("src\\mysql.properties");
        conn.close();
    }
    end = System.currentTimeMillis();

    System.out.println("传统方式连接5000次的耗时:"+(end-start));
}

方式二 通过xml文件配置,代码更加简洁

@Test
public void testC3P0_02() throws SQLException {

    ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource("c3p0");

    long start = System.currentTimeMillis();
    for (int i = 0; i < 5000; i++) {
        Connection connection = comboPooledDataSource.getConnection();

        //关闭连接
        connection.close();
    }
    long end = System.currentTimeMillis();

    System.out.println("连接5000次的耗时:"+(end-start));

    comboPooledDataSource.close();
}

c3p0.config.xml解释

C3p0资源包都有c3p0.config.xml,一般放在src包下。配置时,只需修改黄色框内即可

QQ图片20220906222116-16624741850803.png

QQ图片20220906222116.png

Druid 连接池

QQ图片20220907112406.png

快速入门

public void test() throws Exception {
    //1. 导入jar包、druid.properties文件
    //2. 设置配置文件

    // 获取Properties文件
    Properties properties = new Properties();
    properties.load(new FileInputStream("src\\druid.properties"));

    // 使用德鲁伊工厂获取连接池对象
    DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);

    // 测试性能
    long l = System.currentTimeMillis();
    for (int i = 0; i < 5000000; i++) {
        Connection connection = dataSource.getConnection();
        connection.close();
    }
    long l1 = System.currentTimeMillis();
    System.out.println("连接5000000耗时:"+(l1-l));
}

JBDCUtilsByDruid 工具类

public class JDBCUtilsByDruid {
    private static DataSource ds;

    // 静态代码块初始化
    static {
        Properties properties = new Properties();


        try {
            properties.load(new FileInputStream("src\\druid.properties"));
            ds = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static Connection getConn(){
        try {
            return ds.getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public static void CloseConn(ResultSet set, Statement statement, Connection connection) {

        try {
            if (set != null) {
                set.close();
            }
            if (statement != null) {
                statement.close();
            }
            if (connection != null) {
                connection.close();

            }
        } catch (SQLException e) {

            throw new RuntimeException(e);
        }


    }
}