使用C3P0连接池时报TimeoutException错的解决方法
最近在跟着黑马程序员的Java就业班教程学习数据库连接池的配置和使用时,遇到了一个棘手的问题,就是在使用C3P0连接池连接本机的lzm数据库时无法获取连接,测试代码如下:
C3P0Demo1.java
package cn.itcast.datasource.c3p0; import com.mchange.v2.c3p0.ComboPooledDataSource; import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException; public class C3P0Demo1 { public static void main(String[] args) throws SQLException { //1.创建数据库连接池对象 DataSource dataSource =new ComboPooledDataSource(); //2.获取连接对象 Connection connection=dataSource.getConnection(); //3.打印 System.out.println(connection); } }
c3p0-config.xml配置信息如下:
<?xml version="1.0" encoding="utf-8"?> <c3p0-config> <default-config> <property name="driverClass">com.mysql.cj.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/lzm</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.cj.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/lzm</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> </named-config> </c3p0-config>
IDEA报了以下错误:
Exception in thread "main" java.sql.SQLException: An attempt by a client to checkout a Connection has timed out. at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:118) at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:77) at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:690) at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:140) at cn.itcast.datasource.c3p0.C3P0Demo1.main(C3P0Demo1.java:14) Caused by: com.mchange.v2.resourcepool.TimeoutException: A client timed out while waiting to acquire a resource from com.mchange.v2.resourcepool.BasicResourcePool@19bb089b -- timeout at awaitAvailable() at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1467) at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:644) at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:554) at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutAndMarkConnectionInUse(C3P0PooledConnectionPool.java:758) at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:685) ... 2 more
在各种网站上都搜索不到解决的方法,在即将陷入绝望时,突发灵感,检查开发环境
我的开发环境是 Java8+MySQL5.7,使用的Jar包是mysql-connector-java-8.0.17.jar,c3p0-0.9.5.2.jar,mchange-commons-java-0.2.12.jar
而视频教程中使用的数据库驱动包是mysql-connector-java-5.1.48.jar,于是我将libs目录下的mysql-connector-java-8.0.17.jar删除,导入mysql-connector-java-5.1.48.jar
把c3p0-config.xml配置信息中的<property name="driverClass">com.mysql.cj.jdbc.Driver</property>代码都修改成<property name="driverClass">com.mysql.jdbc.Driver</property>
再次运行C3P0Demo1.java后,问题解决,控制台打印的消息如下:
问题解决!原来这个问题的根本原因是mysql-connector-java-8.0.17.jar与c3p0-0.9.5.2.jar,mchange-commons-java-0.2.12.jar这两个包不匹配或不兼容所导致的,像mysql-connector-java-8.0.17.jar是最新版的Jar包,c3p0-0.9.5.2.jar,mchange-commons-java-0.2.12.jar是较老的Jar包,显然会出现不能适配的情况了,所以开发不做最新版,这句话还是很有道理的啦!
总结
解决的这个问题给了我一个很大的警示,就是在做Java开发的过程中选择正确的开发环境很重要,一定要选择好版本相匹配的开发环境和Jar包,配置合适的文件信息,才能让程序正常地运行,才不会产生让人焦头烂额的各种异常信息。今后,要引以为戒!