文章目录
- 数据库连接池
- 1 C3P0 数据库连接池
- 2 DBCP 数据库连接池
- 3 Druid 数据库连接池
数据库连接池
- JDBC 的数据库连接池使用 javax.sql.DataSource 来表示,DataSource 只是一个接口,该接口通常由服务器(Weblogic, WebSphere, Tomcat)提供实现,也有一些开源组织提供实现:
- DBCP 是Apache提供的数据库连接池。tomcat 服务器自带dbcp数据库连接池。速度相对c3p0较快,但因自身存在BUG,Hibernate3已不再提供支持。
- C3P0 是一个开源组织提供的一个数据库连接池,**速度相对较慢,稳定性还可以。**hibernate官方推荐使用
- Proxool 是sourceforge下的一个开源项目数据库连接池,有监控连接池状态的功能,稳定性较c3p0差一点
- BoneCP 是一个开源组织提供的数据库连接池,速度快
- Druid 是阿里提供的数据库连接池,据说是集DBCP 、C3P0 、Proxool 优点于一身的数据库连接池,但是速度不确定是否有BoneCP快
- DataSource 通常被称为数据源,它包含连接池和连接池管理两个部分,习惯上也经常把 DataSource 称为连接池
- DataSource用来取代DriverManager来获取Connection,获取速度快,同时可以大幅度提高数据库访问速度。
- 特别注意:
- 数据源和数据库连接不同,数据源无需创建多个,它是产生数据库连接的工厂,因此整个应用只需要一个数据源即可。
- 当数据库访问结束后,程序还是像以前一样关闭数据库连接:conn.close(); 但conn.close()并没有关闭数据库的物理连接,它仅仅把数据库连接释放,归还给了数据库连接池。
附三种数据库连接池 jar 包,官网的下载地址:
https://blog.csdn.net/weixin_43671437/article/details/134141851?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22134141851%22%2C%22source%22%3A%22weixin_43671437%22%7D
1 C3P0 数据库连接池
1 导包
2 编写配置文件
创建 c3p0-config.xml 文件, 注意文件名字不能改。
<?xml version="1.0" encoding="utf-8" ?>
<c3p0-config>
<named-config name="helloC3P0">
<!-- 提供获取连接的4个基础信息 -->
<property name="driverClass">com.mysql.cj.jdbc.Driver</property> <!-- & xml文件中的 & 符号 -->
<property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbc_learn?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true</property>
<property name="user">root</property>
<property name="password">root</property>
<!-- 进行数据库连接池管理的基本信息-->
<!-- 当数据库连接池中的连接数不够时,C3P0一次性向数据库申请的连接数 -->
<property name="acquireIncrement">5</property>
<!-- c3p0数据库连接池初始化时的连接数 -->
<property name="initialPoolSize">10</property>
<!-- c3p0数据库连接池维护的最小的连接数 -->
<property name="minPoolSize">50</property>
<!-- c3p0数据库连接池维护的最大的连接数 -->
<property name="maxPoolSize">100</property>
<!-- c3p0数据库连接池最多维护的Statements的个数 -->
<property name="maxStatements">50</property>
<!-- 每个连接中可以最多使用的Statement的个数 -->
<property name="maxStatementsPerConnection">2</property>
</named-config>
</c3p0-config>
3 编写连接池代码
/**
* ClassName: C3P0Test
* Description:
* C3P0数据库连接池 测试类
* @Create 2023/11/1 18:36
* @Version 1.0
*/
public class C3P0Test {
// 方式一:
@Test
public void getConnection() throws PropertyVetoException, SQLException {
// 创建 C3P0 数据库连接池
ComboPooledDataSource cpds = new ComboPooledDataSource();
// 设置基本信息
cpds.setDriverClass( "com.mysql.cj.jdbc.Driver" ); //loads the jdbc driver
cpds.setJdbcUrl( "jdbc:mysql://localhost:3306/jdbc_learn?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true" );
cpds.setUser("root");
cpds.setPassword("root");
// 通过设置相关参数,对数据库连接池进行管理
// 设置数据库链接池中的链接数
cpds.setInitialPoolSize(10);
// 获取连接
Connection conn = cpds.getConnection();
System.out.println(conn);
// 销毁c3p0数据库连接池 一般不会做这个操作
// DataSources.destroy(cpds);
}
// 方式二:使用配置文件
@Test
public void getConnection2() throws Exception {
// // 创建 C3P0 数据库连接池
ComboPooledDataSource cpds = new ComboPooledDataSource("helloC3P0");
// 获取连接
Connection conn = cpds.getConnection();
System.out.println(conn);
}
}
2 DBCP 数据库连接池
1 导包
将这两个包导入项目内
2 编写配置文件
创建 dbcp.properties 文件, 注意文件名字不能改。
username=root
password=root
url=jdbc:mysql://localhost:3306/jdbc_learn?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true
driverClassName=com.mysql.cj.jdbc.Driver
initialSize=10
3 编写连接池代码
/**
* ClassName: DBCPTest
* Description: 测试DBCP数据库连接池
*
* @Create 2023/11/2 14:42
* @Version 1.0
*/
public class DBCPTest {
// 方式一: 不推荐
@Test
public void testGetConnection() throws SQLException {
// 创建DBCP的数据库连接池
BasicDataSource source = new BasicDataSource();
// 设置基本信息
source.setDriverClassName("com.mysql.cj.jdbc.Driver");
source.setUrl("jdbc:mysql://localhost:3306/jdbc_learn?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true");
source.setUsername("root");
source.setPassword("root");
// 还可以设置其他涉及数据库连接池的管理的属性
source.setInitialSize(10);
source.setMaxActive(10);
// 获取一个连接
Connection conn = source.getConnection();
System.out.println(conn);
}
// 方式二:使用配置文件 推荐
@Test
public void testGetConnection2() throws Exception {
Properties pros = new Properties();
// 读取配置文件
// 方式一:
//InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("dbcp.properties");
// 方式二:
FileInputStream is = new FileInputStream(new File("src/main/resources/dbcp.properties"));
pros.load(is);
// 创建数据库连接池
DataSource source = BasicDataSourceFactory.createDataSource(pros);
// 获取一个连接
Connection conn = source.getConnection();
System.out.println(conn);
}
}
3 Druid 数据库连接池
1 导包
2 编写配置文件
创建 druid.properties 文件, 注意文件名字不能改。
username=root
password=root
url=jdbc:mysql://localhost:3306/jdbc_learn?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true
driverClassName=com.mysql.cj.jdbc.Driver
initialSize=10
maxActive=10
3 编写连接池代码
/**
* ClassName: DruidTest
* Description:
* Druid数据库连接池 测试
* @Create 2023/11/2 15:31
* @Version 1.0
*/
public class DruidTest {
@Test
public void testGetConnection() throws Exception {
// 读取配置文件
Properties pros = new Properties();
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("druid.properties");
pros.load(is);
// 创建连接池
DataSource source = DruidDataSourceFactory.createDataSource(pros);
// 获取一个连接
Connection conn = source.getConnection();
System.out.println(conn);
}
}