数据库连接池(MySQL)

数据库连接池

1.传统连接图解

1.通过网络连接,最大连接数有限制,多个Java程序并发会瘫痪。

在这里插入图片描述
代码模拟:

public  void testCon(){
for (int i = 0;i<5000;i++){
Connection connection = JDBCUtils.getConnection();
JDBCUtils.close(null,null,connection);
}

此时不关闭,抛出Too many cnonections异常
当每一次都关闭时,耗时7秒。

2.传统连接问题分析

在这里插入图片描述
时间与网络质量有关。

3.数据库连接池

3.1基本介绍

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

数据库连接池示意图:
当连接池中连接都用完时,会进入等待队列,等待再连接。放回连接是不在引用连接,而不是断掉连接到数据库的线。
在这里插入图片描述

3.2数据库连接池种类

在这里插入图片描述
注:1.速度相对慢,只是相对,C3P0稳定性好用的也比较多。德鲁伊用的最多。
2.提供相应的接口,就是会有相应的Java包。

4.连接池C3P0的使用

1.c3p0的jar包拷贝到libs下并加入项目中。
2.在使用标准配置文件模板时,c3p0-confg.xml
内容如下

<?xml version="1.0" encoding="UTF-8" ?>
<c3p0-config>
        <!--数据源名称代表连接池-->
    <named-config name="testc3p0">
        <!--指定连接数据源的基本属性-->
        <!--用户名-->
        <property name="user">root</property>
        <!--密码-->
        <property name="password">123456</property>
<!--驱动类-->
        <property name="driverClass">com.mysql.cj.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/studb</property>
        <!--初始化数据库连接池时连接的数量-->
        <property name="initialPoolSize">5</property>
        <!--若数据库中连接数不足时,一次向数据库服务器申请多少个连接-->
        <property name="acquireIncremen">5</property>
        <!--数据库连接池中最大的数据库连接数-->
        <property name="maxPoolSize">20</property>
        <!--数据库连接池中最小的数据库连接数(空连对象个数为这个数时就去申请更多连接对象)-->
        <property name="minPoolSize">2</property>
    </named-config>
</c3p0-config>

代码演示:

注意: 以下演示两种连接方式,第一种不使用配置文件,自己获取连接时相关参数,passw、user、url等等。第二种使用配置文件,ComplooedDataSource中自动获取相关参数。直接连接,非常简便

package Jdbc.datasourse;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.Test;

import java.beans.PropertyVetoException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @author zq
 * 演示C3P0的使用
 *
 */
public class C3P0_ {
    @Test
    //方式一:程序中指定user、url、password等
    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\\Jdbc\\myjdbc\\mysql.properties"));
        //读取相关信息
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String url = properties.getProperty("url");
        String driver = properties.getProperty("driver");

        //给数据源 comboPolledDataSource 设置相关参数
        //连接管理由他管理
        comboPooledDataSource.setDriverClass(driver);
        comboPooledDataSource.setJdbcUrl(url);
        comboPooledDataSource.setUser(user);
        comboPooledDataSource.setPassword(password);

        //设置初始化连接数
        comboPooledDataSource.setInitialPoolSize(10);
        //设置最大连接数,最多能增加到多少
        comboPooledDataSource.setMaxPoolSize(50);
        //取出一个连接
        //测试连接池效率,测试连接mysql5000千次效率
        long start = System.currentTimeMillis();
        for (int i = 0; i < 5000; i++) {
            Connection connection = comboPooledDataSource.getConnection();
            //这个方法从DataSource接口实现

            connection.close();
        }
        System.out.println("连接成功");
        long end = System.currentTimeMillis();
        System.out.println(end - start);

    }
    @Test
    //使用配置文件模板连接c3p0-config.xml
//    将其拷贝到src下,该文件指定了连接数据库和连接池的相关参数
    public  void testC3PO_02() throws SQLException {
        //数据源
        ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource("testc3p0");
        //直接连接,ComPooledDataSource以及全部做完获取参数等
        //测试效率
        long start = System.currentTimeMillis();
        for (int i = 0; i < 5000; i++) {

            Connection connection = comboPooledDataSource.getConnection();
           connection.close();
        }
        long end = System.currentTimeMillis();
        System.out.println("连接成功");
        System.out.println(end - start);

    }

}


5.连接池德鲁伊的使用

1.同样加入德鲁伊的jar包到项目
下载地址:
https://repo1.maven.org/maven2/com/alibaba/druid/
2.加入配置文件druid.properties到src目录
druid.properties代码演示:

driverClassName=com.mysql.cj.jdbc.Driver

url=jdbc:mysql://localhost:3306/studb?user=root&password=123456&useUnicode=true


user=root
password=123456
initialSize = 10
maxActive=300
maxWait=60000
minTdle =5

代码演示:

package Jdbc.datasourse;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.junit.Test;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.util.ConcurrentModificationException;
import java.util.Properties;

/**
 * @author  zq
 */public class Druid_ {
     @Test
     public  void testDruid() throws Exception {
         //创建Prperties对象读取配置文件
         Properties properties = new Properties();
         properties.load(new FileInputStream("src\\druid.properties"));
//         创建一个指定参数的数据库连接池
         DataSource dataSource =
                 DruidDataSourceFactory.createDataSource(properties);
         Connection connection = dataSource.getConnection();
         System.out.println("连接成功");
         connection.close();

     }
}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值