JDBC连接池c3p0连接异常和日志处理

1.c3p0连接数据库由于时区没限制报出的错误

 代码:

package C3P0;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.mchange.v2.log.ModifyLogLevel;
import org.junit.Test;

import javax.sql.DataSource;
import java.sql.Connection;
import java.util.logging.Level;

public class C3P0_test {
    
    public static Connection getConnection1() throws Exception{
        ComboPooledDataSource cpds = new ComboPooledDataSource();
        cpds.setDriverClass("com.mysql.cj.jdbc.Driver");
        cpds.setJdbcUrl("jdbc:mysql://localhost:3306/library");
        cpds.setUser("root");
        cpds.setPassword("123456");

//	cpds.setMaxPoolSize(100);

        Connection conn = cpds.getConnection();
        return conn;
    }

    public static void main(String[] args) throws Exception {
        System.out.println(getConnection1());
        /*DataSource cpds = new ComboPooledDataSource("helloc3p0");
        Connection conn = cpds.getConnection();
        System.out.println(conn);*/
    }
}

处理方式:需要在url后添加:?serverTimezone=UTC

 处理后就能正常执行

 八月啥不是在报错是代码执行报出的日志,处理方式在自己对应的项目下创建一个与日志同名的包,代码如下:

创建完成后,在对应的主方法中调用该方法就能完美处理日志(将调用部分放在代码块中优先执行)

 遇见版本或字符集或时区问题处理方式:

url=jdbc:mysql://localhost:3306/library?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC&useSSL=falser

如果只有其中一个问题取其一即可;如果问题都有全加上就能完美处理问题

2.C3P0采用配置文件连接数据库

如果在对应的xml文件中url后添加:?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC&useSSL=falser时需要添加转义字符:&

 

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
    <named-config name="helloc3p0">
        <!-- 获取连接的4个基本信息 -->
        <property name="user">root</property>
        <property name="password">123456</property>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/library?&amp;useSSL=false&amp;serverTimezone=UTC</property>
        <property name="driverClass">com.mysql.jdbc.Driver</property>

        <!-- 涉及到数据库连接池的管理的相关属性的设置 -->
        <!-- 若数据库中连接数不足时, 一次向数据库服务器申请多少个连接 -->
        <property name="acquireIncrement">5</property>
        <!-- 初始化数据库连接池时连接的数量 -->
        <property name="initialPoolSize">5</property>
        <!-- 数据库连接池中的最小的数据库连接数 -->
        <property name="minPoolSize">5</property>
        <!-- 数据库连接池中的最大的数据库连接数 -->
        <property name="maxPoolSize">10</property>
        <!-- C3P0 数据库连接池可以维护的 Statement 的个数 -->
        <property name="maxStatements">20</property>
        <!-- 每个连接同时可以使用的 Statement 对象的个数 -->
        <property name="maxStatementsPerConnection">5</property>

    </named-config>
</c3p0-config>

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值