数据库连接池的学习(三)——C3P0连接池

C3P0是一个开源的JDBC连接池,支持JDBC规范。目前使用它的开源项目有Hibernate,Spring等。

C3P0与DBCP的区别:

dbcp没有自动回收空闲连接的功能

c3p0有自动回收空闲连接功能

dbcp需要手动设置配置文件(自己写Java代码度配置文件)

c3p0不需要手动设置(自动读取配置文件)

需要导入包:c3p0-0.9.1.2.jar

首先需要添加配置文件,配置文件放在src下,文件名为:c3p0-config.xml,这两个都是固定的。

<?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?characterEncoding=utf-8
        </property>
        <property name="user">root</property>
        <property name="password">1234</property>
        <!--扩展配置 -->
        <!-- 连接超过30秒报错 -->
        <property name="checkoutTimeout">30000</property>
        <!--30秒检查空闲连接 -->
        <property name="idleConnectionTestPeriod">30</property>
        <!-- 初始化连接对象的数目 -->
        <property name="initialPoolSize">10</property>
        <!-- 30秒不适用丢弃 -->
        <property name="maxIdleTime">30</property>
        <!-- 数据库连接池最大连接数100 -->
        <property name="maxPoolSize">100</property>
        <!-- 数据库最小连接数 -->
        <property name="minPoolSize">10</property>
    </default-config>

    <!-- 命名的配置 -->
    <named-config name="oracle">
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/bookdb</property>
        <property name="user">root</property>
        <property name="password">123</property>
        <!-- 如果池中数据连接不够时一次增长多少个 -->
        <property name="acquireIncrement">5</property>
        <property name="initialPoolSize">20</property>
        <property name="minPoolSize">10</property>
        <property name="maxPoolSize">40</property>
    </named-config>


</c3p0-config>


再创建一个c3p0工具类:C3P0Utils

扫描二维码关注公众号,回复: 3460805 查看本文章

import com.mchange.v2.c3p0.ComboPooledDataSource;

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

public class C3P0Utils {
    //默认会读取类路径下一个配置文件:c3p0-config.xml
    private static ComboPooledDataSource cpds = new ComboPooledDataSource();
    public static Connection getConnection() {
        try {
            return cpds.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    //关闭资源
    public static void close(Connection conn){
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static void close(PreparedStatement st) {

        if (st != null) {
            try {
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void close(ResultSet set) {
        if (set != null) {
            try {
                set.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void close(Connection conn, PreparedStatement st, ResultSet rt) {
        close(conn);
        close(st);
        close(rt);
    }
}

数据库查询测试:

import org.junit.Test;

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

public class C3P0Test {
    @Test
    public void t() throws SQLException {
        Connection conn = C3P0Utils.getConnection();
        PreparedStatement ps = conn.prepareStatement("select * from students");
        ResultSet rs = ps.executeQuery();
        while(rs.next()){
            String sname = rs.getString("sname");
            System.out.println(sname);
        }

        C3P0Utils.close(conn,ps,rs);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_41061437/article/details/82858978