【数据库】8、数据库连接池
目录
1 概念
1.1 连接池中连接的释放与使⽤原则
1.2 几种常用的连接池
2.C3P0
3.Druid 连接池
4.DBCP 连接池
1 概念
数据库连接池负责分配、管理和释放数据库连接,它允许应⽤程序重复使⽤⼀个现有的数据库连 接,⽽不是再重新建⽴⼀个;释放空闲时间超过最⼤空闲时间的数据库连接来避免因为没有释放 数据库连接⽽引起的数据库连接遗漏。数据库连接池是⼀个容器(集合),存放数据库连接的容器。当系统初始化好后,容器被创建,容器中会申请⼀些连接对象,当⽤户来访问数据库时,从容器 中获取连接对象,⽤户访问完之后,会将连接对象归还给容器。类似线程池。
1.1 连接池中连接的释放与使⽤原则
应⽤启动时,创建初始化数量的连接 当申请时⽆连接可⽤或者达到指定的最⼩连接数,按增量参数值创建新的连接
为确保连接池中最⼩的连接数的策略:
动态检查:定时检查连接池,⼀旦发现数量⼩于最⼩连接数,则补充对应的新连接,保 证连接池正常运转。
静态检查:空闲连接不⾜时,系统才检测是否达到最⼩连接数 按需分配,⽤过归还,空闲超时释放,获取超时报错。
1.2 几种常用的连接池
1. C3P0:⼀个开源的 JDBC 连接池,它实现了数据源和 JNDI 绑定。⽬前使⽤它的开源项⽬有 Hibernate、Spring等。
2. Druid:由阿⾥巴巴提供的数据库连接池实现技术,⽀持所有 JDBC 兼容的数据库,包括 Oracle、MySql等。
3. DBCP:也是⼀个开源的连接池,是Apache Common成员之⼀,也是tomcat内置的连接 池。
2.C3P0
1.配置文件
2.导入jar包
3.C3P0提供核⼼⼯具类:ComboPooledDataSource,如果要使⽤连接池,必须创建该类的 实例对象。
public class C3P0Utils {
private static DataSource dataSource = new ComboPooledDataSource("c3p0-config.xml");
public static DataSource getDataSource(){return dataSource;}
public static Connection getConnection() throws SQLException {
Connection connection = dataSource.getConnection();
return connection;}
}
3.Druid 连接池
1. 导⼊jar包:druid-1.1.16.jar
2. 定义配置⽂件
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/java2106?serverTimezone=UTC
username=root
password=123456
initialSize=5
maxActive=10
maxWait=3000
3.工具类
public class DruidUtils {
private static DataSource dataSource;
static {
Properties properties = new Properties();
try {
properties.load(DruidUtils.class.getResourceAsStream("Druid.properties"));
dataSource = DruidDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
e.printStackTrace();
}
}
public static DataSource getDataSource(){
return dataSource;
}
}
4.DBCP 连接池
1. 导⼊jar包 commons-dbcp2-2.1.1.jar commons-pool2-2.4.2.jar
2.配置文件
#连接设置
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/java2106?serverTimezone=UTC
username=root
password=123456
#
initialSize=10
#最大连接数量
maxActive=50
#
maxIdle=20
#
minIdle=5
#
maxWait=60000
#JDBC驱动建立连接时附带的连接属性属性的格式必须为这样:[属性名=property;]
#注意:"user" 与 "password" 两个属性会被明确地传递,因此这里不需要包含他们。
connectionProperties=useUnicode=true;characterEncoding=gbk
#指定由连接池所创建的连接的自动提交(auto-commit)状态。
defaultAutoCommit=true
#driver default 指定由连接池所创建的连接的事务级别(TransactionIsolation)。
#可用值为下列之一:(详情可见javadoc。)NONE,READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE
defaultTransactionIsolation=READ_UNCOMMITTED
3.获取数据库连接池对象:加载配置文件,通过⼯⼚来来获取 BasicDataSourceFactory
Properties properties = new Properties();
properties.load(Demo1.class.getResourceAsStream("dbcpconfig.properties"));
DataSource dataSource = BasicDataSourceFactory.createDataSource(properties);
Connection connection = dataSource.getConnection();
5.JdbcTemplate
JdbcTemplate是通过 SQL 语句 + 参数,模板化了编程。它是 Spring 框架中提供的⼀个对象,是 对原始 Jdbc API 对象的简单封装。
代码:
public class Demo1 {
//获取数据库链接池
private JdbcTemplate jdbcTemplate = new JdbcTemplate(DruidUtils.getDataSource());
@Test
public void updateTest(){
String sql = "insert into user2 values (?,?)";
jdbcTemplate.update(sql,4,"lqq");
}
@Test
public void Test2(){
String sql = "update user2 set username = '大帅哥' where id = 3";
int count = jdbcTemplate.update(sql);
System.out.println(count);
}
@Test
public void Test3(){
String sql = "delete from user2 where id = ?";
jdbcTemplate.update(sql,2);
}
@Test
public void Test4(){
String sql = "select * from emp where empno = ? /*or empno = ?*/";
Map
System.out.println(map);
}
@Test
//query 底层实现是RowMap
public void Test5(){
String sql = "select * from emp";
List
for (Emp emp:list) {
System.out.println(emp);
}
}
@Test
public void Test6(){
String sql = "select count(empno) from emp";
int total = jdbcTemplate.queryForObject(sql,int.class);
System.out.println(total);
}
}