04-数据库连接池

nobility 发布于 2021-08-22 613 次阅读


数据库连接池

由于申请数据库课连接是向数据库服务器的系统调用,频繁的系统调用导致程序的效率降低,所以需要在程序初始化时,一次申请多个连接对象,保存在一个连接池中,每次需要连接对象时从连接池中拿,用完放回池中,减少了系统调用,提高了程序效率

java定义了DataSource接口作为连接池的标准,由各大数据库厂商实现具体的连接池类,该接口方法Connection getConnection()用来从连接池中获取连接对象

连接池中的连接类与数据库厂商提供的连接类是不同的,他们重新写了一个连接类,所以在连接池中申请的连接对象调用void close()方法不在将连接释放掉,而是归还到连接池中

C3P0

  1. 需要引入相关jar包,需要引入相关jar包,从maven官网中查找c3p0并下载,以及下载相关依赖(mchange-commons-java),将包导入项目即可

  2. 在用户classPath下创建配置文件:c3p0.properties或c3p0-config.xml

<c3p0-config>
    <!-- 使用默认的配置读取连接池对象 -->
    <default-config>
        <!--  连接参数 -->
        <property name="driverClass">com.mysql.cj.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/test</property><!-- & 使用&amp;转义 -->
        <property name="user">root</property>
        <property name="password">root</property>

        <!-- 连接池参数 -->
        <property name="initialPoolSize">5</property><!--连接池初始化连接对象数量-->
        <property name="acquireIncrement">2</property><!--向数据库服务器一次申请的连接数量-->
        <property name="minPoolSize">2</property><!--连接池最少的连接数量,不够会向数据库申请-->
        <property name="maxPoolSize">10</property><!--连接池可存放最大的连接数量-->
        <property name="checkoutTimeout">3000</property><!--向连接池申请连接对象等待超时时间-->
    </default-config>
  
		<!--  使用test配置文件信息读取连接池对象 -->
    <named-config name="test">
        <!--  连接参数 -->
        <property name="driverClass">com.mysql.cj.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/dome</property>
        <property name="user">root</property>
        <property name="password">root</property>

        <!-- 连接池参数 -->
        <property name="initialPoolSize">5</property><!--连接池初始化连接对象数量-->
        <property name="acquireIncrement">2</property><!--向数据库服务器一次申请的连接数量-->
        <property name="minPoolSize">2</property><!--连接池最少的连接数量,不够会向数据库申请-->
        <property name="maxPoolSize">10</property><!--连接池可存放最大的连接数量-->
        <property name="checkoutTimeout">3000</property><!--向连接池申请连接对象等待超时时间-->
    </named-config>
</c3p0-config>
  1. 手工方式使用:此方法配合Properties可以使用任意的配置文件名
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
comboPooledDataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
comboPooledDataSource.setJdbcUrl("jdbc:mysql://122.51.213.160:3306/test");
comboPooledDataSource.setUser("root");
comboPooledDataSource.setPassword("root");

comboPooledDataSource.setAcquireIncrement(5);
//若数据库中连接数不足时, 一次向数据库服务器申请多少个连接
comboPooledDataSource.setInitialPoolSize(5);
//初始化数据库连接池时连接的数量
comboPooledDataSource.setMinPoolSize(2);
//数据库连接池中的最小的数据库连接数
comboPooledDataSource.setMaxPoolSize(10);
//数据库连接池中的最大的数据库连接数
Connection connection = comboPooledDataSource.getConnection();
//获取连接对象
  1. 获取连接对象
    1. new ComboPooledDataSource().getConnection()使用默认配置文件的连接池,获取连接对象
    2. new ComboPooledDataSource("test").getConnection()使用指定配置文件的连接池,获取连接对象

Druid

  1. 需要引入相关jar包,需要引入相关jar包,从maven官网中查找druid并下载,该包无依赖,将包导入项目即可
  2. 定义配置文件:可以是任意名字
# 连接参数
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false
username=root
password=root

# 连接池参数
initialSize=5
# 初始化连接数量
maxActive=10
# 最大连接数量
minIdle=2
# 最小连接数量
maxWait=3000
# 连接申请超时时间
  1. 加载配置文件,获取连接对象
InputStream inputStream = Main.class.getClassLoader().getResourceAsStream("druid.properties");
//反射获取配置文件输入流
Properties properties = new Properties();
properties.load(inputStream);
//将配置文件输入流加载到properties
DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
//将properties作为参数利用DruidDataSourceFactory工厂类创建连接池
Connection connection = dataSource.getConnection();
//从连接池中获取连接对象
此作者没有提供个人介绍
最后更新于 2021-08-22