java 测试连接C3P0 小案例

创建一个普通的Java 工程 在src 下创建我们的开发代码路径,引入jar 包,编写配置文件以及测试文件

mchange-commons-java-0.2.20.jar c3p0 需要依赖的jar

c3p0-0.9.5.5.jar  数据库连接池

mysql-connector-java-8.0.22.jar mysql 驱动

junit-4.10.jar  测试jar

hamcrest-core-1.3.jar  junit 依赖的jar

c3p0-config.xml  的内容

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
    <named-config name="mysql">
        <!-- 配置数据库用户名 -->
        <property name="user">root</property>
        <!-- 配置数据库密码 -->
        <property name="password">WA</property>
        <!-- 配置数据库链接地址 -->
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/study?useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=Asia/Shanghai</property>
        <!-- 配置数据库驱动 -->
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <!-- 数据库连接池一次性向数据库要多少个连接对象 -->
        <property name="acquireIncrement">20</property>
        <!-- 初始化连接数 -->
        <property name="initialPoolSize">10</property>
        <!-- 最小连接数 -->
        <property name="minPoolSize">5</property>
        <!--连接池中保留的最大连接数。Default: 15 -->
        <property name="maxPoolSize">30</property>
        <!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements 属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default:0 -->
        <property name="maxStatements">0</property>
        <!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->
        <property name="maxStatementsPerConnection">0</property>
        <!--c3p0是异步操作的,缓慢的JDBC操作通过帮助进程完成。扩展这些操作可以有效的提升性能 通过多线程实现多个操作同时被执行。Default:3 -->
        <property name="numHelperThreads">3</property>
        <!--用户修改系统配置参数执行前最多等待300秒。Default: 300 -->
        <property name="propertyCycle">3</property>
        <!-- 获取连接超时设置 默认是一直等待单位毫秒 -->
        <property name="checkoutTimeout">1000</property>
        <!--每多少秒检查所有连接池中的空闲连接。Default: 0 -->
        <property name="idleConnectionTestPeriod">3</property>
        <!--最大空闲时间,多少秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
        <property name="maxIdleTime">10</property>
        <!--配置连接的生存时间,超过这个时间的连接将由连接池自动断开丢弃掉。当然正在使用的连接不会马上断开,而是等待它close再断开。配置为0的时候则不会对连接的生存时间进行限制。 -->
        <property name="maxIdleTimeExcessConnections">5</property>
        <!--两次连接中间隔时间,单位毫秒。Default: 1000 -->
        <property name="acquireRetryDelay">1000</property>
    </named-config>
</c3p0-config>

数据库连接工具类

package com.study.c3p0;

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

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class C3P0Util {
    private static final ComboPooledDataSource ds = new ComboPooledDataSource("mysql");

    //获取数据源
    public static DataSource getDataSource() {
        return ds;
    }

    //获取一个连接
    public static Connection getConnection() throws SQLException {
        return ds.getConnection();
    }

    public static void close(Connection connection, PreparedStatement statement, ResultSet resultSet) {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

测试以及过程中遇到的问题

package com.study.c3p0;

import org.junit.Test;

import java.util.List;

public class Tester {
    @Test
    public void testFindAllCustomer() {

        //1:java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
        //        JUnit now uses the latest version of Hamcrest. Thus, you can use all the available matchers and benefit from an improved assertThat which will now print the mismatch description from the matcher when an assertion fails.
        //        junit.jar: Includes the Hamcrest classes. The simple all-in-one solution to get started quickly.Starting with version 4.11, Hamcrest is no longer included in this jar.
        //        hamcrest-core-1.3.jar 高版本不需要手动导入 低版本需要(4.10 及之前的版本)

        //2: java.lang.NoClassDefFoundError: com/mchange/v2/ser/Indirector  缺少c3p0 依赖的jar
        //是缺少 mchange-commons-java-版本号.jar包引起的错误注意:jar 包版本最好相适应。比如c3p0-0.9.5.2.jar与之相适应的是
        // mchange-commons-java-0.2.11.jar
        // https://mvnrepository.com下载

        //3: java.sql.SQLException: The server time zone value 的解决办法 mysql 8 的版本需要在数据库连接上加上serverTimezone=UTC  (这里也可以换成任意一个时区)
        //jdbc:mysql://localhost:3306/study?useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=UTC

        ICustomerService cs = new CustomerServiceImpl();
        List<Customer> list = cs.findAllCustomer();
        for(Customer c: list){
            System.out.println(c);
        }
    }
}

  • 18
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值