文章目录
问题描述求助场景还原完整配置文件内容:总结:问题描述
今天在使用C3P0连接数据库执行对表的一些简单操作的时候出现了一个问题:C3P0始终连接不上并且报如下错误:
八月 08, 6:44:02 下午 com.mchange.v2.log.MLog 信息: MLog clients using java 1.4+ standard logging.八月 08, 6:44:03 下午 com.mchange.v2.c3p0.C3P0Registry 信息: Initializing c3p0-0.9.5.5 [built 11-December- 22:07:46 -0800; debug? true; trace: 10]八月 08, 6:44:03 下午 com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource 信息: Initializing c3p0 pool... com.mchange.boPooledDataSource [ 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 -> 31swv7ab1l2dtgc4t0g00|7591083d, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> null, extensions -> {}, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, forceSynchronousCheckins -> false, forceUseNamedDriverClass -> false, identityToken -> 31swv7ab1l2dtgc4t0g00|7591083d, 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 ]八月 08, 6:44:33 下午 com.mchange.v2.resourcepool.BasicResourcePool 警告: com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask@6865160f -- 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.lang.NullPointerExceptionat com.mysql.fabric.jdbc.FabricMySQLDriver.parseFabricURL(FabricMySQLDriver.java:97)at com.mysql.fabric.jdbc.FabricMySQLDriver.acceptsURL(FabricMySQLDriver.java:93)at java.sql.DriverManager.getDriver(DriverManager.java:299)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)八月 08, 6:44:33 下午 com.mchange.v2.resourcepool.BasicResourcePool 警告: com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask@5c78bad6 -- 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.lang.NullPointerExceptionat com.mysql.fabric.jdbc.FabricMySQLDriver.parseFabricURL(FabricMySQLDriver.java:97)at com.mysql.fabric.jdbc.FabricMySQLDriver.acceptsURL(FabricMySQLDriver.java:93)at java.sql.DriverManager.getDriver(DriverManager.java:299)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)八月 08, 6:44:33 下午 com.mchange.v2.resourcepool.BasicResourcePool 警告: com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask@172e69c1 -- 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.lang.NullPointerExceptionat com.mysql.fabric.jdbc.FabricMySQLDriver.parseFabricURL(FabricMySQLDriver.java:97)at com.mysql.fabric.jdbc.FabricMySQLDriver.acceptsURL(FabricMySQLDriver.java:93)at java.sql.DriverManager.getDriver(DriverManager.java:299)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)八月 08, 6:44:33 下午 com.mchange.v2.resourcepool.BasicResourcePool 警告: Having failed to acquire a resource, com.mchange.v2.resourcepool.BasicResourcePool@22a71081 is interrupting all Threads waiting on a resource to check out. Will try again in response to new client requests.八月 08, 6:44:33 下午 com.mchange.v2.resourcepool.BasicResourcePool 警告: Having failed to acquire a resource, com.mchange.v2.resourcepool.BasicResourcePool@22a71081 is interrupting all Threads waiting on a resource to check out. Will try again in response to new client requests.八月 08, 6:44:33 下午 com.mchange.v2.resourcepool.BasicResourcePool 警告: Having failed to acquire a resource, com.mchange.v2.resourcepool.BasicResourcePool@22a71081 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.roohom.demo.ClassDemo.JDBC.ConnectionPool.tools.C3p0Utils.getConnection(C3p0Utils.java:22)at com.roohom.demo.homework.day18.UserLogin.main(UserLogin.java:39)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)... 3 moreCaused by: java.lang.NullPointerExceptionat com.mysql.fabric.jdbc.FabricMySQLDriver.parseFabricURL(FabricMySQLDriver.java:97)at com.mysql.fabric.jdbc.FabricMySQLDriver.acceptsURL(FabricMySQLDriver.java:93)at java.sql.DriverManager.getDriver(DriverManager.java:299)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.NullPointerExceptionat com.roohom.demo.homework.day18.UserLogin.main(UserLogin.java:44)
求助
于是去找度娘解决,找到的很多办法,
有的说是C3P0配置文件的位置不对,于是更换了配置文件,仍然报错。将C3P0放置在src目录下 经过我的尝试,不好使! 有的说是MYSQL5.5使用的驱动文件是 mysql-connector-java-5.1.47.jar
MYSQL5.7使用的驱动文件是 mysql-connector-java-8.0.11.jar 经过我的尝试,更换版本,不好使! 还有的说是
c3p0-config.xml基本配置<!-- 连接mysql数据的基本的信息配置 --><property name="driverClass">com.mysql.cj.jdbc.Driver</property><property name="jdbcUrl">jdbc:mysql://localhost:3306/mvcproject?&useSSL=false&serverTimezone=UTC</property>
经过我的尝试,不好使!
场景还原
无意间,当我把配置文件中,driverClass的参数更改为以上第三种方式的要求时,报错信息发生改变了!
如下:
八月 08, 6:53:51 下午 com.mchange.v2.log.MLog 信息: MLog clients using java 1.4+ standard logging.八月 08, 6:53:52 下午 com.mchange.v2.c3p0.C3P0Registry 信息: Initializing c3p0-0.9.5.5 [built 11-December- 22:07:46 -0800; debug? true; trace: 10]八月 08, 6:53:52 下午 com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource 信息: Initializing c3p0 pool... com.mchange.boPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 3000, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, contextClassLoaderSource -> caller, dataSourceName -> 31swv7ab1l2qfz11arzrlt|7591083d, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.cj.jdbc.Driver, extensions -> {}, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, forceSynchronousCheckins -> false, forceUseNamedDriverClass -> false, identityToken -> 31swv7ab1l2qfz11arzrlt|7591083d, idleConnectionTestPeriod -> 0, initialPoolSize -> 5, jdbcUrl -> jdbc:mysql://127.0.0.1:3306/roohom?&useSSL=false&serverTimezone=UTC, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 10, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 3, numHelperThreads -> 3, preferredTestQuery -> null, privilegeSpawnedThreads -> false, properties -> {user=******, password=******}, propertyCycle -> 0, statementCacheNumDeferredCloseThreads -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, userOverrides -> {}, usesTraditionalReflectiveProxies -> false ]八月 08, 6:53:52 下午 com.mchange.v2.c3p0.DriverManagerDataSource 警告: Could not load driverClass com.mysql.cj.jdbc.Driverjava.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driverat .URLClassLoader.findClass(URLClassLoader.java:382)at java.lang.ClassLoader.loadClass(ClassLoader.java:424)at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)at java.lang.ClassLoader.loadClass(ClassLoader.java:357)at java.lang.Class.forName0(Native Method)at java.lang.Class.forName(Class.java:264)at com.mchange.v2.c3p0.DriverManagerDataSource.ensureDriverLoaded(DriverManagerDataSource.java:143)at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:173)at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:220)at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:206)at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:203)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)
其中有这么一行信息:
警告: Could not load driverClass com.mysql.cj.jdbc.Driverjava.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver
这说明了什么,这说明更换C3P0配置文件(即c3p0-config.xml
)的目录,已经成功了,已经开始对配置文件进行读了,只是读的参数读之后发现参数不对!这么一来,配置文件的目录问题就排除了,继续排查!
我把driverClass更换为了之前的:com.mysql.jdbc.Driver再次尝试!
这次报错信息又改变了!如下:
信息: Initializing c3p0 pool... com.mchange.boPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 3000, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, contextClassLoaderSource -> caller, dataSourceName -> 31swv7ab1l2w4aoowmqcv|7591083d, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, extensions -> {}, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, forceSynchronousCheckins -> false, forceUseNamedDriverClass -> false, identityToken -> 31swv7ab1l2w4aoowmqcv|7591083d, idleConnectionTestPeriod -> 0, initialPoolSize -> 5, jdbcUrl -> jdbc:mysql://127.0.0.1:3306/roohom?&useSSL=false&serverTimezone=UTC, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 10, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 3, numHelperThreads -> 3, preferredTestQuery -> null, privilegeSpawnedThreads -> false, properties -> {user=******, password=******}, propertyCycle -> 0, statementCacheNumDeferredCloseThreads -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, userOverrides -> {}, usesTraditionalReflectiveProxies -> false ]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 com.roohom.demo.ClassDemo.JDBC.ConnectionPool.tools.C3p0Utils.getConnection(C3p0Utils.java:22)at com.roohom.demo.homework.day18.UserLogin.main(UserLogin.java:39)Caused by: com.mchange.v2.resourcepool.TimeoutException: A client timed out while waiting to acquire a resource from com.mchange.v2.resourcepool.BasicResourcePool@22a71081 -- timeout at awaitAvailable()at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1505)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 moreException in thread "main" java.lang.NullPointerExceptionat com.roohom.demo.homework.day18.UserLogin.main(UserLogin.java:44)
着重看里面的这行信息:
java.sql.SQLException: An attempt by a client to checkout a Connection has timed out.
意思大概是尝试检查对MySQL的链接超时了,于是我猜测是密码错误了,再次查看配置文件内容,着重看了登录参数中的密码:
<property name="password">""</property>
我的本地MySQL默认没有密码,而我设置的是:""
,而问题恰恰就出在这里,当我尝试把密码改为空,也就是取消""
之后,程序终于跑起来了!跑起来了!跑起来了啊!成功运行了!
完整配置文件内容:
<?xml version="1.0" encoding="UTF-8" ?><c3p0-config><!-- 使用默认的配置读取连接池对象 --><default-config><!-- 连接参数 --><property name="driverClass">com.mysql.jdbc.Driver</property><property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/roohom?&useSSL=false&serverTimezone=UTC</property><property name="user">root</property><property name="password"></property><!-- 连接池参数 --><property name="initialPoolSize">5</property><property name="maxPoolSize">10</property><property name="checkoutTimeout">3000</property><!-- <property name="maxStatements">2</property>--></default-config><!-- <named-config name="otherc3p0">--><!-- <!– 连接参数 –>--><!-- <property name="driverClass">com.mysql.jdbc.Driver</property>--><!-- <property name="jdbcUrl">jdbc:mysql://localhost:3306/roohom?&useSSL=false&serverTimezone=UTC</property>--><!-- <property name="user">root</property>--><!-- <property name="password"></property>--><!-- <!– 连接池参数 –>--><!-- <property name="initialPoolSize">5</property>--><!-- <property name="maxPoolSize">8</property>--><!-- <property name="checkoutTimeout">1000</property>--><!-- </named-config>--></c3p0-config>
经过我的尝试,此c3p0-config.xml文件中named-config name="otherc3p0"部分应该完全删除,使用它是不可以运行的!
总结:
c3p0-config.xml配置文件可以放在src目录下,也可以在工程目录下新建一个resources目录,并将其右键Make Directory as -> Resources root,然后将配置文件放置在其下面。如果数据库没有密码,配置文件中就什么都不用填写,连""
都不需要我使用的个版本包分别是(这三个包放置在与src同级的lib目录下): mysql-connector-java-5.1.49-bin.jarmchange-commons-java-0.2.19.jarc3p0-0.9.5.5.jar希望能帮助到你!