文章目录
1. 数据库连接池概述
1.1 什么是数据库连接池
所谓的数据库连接池其实就是一个集合,集合中存储的就是Connection对象。
数据库连接池也是依赖数据库驱动创建连接的。(jdbc4大参数)
数据库连接池也叫数据源(DataSource)(也是jdbc标准(java.sql.*)中的一个)。
1.2 为什么要用数据库连接池
注意:
通过数据库连接池得到的连接和之前得到的连接是不一样的。
只有一个地方不一样!— close方法
不用数据库连接池得到的连接的close()方法是摧毁连接
用数据库连接池得到的连接的close()方法是归还连接到池
因为Connection对象的创建和销毁比较耗费系统资源,如果在一个项目中,频繁的创建和销毁Connection对象是非常不划算的一件事。所以就可以利用数据库连接池来集中管理数据库连接对象,需要的时候就从池子中取出来用,用完了再放回到池子中!
简单来说,就是为了实现Connection的复用!也就是重复使用的意思!
思考:如何归还不用的Connection对象?
con.close()//没用数据库连接池的时候,这个方法表示摧毁连接。但是现在,用了连接池,再调close()方法,就表示归还连接到池
1.3 数据库连接池的两类参数
- 池参数(有默认值可以不配)许多
- 初始大小:10个
- 最小空闲连接数:3个
- 增量:一次创建的最小单位(5个)
- 最大空闲连接数:12个 开始干掉连接
- 最大连接数:20个
- 最大的等待时间:1000毫秒/-1(不停等待)
- 连接参数(必须配)和之前的得到连接的参数是一样的
- classDriverName :驱动
- Url:
- Username:
- Password:
2. 数据库连接池的种类
2.1 DBCP(Database Connection Pool)
apach开源组织的产品
什么是DBCP数据库连接池
DBCP是Apache提供的一款开源免费的数据库连接池!
2.1.1 DBCP数据库连接池所需jar包
2.1.2 DBCP的使用格式一
演示代码:
@Test
public void DBCPmethod(){
//池对象
BasicDataSource ds = new BasicDataSource();
//设置参数
//设置连接相关参数(必须)
ds.setUsername("root");
ds.setPassword("root");
ds.setUrl("jdbc:mysql://localhost:3306/test");
ds.setDriverClassName("com.mysql.jdbc.Driver");
//设置池参数(可选)
ds.setMaxActive(20);
ds.setMaxIdle(10);
ds.setInitialSize(10);
ds.setMinIdle(2);
ds.setMaxWait(1000);
//获得连接对象
Connection con = null;
try {
con = ds.getConnection();
System.out.println(con);
//jdbc:mysql://localhost:3306/test, UserName=root@localhost, MySQL Connector Java
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
//归还连接给池
try {
con.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
2.1.3 DBCP的使用格式二(配置文件)
配置文件:dbcp.properties
username=root
password=root
url=jdbc:mysql://localhost:3306/test
driverClassName=com.mysql.jdbc.Driver
initialSize=10
maxActive=20
maxIdle=12
minIdle=2
maxWait=-1
演示代码:
@Test
public void DBCPmethod1() throws Exception {
//读取配置文件
InputStream in = new FileInputStream("dbcp.properties");
//加载配置文件到Properties对象中
Properties properties = new Properties();
properties.load(in);
//数据库连接池工厂类通过配置对象创建连接池对象(参数自动配置)
DataSource ds = BasicDataSourceFactory.createDataSource(properties);
//获得连接对象
Connection con = null;
try {
con = ds.getConnection();
System.out.println(con);
//jdbc:mysql://localhost:3306/test, UserName=root@localhost, MySQL Connector Java
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
//归还连接给池
try {
con.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
2.2 C3P0
2.2.1 所需jar包
c3p0数据库连接池的使用和dbcp一样,在使用之前需要导包!
2.2.2 使用方式一
@Test
public void C3P0method() throws PropertyVetoException {
//创建连接池对象
ComboPooledDataSource ds = new ComboPooledDataSource();
//配置连接参数
ds.setJdbcUrl("jdbc:mysql://localhost:3306/test");
ds.setUser("root");
ds.setPassword("root");
ds.setDriverClass("com.mysql.jdbc.Driver");
//配置池相关参数
ds.setAcquireIncrement(5);//增量
ds.setInitialPoolSize(20);//初始大小
ds.setMinPoolSize(2);
ds.setMaxPoolSize(50);
//得到连接对象
Connection con = null;
try {
con = ds.getConnection();
System.out.println(con);
//com.mchange.v2.c3p0.impl.NewProxyConnection@67f639d3
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
//归还连接对象
try {
con.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
2.2.3 使用方式二(配置文件)
使用配置文件的要求:(约定好的配置文件的要求,所以程序会自动读取)
- 必须叫c3p0-config.xml(名称的要求)
- 必须在src下(位置的要求)
配置文件:c3p0-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="user">root</property>
<property name="password">root</property>
<property name="acquireIncrement">3</property>
<property name="initialPoolSize">10</property>
<property name="minPoolSize">2</property>
<property name="maxPoolSize">10</property>
</default-config>
<named-config name="mysql-config">
<property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="user">root</property>
<property name="password">root</property>
<property name="acquireIncrement">3</property>
<property name="initialPoolSize">10</property>
<property name="minPoolSize">2</property>
<property name="maxPoolSize">10</property>
</named-config>
</c3p0-config>
演示代码:
@Test
public void C3P0method1() throws PropertyVetoException {
//创建连接池对象
//自动加载src下的c3p0-config.xml配置文件
ComboPooledDataSource ds = new ComboPooledDataSource();
//得到连接对象
Connection con = null;
try {
con = ds.getConnection();
System.out.println(con);
//com.mchange.v2.c3p0.impl.NewProxyConnection@413d1baf
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
//归还连接对象
try {
con.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
2.3 Druid(德鲁伊)
DRUID是阿里巴巴开源平台上一个数据库连接池实现,它结合了C3P0、DBCP、PROXOOL等DB池的优点.
和dbcp的使用形式几乎一样
2.3.1 jar包
2.3.2 使用方式一
@Test
public void Druidmethod(){
//池对象
DruidDataSource ds = new DruidDataSource();
//设置参数
//设置连接相关参数(必须)
ds.setUsername("root");
ds.setPassword("root");
ds.setUrl("jdbc:mysql://localhost:3306/test");
ds.setDriverClassName("com.mysql.jdbc.Driver");
//设置池参数(可选)
ds.setMaxActive(20);
ds.setMaxIdle(10);
ds.setInitialSize(10);
ds.setMinIdle(2);
ds.setMaxWait(1000);
//获得连接对象
Connection con = null;
try {
con = ds.getConnection();
System.out.println(con);
//com.mysql.jdbc.JDBC4Connection@1ed6388a
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
//归还连接给池
try {
con.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
2.3.3 使用方式二(配置文件)
@Test
public void Druidmethod1() throws Exception {
//读取配置文件
InputStream in = new FileInputStream("dbcp.properties");
//加载配置文件到Properties对象中
Properties properties = new Properties();
properties.load(in);
//数据库连接池工厂类通过配置对象创建连接池对象(参数自动配置)
DataSource ds = DruidDataSourceFactory.createDataSource(properties);
//获得连接对象
Connection con = null;
try {
con = ds.getConnection();
System.out.println(con);
//com.mysql.jdbc.JDBC4Connection@4f80542f
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
//归还连接给池
try {
con.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}