JDBC数据库连接池
概述
前情提要:JDBC-MYSQL基础.
之前每次连接数据库都需要需要向系统申请连接,使用完就将释放连接,造成很多不必要的资源浪费,而且在申请连接时则耗时教长
采用连接池形式则可以避免上面的问题,连接池向系统申请多个连接对象供用户使用,用户需要使用就从连接池借一个连接对象,使用完后就把连接对象归还给连接池。类似以线程池
连接池为数据库公司去实现DataSource接口获取数据源
本文所有资源都可到百度云下载
链接:https://pan.baidu.com/s/1EaD33ph_40ZmuPRdPGe82A
提取码:kco6
C3P0连接池
比较为古老的一种连接池技术
- 导入jar包
- 定义配置文件
文件名:c3p0.properties 或者 c3p0-config.xml
要求:
- 文件名不可更改,系统会自动加载这两个文件
- 文件路径建议在src目录下
- 配置文件二选一
配置文件介绍(以c3p0-config.xml举例)
<c3p0-config>
<!-- 使用默认的配置读取连接池对象 -->
<default-config>
<!-- 连接参数 -->
<!-- 数据库驱动 -->
<property name="driverClass">com.mysql.jdbc.Driver</property>
<!-- 数据库路径 -->
<property name="jdbcUrl">jdbc:mysql:///db</property>
<!-- 账号 -->
<property name="user">root</property>
<!-- 密码 -->
<property name="password">root</property>
<!-- 连接池参数 -->
<!-- 初始化连接数量 -->
<property name="initialPoolSize">5</property>
<!-- 最大连接数量 -->
<property name="maxPoolSize">10</property>
<!-- 超时时间 -->
<property name="checkoutTimeout">3000</property>
</default-config>
<!--指定名称连接 -->
<named-config name="otherc3p0">
<!-- 连接参数 -->
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/db</property>
<property name="user">root</property>
<property name="password">root</property>
<!-- 连接池参数 -->
<property name="initialPoolSize">5</property>
<property name="maxPoolSize">8</property>
<property name="checkoutTimeout">1000</property>
</named-config>
</c3p0-config>
- 创建连接池对象
//默认连接池配置
ComboPooledDataSource combo = new ComboPooledDataSource();
//指定连接池配置
ComboPooledDataSource comboname = new ComboPooledDataSource("otherc3p0");
- 获取数据库连接对象
Connection connection = combo.getConnection();
- 操作数据
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("select * from a");
- 归还连接对象
如果Connection连接对象时从连接器获取的,则close不会关闭连接,而是将连接对象归还给连接池,以供用户再次使用
finally{
connection.close();
}
将归还连接放在finally代码块中,避免程序出错,连接归还不成功,导致的资源浪费
Druid连接池
现在最流行的数据库连接池之一,如阿里巴巴实现的。
-
导入jar包
-
定义配置文件
- 特定properties文件形式
- 可为任意名称,也可任意目录下存放,因为文件需要手动加载
#数据库驱动
driverClassName=com.mysql.jdbc.Driver
#数据库连接url
url=jdbc:mysql://127.0.0.1:3306/db
#数据库账号
username=root
#数据库密码
password=root
# 初始化连接数量
initialSize=5
# 最大连接数量
maxActive=10
# 超时时间
maxWait=3000
- 加载配置文件
可参考: java反射加载src目录下的文件.
InputStream path = DruidJDBC.class.getClassLoader().getResourceAsStream("druid.properties");
Properties pro = new Properties();
pro.load(path);
- 创建连接池对象
DataSource dataSource = DruidDataSourceFactory.createDataSource(pro);
- 获取数据库连接对象
Connection connection = dataSource.getConnection();
- 操作数据
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("select * from a");
- 归还连接对象
如果Connection连接对象时从连接器获取的,则close不会关闭连接,而是将连接对象归还给连接池,以供用户再次使用
finally{
connection.close();
}
将归还连接放在finally代码块中,避免程序出错,连接归还不成功,导致的资源浪费
Druid连接池工具类
Druid是比较常用且速度较快的连接池,可写一个工具类供用户长期使用
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Properties;
/**
* @program: JDBC
* @description: TODO
* @author: 吐鲁番
* @Date: 2020/9/1
* @Time: 19:55
**/
public class DruidUtils {
private static DataSource dataSource = null;
private static Connection connection = null;
static {
//加载配置文件
InputStream path = DruidJDBC.class.getClassLoader().getResourceAsStream("druid.properties");
Properties pro = new Properties();
try {
pro.load(path);
} catch (IOException e) {
e.printStackTrace();
}
try {
//获取数据源
dataSource = DruidDataSourceFactory.createDataSource(pro);
//获取数据库连接对象
connection = dataSource.getConnection();
} catch (Exception e) {
e.printStackTrace();
}
}
public static DataSource getDataSource() {
return dataSource;
}
public static Connection getConnection() {
return connection;
}
}