一 DBCP
- 概述
- DBCP(DataBase Connection Pool)数据库连接池,是java数据库连接池的一种,由Apache开发,通过数据库连接池,可以让程序自动管理数据库连接的释放和断开
- jar包
Commons-dbcp.jar
:连接池的实现
Commons-pool.jar
:连接池实现的依赖库 - maven环境配置:
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.6</version>
</dependency>
- 注释
Tomcat 的连接池正是采用该连接池来实现的。该数据库连接池既可以与应用服务器整合使用,也可由应用程序独立使用。
2 实现
(1)编码方式实现连接池
package utils;
import org.apache.commons.dbcp.BasicDataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/**
* Created by pc on 2017/9/10.
*/
public class PoolDBCP {
public static void main(String [] args) throws SQLException{
String url = "jdbc:mysql://localhost:3306/test";
String user = "root";
String password ="root";
PreparedStatement preparedStatement;
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl(url);
dataSource.setUsername(user);
dataSource.setPassword(password);
dataSource.setInitialSize(3);
dataSource.setMaxActive(6);
dataSource.setMaxIdle(3000);
Connection conn = dataSource.getConnection();
preparedStatement = conn.prepareStatement(" delete from student where id= ? ");
preparedStatement.setInt(1,13);
preparedStatement.executeUpdate();
conn.close();
}
}
(2)配置方式实现连接池
- 配置文件(db.properties)
存放位置
db.properties(注意当报错NullPointerException时,需要将文件添加到同级的classes文件中)配置文件中的key与BaseDataSouce属性一样
url=jdbc:mysql://localhost:3306/test
driverClassName=com.mysql.jdbc.Driver
username=root
password=root
initialSize=3
maxActive=6
maxIdle=3000
- 测试
public static void main(String[] args) throws Exception {
PreparedStatement pstmt;
// 加载prop配置文件
Properties prop = new Properties();
// 获取文件流
InputStream inStream = PoolDBCP.class.getResourceAsStream("db.properties");
// 加载属性配置文件
prop.load(inStream);
// 根据prop配置文件直接创建数据源对象
DataSource dataSource = BasicDataSourceFactory.createDataSource(prop);
// 获取连接
Connection conn = dataSource.getConnection();
pstmt = conn.prepareStatement(" delete from student where id= ? ");
pstmt.setInt(1,12);
pstmt.executeUpdate();
// 关闭
conn.close();
}
二 C3P0技术(最常用的连接技术)
1 概述
- 最常用的连接池技术!Spring框架,默认支持C3P0连接池技术!
- C3P0连接池,核心类:CombopooledDataSource ds;
- 引入jar文件: c3p0-0.9.1.2.jar包
maven环境配置:
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
2 实现
(1)编码方式,使用C3P0
package utils;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
/**
* Created by pc on 2017/9/10.
*/
public class PoolC3P0 {
public static void main (String [] args) throws Exception{
String url = "jdbc:mysql://localhost:3306/test";
String user = "root";
String password = "root";
PreparedStatement pstmt;
//创建连接池核心工具类
ComboPooledDataSource cpds = new ComboPooledDataSource();
//连接池参数配置:初始化连接数,最大连接数
cpds.setJdbcUrl(url);
cpds.setDriverClass("com.mysql.jdbc.Driver");
cpds.setUser(user);
cpds.setPassword(password);
cpds.setInitialPoolSize(3);
cpds.setMaxPoolSize(6);
cpds.setMaxIdleTime(1000);
//从连接池对象中获取连接对象
Connection conn = cpds.getConnection();
pstmt = conn.prepareStatement(" delete from student where id= ? ");
pstmt.setInt(1,6);
pstmt.executeUpdate();
conn.close();
}
}
(2)配置方式,使用C3P0[重要]
- c3p0-config.xml
<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="initialPoolSize">3</property>
<property name="maxPoolSize">6</property>
<property name="maxIdleTime">1000</property>
</default-config>
</c3p0-config>
需要将文件添加到classes文件中的根目录下
- 测试
public static void main(String [] args) throws Exception{
PreparedStatement preparedStatement;
ComboPooledDataSource dataSource = new ComboPooledDataSource();
Connection conn = dataSource.getConnection();
preparedStatement = conn.prepareStatement("delete from student where id= ? ");
preparedStatement.setInt(1,5);
preparedStatement.executeUpdate();
conn.close();
}