Java连接数据库---数据库连接池

数据库连接池作用

JDBC提供了通过Java来实现对数据库的操作,但是频繁的创建和销毁数据库连接会降低性能,如果创建大量连接可能使数据库运行缓缓或者崩溃。数据库连接池作用在于事先创建多个数据库连接存放在容器中,限制连接的数量保证数据库不过载,在需要获取连接来操作数据库时,不用临时创建数据库连接直接从容器中获取,提高了响应时间。

C3P0数据库连接池

c3p0提供了两种配置连接池的方式,一种是通过读取xml文件来读取配置信息,另一种是通过在代码中设置数据库连接信息进行配置。

第一种方式需要我们配置c3p0-config.xml文件:

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
<c3p0-config>
<!-- 使用默认的配置读取连接池对象 -->
<default-config>
<!-- 连接参数 -->
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/practice?characterEncoding=utf8</property>
<property name="user">root</property>
<property name="password">root</property>

<!-- 连接池参数 -->
<property name="initialPoolSize">5</property> <!-- 初始化后连接池大小 -->
<property name="maxPoolSize">10</property> <!-- 连接池扩展的最大容量 -->
<property name="checkoutTimeout">3000</property> <!-- 数据库连接超时时间 -->
</default-config>

<named-config name="otherc3p0">
<!-- 连接参数 -->
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/practice?characterEncoding=utf8</property>
<property name="user">root</property>
<property name="password">root</property>

<!-- 连接池参数 -->
<property name="initialPoolSize">5</property>
<property name="maxPoolSize">8</property>
<property name="checkoutTimeout">1000</property>
</named-config>
</c3p0-config>

使用C3P0通过封装只暴露获取连接和回收连接的接口:

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
public class C3P0Util {
private static final DataSource dataSource; // DataSource是Java提供的数据源接口规范,这里使用多态

static {
dataSource = new ComboPooledDataSource(); // 空参表示使用c3p0-config.xml的默认配置
// dataSource = new ComboPooledDataSource("otherc3p0"); // 可以指定name值来使其加载指定配置
}

public static Connection getConnection() throws SQLException {
return dataSource.getConnection(); // 返回连接
}

public static DataSource getDataSource(){
return dataSource; // 这里返回数据源实例对象是以后方便JdbcTemplate调用
}

public static void close(Connection conn, PreparedStatement ps, ResultSet rs) throws SQLException {
if (conn != null){
conn.close(); // 需要注意的是,这里的close是回收连接,而不是销毁,这一点是和直接使用JDBC有区别的
}

if (ps != null){
ps.close(); // 这里是直接销毁连接
}

if (rs != null){
rs.close(); // 这里也是销毁连接
}
}

public static void close(Connection conn, PreparedStatement ps) throws SQLException {
close(conn, ps, null);
}

}

对封装后的接口进行调用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class demo {
public static void main(String[] args) throws SQLException {

Connection conn = C3P0Util.getConnection();

PreparedStatement ps = conn.prepareStatement("SELECT * FROM USER");

ResultSet rs = ps.executeQuery();

while (rs.next()){
System.out.println(rs.getInt("id") + "..." + rs.getString("name") +
"..." + rs.getInt("age"));
}

C3P0Util.close(conn, ps, rs); // 回收连接

}
}

也可以通过手动配置来配置数据库驱动,数据库连接信息等:

1
2
3
4
5
6
7
8
9
10
11
12
public class demo {
public static void main(String[] args) throws PropertyVetoException, SQLException {

ComboPooledDataSource dataSource = new ComboPooledDataSource(); // 由于DataSource接口没有提供手动输入信息来配置连接池的方法,所以这里不使用多态,使用C3P0提供的手动配置方法

dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/practice");
dataSource.setUser("root");
dataSource.setPassword("root");

}
}

Druid数据库连接池

Druid是阿里开源的连接池组件,同样提供使用配置文件和手动输入配置信息来对连接池进行配置

使用druid.properties来配置连接池,文件名可以为任意名,这一点与C3P0自动加载必须使用规定xml名和格式是有区别的:

1
2
3
4
5
6
7
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/practice?characterEncoding=utf8
username=root
password=root
initialSize=5
maxActive=10
maxWait=3000

使用Druid通过封装只暴露获取连接和回收连接的接口:

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
public class DruidUtil {
private static DataSource dataSource; // 这里以便jdbcTemplate调用

static {

Properties properties = new Properties(); // 使用Properties来加载配置文件
try {
properties.load(DruidUtil.class.getClassLoader().getResourceAsStream("druid.properties"));
dataSource = DruidDataSourceFactory.createDataSource(properties); // 通过工厂模式实现DataSource接口

} catch (Exception e) {
e.printStackTrace();
}

}

public static DataSource getDataSource(){
return dataSource;
}

public static Connection getConnection() throws SQLException {
return dataSource.getConnection();
}

public static void close(Connection conn, PreparedStatement ps, ResultSet rs) throws SQLException {

if (conn != null){
conn.close();
}

if (ps != null){
ps.close();
}

if (rs != null){
rs.close();
}

}

public static void close(Connection conn, PreparedStatement ps) throws SQLException {
close(conn, ps, null);
}
}

调用方式和C3P0相同,也就不再记录。还可以通过手动输入配置信息来进行连接池配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class demo {
public static void main(String[] args) throws SQLException {

DruidDataSource dataSource = new DruidDataSource(); // 和C3P0一样,手动输入配置信息就不能受到DataSource约束
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/practice?characterEncoding=utf8");
dataSource.setUsername("root");
dataSource.setPassword("root");

Connection conn = dataSource.getConnection();

PreparedStatement ps = conn.prepareStatement("DELETE FROM USER WHERE ID = ?");
ps.setInt(1, 7);
int affectedRow = ps.executeUpdate();
System.out.println(affectedRow);

}
}