0%

两种数据库连接池 (2017/9/5)

2017年9月5日 上午9:56

前身:JDBC,通过属性配置文件封装DbUtiles类
这篇是对上一篇的改进。

概述:

要起到的效果:使用C3P0方式封装的DbUtils工具类,用来获取连接和关闭连接(让dao层去调用DbUtils工具类),也就是说,DbUtils成为了dao的下层,服务层调用的是dao层完成数据库操作,根本不知道DbUtils的存在。
使用:要与dao(数据持久层)共同使用,参照DAO层

  1. 这东西其实对于写代码来说感觉不出来啥,基本上和不用连接池技术时的写代码的感觉是一样的
  2. 但是,这对于数据库来说就不一样的,可以减轻他的压力,我们当然感觉不出来了
  3. 说明封装的好

DataSource

  1. DataSource (Java Platform SE 7 )
  2. DataSource是一个java接口,是实现连接池的关键,但是java中没有给出这个接口的实现类,所以我们要使用第三方的如DBCP和C3P0
  3. 作用是:A factory for connections to the physical data source ,An alternative to the DriverManager facility
  4. 使用:由于The DataSource interface is implemented by a driver vendor,所以要使用第三方vendor的jar包

现在有两种实现方法:DBCP和C3P0

这两种方法对于开发者来说,使用起来是一样的
他们的实现原理不同,但这与使用者来说无关

他们都有官方的手册查询(english)

第一步是引入的jar包


方式一:C3P0

C3P0连接池配置和实现详解 - pplsunny——舞动青春 - CSDN博客

文章解读:

  1. 其中的说的classPath是指的当前项目src目录下,一层都不能深
  2. 如果c3p0位置不在src目录下,要进行配置
  3. 配置代码为:
  4. System.setProperty("com.mchange.v2.c3p0.cfg.xml", System.getProperty("user.dir") + "/src/dbUtils/c3p0-config.xml");
  5. 其中的com.mchange.v2.c3p0.cfg.xml是c3p0中的默认属性,通过给这个属性赋值来说明当前c3p0-config.xml文件位置

关于C3P0的使用代码,看附录


方式二:DBCP

DBCP数据库连接池的简单使用
DBCP2配置详细说明(中文翻译) - 老黎的专栏 - CSDN博客

重点摘抄

应用程序中,使用完一个数据库连接后,DBCP连接池如何管理该连接
   分两种情况:
1. 应用程序中主动关闭该连接,即DBCPTest.java中第79行 conn.close();
1. 这种情况并不是手动将该连接关闭,而是将该连接交回给DBCP连接池,由连接池管理该连接。即用完连接后显示的将数据库连接提交至DBCP连接池。
2. 应用程序中不关闭该连接,即将DBCPTest.java中第79行 conn.close()注释掉
1. 这种情况DBCP配置文件dbcp.properties中的配置项(注意jar包版本,低版本中使用removeAbandoned=true配置项)
2. removeAbandonedOnMaintenance=true
3. removeAbandonedOnBorrow=true
4. removeAbandonedTimeout=1
5. 会起作用,removeAbandonedOnMaintenance=true和removeAbandonedOnBorrow=true表示DBCP连接池自动管理应程序中使用完毕的连接,removeAbandonedTimeout=1表示一个连接在程序中使用完毕后,若在1秒之内没有再次使用,则DBCP连接池回收该连接(通常removeAbandonedTimeout不会配置1,此处为了测试使用)。
3. 验证removeAbandonedOnMaintenance=true、removeAbandonedOnBorrow=true和removeAbandonedTimeout=1配置项的作用


导包排错

当我们在web项目中导入c3p0和mysql的时
第一种方式是:图一
第二种方式是:图二
如果出现图一方式不行,那么换用第二种方式。


图1

图2


附录:
使用C3P0方式封装的DbUtils工具类,用来获取连接和关闭连接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package dbUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;


/*
* 数据库工具类
*/
public class DbUtils {
//连接池对象
private static DataSource dataSource;
//数据库连接对象 Connection
private static Connection conn;
//操作数据库对象Statement
private static Statement sta;
//操作数据库预编译对象Preparstatement
private static PreparedStatement ps;

/*
* 静态代码块 类加载时完成数据初始化
*/
static {
ComboPooledDataSource cpds = new ComboPooledDataSource();
dataSource = cpds;
}
/*
* 获取Connection连接的方法
* @return conn
*/
public static Connection getConnection() {
try {
conn = dataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}

/*
* 关闭Connection连接的方法
*/
public static void close(){
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}

// public static void main(String[] args) {
// Connection conn = DbUtils.getConnection();
// System.out.println(conn);
// }
}