C3P0运行报错

C3P0运行报错

报错信息如下:

八月 29, 2021 1:58:41 下午 com.mchange.v2.log.MLog 

信息: MLog clients using java 1.4+ standard logging.

八月 29, 2021 1:58:42 下午 com.mchange.v2.c3p0.C3P0Registry 

信息: Initializing c3p0-0.9.5.5 [built 11-December-2019 22:07:46 -0800; debug? true; trace: 10]

八月 29, 2021 1:58:42 下午 com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource 

信息: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, contextClassLoaderSource -> caller, dataSourceName -> 1hge774aj14398fg13403gc|5cb0d902, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> null, extensions -> {}, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, forceSynchronousCheckins -> false, forceUseNamedDriverClass -> false, identityToken -> 1hge774aj14398fg13403gc|5cb0d902, idleConnectionTestPeriod -> 0, initialPoolSize -> 3, jdbcUrl -> null, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 15, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 3, numHelperThreads -> 3, preferredTestQuery -> null, privilegeSpawnedThreads -> false, properties -> {}, propertyCycle -> 0, statementCacheNumDeferredCloseThreads -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, userOverrides -> {}, usesTraditionalReflectiveProxies -> false ]

八月 29, 2021 1:59:12 下午 com.mchange.v2.resourcepool.BasicResourcePool 

警告: com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask@3b5db1d9 -- Acquisition Attempt Failed!!! Clearing pending acquires. While trying to acquire a needed new resource, we failed to succeed more than the maximum number of allowed acquisition attempts (30). Last acquisition attempt exception: 

java.sql.SQLException: No suitable driver

at java.sql.DriverManager.getDriver(DriverManager.java:315)

at com.mchange.v2.c3p0.DriverManagerDataSource.driver(DriverManagerDataSource.java:285)

at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:161)

at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:161)

at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:147)

at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:202)

at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1176)

at com.mchange.v2.resourcepool.BasicResourcePool.doAcquireAndDecrementPendingAcquiresWithinLockOnSuccess(BasicResourcePool.java:1163)

at com.mchange.v2.resourcepool.BasicResourcePool.access$700(BasicResourcePool.java:44)

at com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask.run(BasicResourcePool.java:1908)

at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:696)


八月 29, 2021 1:59:12 下午 com.mchange.v2.resourcepool.BasicResourcePool 

警告: com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask@3915db84 -- Acquisition Attempt Failed!!! Clearing pending acquires. While trying to acquire a needed new resource, we failed to succeed more than the maximum number of allowed acquisition attempts (30). Last acquisition attempt exception: 

java.sql.SQLException: No suitable driver

at java.sql.DriverManager.getDriver(DriverManager.java:315)

at com.mchange.v2.c3p0.DriverManagerDataSource.driver(DriverManagerDataSource.java:285)

at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:161)

at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:161)

at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:147)

at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:202)

at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1176)

at com.mchange.v2.resourcepool.BasicResourcePool.doAcquireAndDecrementPendingAcquiresWithinLockOnSuccess(BasicResourcePool.java:1163)

at com.mchange.v2.resourcepool.BasicResourcePool.access$700(BasicResourcePool.java:44)

at com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask.run(BasicResourcePool.java:1908)

at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:696)


八月 29, 2021 1:59:12 下午 com.mchange.v2.resourcepool.BasicResourcePool 

警告: Having failed to acquire a resource, com.mchange.v2.resourcepool.BasicResourcePool@71c7db30 is interrupting all Threads waiting on a resource to check out. Will try again in response to new client requests.

八月 29, 2021 1:59:12 下午 com.mchange.v2.resourcepool.BasicResourcePool 

警告: Having failed to acquire a resource, com.mchange.v2.resourcepool.BasicResourcePool@71c7db30 is interrupting all Threads waiting on a resource to check out. Will try again in response to new client requests.

java.sql.SQLException: Connections could not be acquired from the underlying database!

at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:118)

at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:692)

at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:140)

at com.jdbc.com.C3P0Sample.main(C3P0Sample.java:17)

