最近复习数据库连接池遇到mysql版本导致连接异常问题记录如下
环境:mysql版本:mysql8.0.25
jar包版本:
mysql驱动包:mysql-connector-java-8.0.11.jar
c3p0连接池jar包:mchange-commons-java-0.2.12.jar mysql-connector-java-8.0.11.jar
出现异常:
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 myStudy_jdbc.com.xiaoqiang.JDBCConnectionPool.c3p0DempMethod(JDBCConnectionPool.java:52)
at myStudy_jdbc.com.xiaoqiang.JDBCConnectionPool.main(JDBCConnectionPool.java:45)
Caused by: com.mchange.v2.resourcepool.TimeoutException: A client timed out while waiting to acquire a resource from com.mchange.v2.resourcepool.BasicResourcePool@18e8568 -- 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)
... 3 more
解决方案:
修改连接配置,8.0驱动之前驱动类的位置是 com.mysql.jdbc.Driver,8.0版本之后驱动类的位置是com.mysql.cj.jdbc.Driver,8.0版本的url连接需要加上使用的时区。
配置修改如下:
<c3p0-config>
<!-- 使用默认的配置读取连接池对象 -->
<default-config>
<!-- 连接参数 -->
<!-- mysql 8.0版本配置 -->
<property name="driverClass">com.mysql.cj.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/db1?serverTimezone=UTC</property>
<!-- mysql 8.0版本之前配置 -->
<!--
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/db1</property>
-->
<property name="user">root</property>
<property name="password">Xiaoqiang1006</property>
<!-- 连接池参数 -->
<!--初始化的申请的连接数量-->
<property name="initialPoolSize">5</property>
<!--最大的连接数量-->
<property name="maxPoolSize">10</property>
<!--连接超时时间-->
<property name="checkoutTimeout">3000</property>
</default-config>
<named-config name="otherc3p0">
<!-- 连接参数 -->
<!-- mysql 8.0版本配置 -->
<property name="driverClass">com.mysql.cj.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/db1?serverTimezone=UTC</property>
<!-- mysql 8.0版本之前配置 -->
<!--
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/db1</property>
-->
<property name="user">root</property>
<property name="password">Xiaoqiang1006</property>
<!-- 连接池参数 -->
<property name="initialPoolSize">5</property>
<property name="maxPoolSize">8</property>
<property name="checkoutTimeout">1000</property>
</named-config>
</c3p0-config>