一 DBCP
- 概述
- DBCP(DataBase Connection Pool)數(shù)據(jù)庫連接池糯景,是java數(shù)據(jù)庫連接池的一種单默,由Apache開發(fā)够颠,通過數(shù)據(jù)庫連接池压语,可以讓程序自動(dòng)管理數(shù)據(jù)庫連接的釋放和斷開
- jar包
Commons-dbcp.jar
:連接池的實(shí)現(xiàn)
Commons-pool.jar
:連接池實(shí)現(xiàn)的依賴庫 - maven環(huán)境配置:
<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 的連接池正是采用該連接池來實(shí)現(xiàn)的褂傀。該數(shù)據(jù)庫連接池既可以與應(yīng)用服務(wù)器整合使用忍啤,也可由應(yīng)用程序獨(dú)立使用。
2 實(shí)現(xiàn)
(1)編碼方式實(shí)現(xiàn)連接池
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)配置方式實(shí)現(xiàn)連接池
- 配置文件(db.properties)
存放位置
db.properties(注意當(dāng)報(bào)錯(cuò)NullPointerException時(shí)仙辟,需要將文件添加到同級(jí)的classes文件中)配置文件中的key與BaseDataSouce屬性一樣
url=jdbc:mysql://localhost:3306/test
driverClassName=com.mysql.jdbc.Driver
username=root
password=root
initialSize=3
maxActive=6
maxIdle=3000
- 測(cè)試
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);
// 根據(jù)prop配置文件直接創(chuàng)建數(shù)據(jù)源對(duì)象
DataSource dataSource = BasicDataSourceFactory.createDataSource(prop);
// 獲取連接
Connection conn = dataSource.getConnection();
pstmt = conn.prepareStatement(" delete from student where id= ? ");
pstmt.setInt(1,12);
pstmt.executeUpdate();
// 關(guān)閉
conn.close();
}
二 C3P0技術(shù)(最常用的連接技術(shù))
1 概述
- 最常用的連接池技術(shù)同波!Spring框架,默認(rèn)支持C3P0連接池技術(shù)叠国!
- C3P0連接池未檩,核心類:CombopooledDataSource ds;
- 引入jar文件: c3p0-0.9.1.2.jar包
maven環(huán)境配置:
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
2 實(shí)現(xiàn)
(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;
//創(chuàng)建連接池核心工具類
ComboPooledDataSource cpds = new ComboPooledDataSource();
//連接池參數(shù)配置:初始化連接數(shù)粟焊,最大連接數(shù)
cpds.setJdbcUrl(url);
cpds.setDriverClass("com.mysql.jdbc.Driver");
cpds.setUser(user);
cpds.setPassword(password);
cpds.setInitialPoolSize(3);
cpds.setMaxPoolSize(6);
cpds.setMaxIdleTime(1000);
//從連接池對(duì)象中獲取連接對(duì)象
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文件中的根目錄下
- 測(cè)試
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();
}