1、由于传统数据库连接消耗大量的资源和时间,数据库的连接资源没有得到很好的利用
如果许多人频繁的进行数据库的连接,那么有可能导致服务器崩溃
2、所以就出现了数据库连接池技术,用来解决传统开发中数据库连接问题
1、思想
为数据库建立一个缓冲池,预先在缓冲池中放一定数量的连接,当需要用到的时候,便从缓冲池中取出一个,用完之后再放进去。
2、优点
- 资源重用
- 更快的系统反应速度
- 新的资源分配手段
- 统一的连接管理,避免数据库泄露
3、DataScore
- 俗称:数据源,包含连接池和连接池管理两部分,通常也叫连接池
- DataScore用来取代DriverManager来获取Connection,获取速度快
- DataScore只需要创建一个,他是产生连接的工厂
- 访问结束,也是要关闭连接,但是这个关闭只是把数据库释放掉,归还给数据库连接池
4、三种常用的数据库连接池
- CP30数据库连接池
private static DataScore cpds = new ComboPoolDataScore("helloc3p0");
public static Connection getConnection() throws SQLException{
Connection connnection = cpds.getConnection();
return connection;
}
//其中,src下配置文件为:c3p0-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<named-config name="helloc3p0">
<!-- 获取连接的4个基本信息 -->
<property name="user">root</property>
<property name="password">abc123</property>
<property name="jdbcUrl">jdbc:mysql:///test</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<!-- 涉及到数据库连接池的管理的相关属性的设置 -->
<!-- 若数据库中连接数不足时, 一次向数据库服务器申请多少个连接 -->
<property name="acquireIncrement">5</property>
<!-- 初始化数据库连接池时连接的数量 -->
<property name="initialPoolSize">5</property>
<!-- 数据库连接池中的最小的数据库连接数 -->
<property name="minPoolSize">5</property>
<!-- 数据库连接池中的最大的数据库连接数 -->
<property name="maxPoolSize">10</property>
<!-- C3P0 数据库连接池可以维护的 Statement 的个数 -->
<property name="maxStatements">20</property>
<!-- 每个连接同时可以使用的 Statement 对象的个数 -->
<property name="maxStatementsPerConnection">5</property>
</named-config>
</c3p0-config>
- DBCP数据库连接池:Apache软件基金组织下的开源连接池实现,Tomcat的连接池用这个连接池实现
前提:需要在系统增加两个jar文件
(1)Commons-dbcp.jar
(2)Commons-pool.jar
private static DataSource source = null;
static{
try {
Properties pros = new Properties();
InputStream is = DBCPTest.class.getClassLoader().getResourceAsStream("dbcp.properties");
pros.load(is);
//根据提供的BasicDataSourceFactory创建对应的DataSource对象
source = BasicDataSourceFactory.createDataSource(pros);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection2() throws Exception {
Connection conn = source.getConnection();
return conn;
}
//其中,src下的配置文件为:【dbcp.properties】
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?rewriteBatchedStatements=true&useServerPrepStmts=false
username=root
password=abc123
initialSize=1
- Druid数据库连接池(阿里研发)——可以说是目前最好的连接池之一
package com.atguigu.druid;
import java.sql.Connection;
import java.util.Properties;
import javax.sql.DataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
public class TestDruid {
public static void main(String[] args) throws Exception {
Properties pro = new Properties(); pro.load(TestDruid.class.getClassLoader().getResourceAsStream("druid.properties"));
DataSource ds = DruidDataSourceFactory.createDataSource(pro);
Connection conn = ds.getConnection();
System.out.println(conn);
}
}
//其中,src下的配置文件为:【druid.properties】
url=jdbc:mysql://localhost:3306/test?rewriteBatchedStatements=true
username=root
password=123456
driverClassName=com.mysql.jdbc.Driver
initialSize=10
maxActive=20
maxWait=1000
filters=wall