数据库连接池的使用方法

数据库连接池概述

  基本介绍:

    1、预先在缓冲区防止一定数量的连接,当需要建立连接时,只需要在“缓冲区”中取出一个,使用完毕之后放回去。

    2、数据库连接池负责分配、管理和释放数据库的连接,它允许应用程序重复使用一个现有的数据库连接,而不是重新建立一个连接。

    3、当应用程序想连接池的请求连接超过最大数量时,这些请求将被加入到等待队列中

 

C3P0数据库连接池

    速度相对较慢,稳定性还不错(hibernate、spring)

复制代码
//方式一:在程序当中指定相关的参数user、url、password @Test public void testC3P0_01() throws IOException, PropertyVetoException, SQLException { //1、创建一个数据源对象 ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource(); //2、通过配置文件mysql.properties 获取相关的连接信息 Properties properties = new Properties(); properties.load(new FileInputStream("src\\mysql.properties")); //读取相关的属性值 String user = properties.get("user").toString(); String password = properties.get("password").toString(); String url = properties.get("url").toString(); String driver = properties.get("drive").toString(); //给comboPooledDataSource 设置相关的参数 //注意:连接管理是由 comboPooledDataSource 来进行管理 comboPooledDataSource.setDriverClass(driver); comboPooledDataSource.setJdbcUrl(url); comboPooledDataSource.setUser(user); comboPooledDataSource.setPassword(password); //设置初始化连接数 comboPooledDataSource.setInitialPoolSize(10); //最大连接数 comboPooledDataSource.setMaxPoolSize(10); Connection connection = null; long start = System.currentTimeMillis(); for (int i = 0; i < 5000; i++) { //这个方法就是从DataSource接口拿到连接 connection = comboPooledDataSource.getConnection(); connection.close(); } long end = System.currentTimeMillis(); System.out.println("c3p0 5000连接mysql耗时:" + (end - start)); //System.out.println("连接成功:" + connection); }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//第二种方式:使用配置文件模板来完成
//1、将c3p0提供的c3p0.config.xml 宝贝到src目录下
//2、该文件指定连接数据库和连接池的相关参数
@Test
public void testC3P0_02() throws SQLException {
    ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource("db_edu");
    Connection connection = null;
    long satrt = System.currentTimeMillis();
    for (int i = 0; i < 5000; i++) {
        connection = comboPooledDataSource.getConnection();
        connection.close();
    }
    long end = System.currentTimeMillis();
    System.out.println("耗时:" + (end - satrt));
 
}
复制代码
<!-- c3p0-config.xml配置文件 放置到项目的src目录下面 --> <!-- 数据源名称代表连接池 --> <?xml version="1.0" encoding="utf-8"?> <c3p0-config> <default-config> <!-- 数据源名称代表连接池 --> <name-config name="db_edu" /> <!-- 数据库的连接地址 --> <property name="jdbcUrl">jdbc:mysql://localhost:3306/db01</property> <!-- 驱动类 --> <property name="driverClass">com.mysql.jdbc.Driver</property> <!-- 用户名 --> <property name="user">root</property> <!-- 密码 --> <property name="password">123456</property> <!-- 每次增长的连接数 --> <property name="acquireIncrement">3</property> <!-- 初始化连接数 --> <property name="initialPoolSize">10</property> <!-- 最小的连接数 --> <property name="minPoolSize">2</property> <!-- 最大的连接数 --> <property name="maxPoolSize">10</property> </default-config> </c3p0-config>
复制代码

 

Druid(德鲁伊)数据库连接池

    Druid是阿里提供的数据库连接池,集DBCP、C3P0、Proxool优点与一身的数据库连接池

复制代码
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); //5、拿到连接 Connection connection = null; long start = System.currentTimeMillis(); for (int i = 0; i < 500000; i++) { connection = dataSource.getConnection(); connection.close(); } long end = System.currentTimeMillis(); System.out.println("Druid连接数据库5000次耗时:" + (end - start)); }
复制代码
复制代码
#druid.properties文件当中的配置 #加载驱动 driverClassName=com.mysql.jdbc.Driver #数据库的连接地址 url=jdbc:mysql://127.0.0.1:3306/db01?characterEncoding=utf-8 #连接数据库的用户名 username=root #连接数据库的密码。 password=123456 #初始化时池中建立的物理连接个数 initialSize=2 #最小的连接数 minIdle=5 #最大的连接数 maxActive=300 #最长的等待时间(毫秒) maxWait=5000
复制代码

 

基于Druid的封装的工具类

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
38
39
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 (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    //编写getConnection方法
    public static Connection getConnection() throws SQLException {
        return ds.getConnection();
    }
 
    //关闭连接,在连接池当中说的关闭连接,是将连接放入到连接池当中
    public static void closeConnection(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);
        }
    }
}

 


__EOF__

本文作者通过程序看世界
本文链接https://www.cnblogs.com/dbcxy/p/16941080.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   通过程序看世界  阅读(307)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
点击右上角即可分享
微信分享提示