JDBC连接池
使用连接池的原因
- 数据库连接的建立及关闭资源消耗巨大
- 传统数据库访问方式:一次数据库访问对应一个物理连接,每次操作数据库都要打开、关闭该物理连接,系统性能严重受损。
数据库连接池(Connection Pool)
- 系统初始运行时,主动建立足够的连接,组成一个池。每次应用程序请求数据库连接时,无需重新打开连接,而是从池中取出已有的连接,使用完后,不再关闭,而是归还。
连接池中连接的释放与使用原则
-
应用启动时,创建初始化数量的连接
-
当申请时无连接可用或者达到指定的最小连接数,按增量参数值创建新的连接
-
为确保连接池中最小的连接数的策略:
-
动态检查:定时检查连接池,一旦发现数量小于最小连接数,则补充对应的新连接,保证连接池正常运转
-
静态检查:空闲连接不足时,系统才检测是否达到最小连接数
-
-
按需分配,用过归还,空闲超时释放,获取超时报错
-
连接池也只是接口,具体实现由厂商来完成。
DataSource实现类:C3P0,Druid,Apache DBCP
C3P0连接池配置
- C3P0开源免费的连接池!目前使用它的开源项目有:Spring、Hibernate等。使用第三方工具需要导入jar包,c3p0使用时还需要添加配置文件c3p0-config.xml。
步骤:
- 使用Maven的在pom.xml文件中加入:
<!-- https://mvnrepository.com/artifact/c3p0/c3p0 -->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.mchange/mchange-commons-java -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>mchange-commons-java</artifactId>
<version>0.2.20</version>
</dependency>
- 手动导入包
-
配置文件
- 在项目src(类路径)下创建配置文件c3p0-config.xml文件
- 配置文件内容:
<c3p0-config> <!-- 使用默认的配置读取连接池对象 --> <default-config> <!-- 连接参数 --> <property name="driverClass">注册驱动对象</property> <property name="jdbcUrl">jdbc:数据库类型://ip地址:端口/数据库名</property> <property name="user">数据库用户名</property> <property name="password">数据库密码</property> <!-- 连接池参数 --> <!-- 如果池中数据库连接不够时一次增长多少个 --> <property name="acquireIncrement">5</property> <!-- 初始化连接数 --> <property name="initialPoolSize">5</property> <!-- 最小连接数 --> <property name="minPoolSize">10</property> <!-- 最大连接数 --> <property name="maxPoolSize">10</property> <!-- JDBC的标准参数,用以控制数据源内加载的PreparedStatement数量 --> <property name="maxStatements">0</property> <!-- 连接池内单个连接所拥有的最大缓存statement数--> <property name="maxStatementsPerConnection">5</property> <!-- 配置当连接池所有连接用完时应用程序getConnection的等待时间。为0则无限等待直至有其他连接释放或者创建新的连接。--> <property name="checkoutTimeout">3000</property> </default-config> </c3p0-config>
- 常见配置项
编写工具类:
C3P0提供核心工具类:ComboPooledDataSource,如果要使用连接池,必须创建该类的实例对象。
- new ComboPooledDataSource(“名称”); 使用配置文件“命名配置”
<named-config name="mysql">
- new ComboPooledDataSource(); 使用配置文件“默认配置”
<default-config>
DBCP连接池
DBCP也是一个开源的连接池,是Apache Common成员之一,在企业开发中也比较常见,tomcat内置的连接池。
步骤
- 使用Maven的在pom.xml文件中加入:
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.7.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.9.0</version>
</dependency>
- 手动导入包
- 配置文件(文件位置任意)
- 配置文件内容:properties不能编写中文,不支持在STS中修改,必须使用记事本修改内容,否则中文注释就乱码了
#连接设置
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/jdbc
username=root
password=root
#初始化连接
initialSize=10
#最大连接数量
maxActive=50
#最大空闲连接
maxIdle=20
#最小空闲连接
minIdle=5
#超时等待时间以毫秒为单位 6000毫秒/1000等于60秒
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
注:mysql 6.0+用com.mysql.cj.jdbc.Driver
- 编写工具类
public class DbcpUtils {
private static String driverName=null;
private static DataSource dataSource = null;
static {
Properties properties = new Properties();
try {
properties.load(DruidUtils.class.getResourceAsStream("dbcpconfig.properties"));
dataSource = BasicDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
public static void close() throws SQLException {
getConnection().close();
}
}
Druid连接池
Druid是阿里开源的数据库连接池,作为后起之秀,性能比dbcp、c3p0更高,使用也越来越广泛。当然Druid不仅仅是一个连接池,还有很多其他的功能。
步骤:
- 使用Maven的在pom.xml文件中加入:
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.24</version>
</dependency>
- 配置文件
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/db3
username=root
password=root
##初始连接数,默认0
initialSize=10
#最大连接数,默认8
maxActive=30
#最小闲置数
minIdle=10
#获取连接的最大等待时间,单位毫秒
maxWait=2000
#缓存PreparedStatement,默认false
poolPreparedStatements=true
#缓存PreparedStatement的最大数量,默认-1(不缓存)。大于0时会自动开启缓存PreparedStatement,所以可以省略上一句设置
maxOpenPreparedStatements=20
注:mysql 6.0+用com.mysql.cj.jdbc.Driver
- 编写工具类
public class DruidUtils {
private static String driverName = null;
private static DataSource dataSource = null;
static {
Properties properties = new Properties();
try {
properties.load(DruidUtils.class.getResourceAsStream("druid.properties"));
dataSource = DruidDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
public static void close() throws SQLException {
getConnection().close();
}
}