Fork me on GitHub

c3p0 连接池的用法

本系列文章在 https://github.com/freestylefly/javaStudy 持(huan)续(ying)更(jia)新(ru)中,欢迎有兴趣的童鞋们关注。

c3p0连接池的用法

1、导入c3p0jar包

c3p0-0.9.1.2.jar

2、xml配置文件

在src的目录下, 创建一个名称为 c3p0-config.xml的配置文件, 配置信息如下:

1)最基本配置*四个连接数据库必须的参数)

1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://106.13.43.205:3306/test</property>
<property name="user">root</property>
<property name="password">aaaaa123</property>
</default-config>
</c3p0-config>

2)复杂的配置信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config>  
        <property name="driverClass">com.mysql.jdbc.Driver</property>  
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property>  
        <property name="user">root</property>  
        <property name="password">123456</property>  
             <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default:3 -->  
        <property name="acquireIncrement">5</property>  
             <!--初始化的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3-->  
        <property name="initialPoolSize">10</property>  
             <!--连接池中保留的最小连接数-->  
        <property name="minPoolSize">5</property>  
             <!--连接池中保留的最大连接数。Default:15 -->  
        <property name="maxPoolSize">20</property>  
             <!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->  
        <property name="acquireRetryAttempts">30</property>  
             <!--两次连接中间隔时间,单位毫秒。Default: 1000 -->  
        <property name="acquireRetryDelay">1000</property>  
             <!--连接关闭时默认将所有未提交的操作回滚。Default: false -->  
         <property name="autoCommitOnClose">false</property>  
    </default-config> 
</c3p0-config>

3、创建数据源DataSourceUtils(工具类)

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
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.canghe.utils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class DataSourceUtils {

private static DataSource dataSource = new ComboPooledDataSource();

private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();

// 直接可以获取一个连接池
public static DataSource getDataSource() {
return dataSource;
}

// 获取连接对象
public static Connection getConnection() throws SQLException {

Connection con = tl.get();
if (con == null) {
con = dataSource.getConnection();
tl.set(con);
}
return con;
}

// 开启事务
public static void startTransaction() throws SQLException {
Connection con = getConnection();
if (con != null) {
con.setAutoCommit(false);
}
}

// 事务回滚
public static void rollback() throws SQLException {
Connection con = getConnection();
if (con != null) {
con.rollback();
}
}

// 提交并且 关闭资源及从ThreadLocall中释放
public static void commitAndRelease() throws SQLException {
Connection con = getConnection();
if (con != null) {
con.commit(); // 事务提交
con.close();// 关闭资源
tl.remove();// 从线程绑定中移除
}
}

// 关闭资源方法
public static void closeConnection() throws SQLException {
Connection con = getConnection();
if (con != null) {
con.close();
}
}

public static void closeStatement(Statement st) throws SQLException {
if (st != null) {
st.close();
}
}

public static void closeResultSet(ResultSet rs) throws SQLException {
if (rs != null) {
rs.close();
}
}

}

4、使用DButils进行连接并对数据库进行操作

1
2
3
4
5
6
7
8
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from product";
List<Product> productList = null;
try {
productList = runner.query(sql, new BeanListHandler<Product>(Product.class));
} catch (SQLException e) {
e.printStackTrace();
}

本文章已同步至我的GitHub仓库:Javastudy,期待您的加入:blush:

本文章已同步至苍何的个人博客,可以直接在博客上留言哦:blush:

来我的微信公众号玩耍呗:blush:

扫码无套路关注我的CSDN博客:blush:

苍 何 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
-------------本文结束感谢您的阅读-------------
坚持原创技术分享,您的支持将鼓励我继续创作!