Caused by: com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.

at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1507)

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

Caused by: java.sql.SQLException: No suitable driver

at java.sql.DriverManager.getDriver(DriverManager.java:315)

at com.mchange.v2.c3p0.DriverManagerDataSource.driver(DriverManagerDataSource.java:285)

at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:161)

at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:161)

at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:147)

at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:202)

at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1176)

at com.mchange.v2.resourcepool.BasicResourcePool.doAcquireAndDecrementPendingAcquiresWithinLockOnSuccess(BasicResourcePool.java:1163)

at com.mchange.v2.resourcepool.BasicResourcePool.access$700(BasicResourcePool.java:44)

at com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask.run(BasicResourcePool.java:1908)

at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:696)

Exception in thread "main" java.lang.NullPointerException

at com.jdbc.com.DbUtils.closeConnect(DbUtils.java:11)

at com.jdbc.com.C3P0Sample.main(C3P0Sample.java:31)


Process finished with exit code 1


相关代码:

package com.jdbc.com;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import javax.sql.DataSource;
import java.sql.*;

public class C3P0Sample {
public static void main(String[] args) {
DataSource dataSource = new ComboPooledDataSource();
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;

{
try {
conn = dataSource.getConnection();
pstmt = conn.prepareStatement("select * from goodsmanger limit 0,1");
rs = pstmt.executeQuery();
while (rs.next()) {
Integer id = rs.getInt(1);
String ggname = rs.getString("gname");
Float price = rs.getFloat("price");
String desc = rs.getString("desc");
Date ctime = rs.getDate("creat_time");
System.out.println(id + "-" + ggname + "-" + price + "-" + desc + "-" + ctime);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DbUtils.closeConnect(conn,pstmt,rs);
}

}
}
}

相关代码:

<?xml version="1.0" encoding="utf-8" ?>
<c3p0-config>
<defalut-config>
<property name="driverClass">com.mysql.cj.jdbc.Driver</property>
<property name="url">jdbc:mysql://localhost:3306/goods?useSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=Asia/Shanghai&amp;allowPublicKeyRetrieval=true</property>
<property name="user">root</property>
<property name="password">123</property>
<property name="initialPoolSize">20</property>
<property name="maxPoolSize">20</property>
</defalut-config>
</c3p0-config>

相关截图:

http://img1.sycdn.imooc.com//climg/612b22c10998a16107701006.jpg

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕小尤 2021-08-29 14:30:25

同学你好,1、<default-config>标签书写错误,则建议同学进行修改。

2、连接数据库的name值应该是jdbcUrl而不是url,则建议同学进行修改。修改后代码如下所示:

http://img1.sycdn.imooc.com//climg/612b296a0961f07f07900338.jpg

祝学习愉快!

  • 提问者 慕村4629685 #1

    修改过后依然报错:1.空指针错误 2.底层数据库连接失败

    报错信息如下:

    Exception in thread "main" java.lang.NullPointerException

    at com.jdbc.com.DbUtils.closeConnect(DbUtils.java:11)

    at com.jdbc.com.C3P0Sample.main(C3P0Sample.java:31)


    ​java.sql.SQLException: Connections could not be acquired from the underlying database!

          at com.jdbc.com.C3P0Sample.main(C3P0Sample.java:17)

    2021-08-29 15:34:19
  • 提问者 慕村4629685 #2

    请问是不是jar版本与工具版本不匹配,不兼容的问题,

    2021-08-29 16:08:01
  • 好帮手慕小尤 回复 提问者 慕村4629685 #3

    同学你好,1、测试同学代码并未复现该问题,则建议同学查看DbUtils.closeConnect()方法,查看是否有null调用方法的情况。

    2、可能是jar与mysql版本不符导致的,如果同学MySQL是5的版本,则建议同学使用5jar包的依赖,并去除com.mysql.cj.jdbc.Driver中的cj。如下所示:

      <property name="driverClass">com.mysql.jdbc.Driver</property>

    ​祝学习愉快!

    2021-08-29 16:56:50
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师