首页 > 代码库 > C3P0数据库连接池的相关bug解决

C3P0数据库连接池的相关bug解决

数据库连接池的几个常见bug:

1.警告: com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@76c7022e -- APPARENT DEADLOCK!!! Creating emergency threads for unassigned pending tasks!

十月 01, 2016 6:28:24 下午 com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector run

 

80%以上原因是数据连接配置或数据库等类似问题。

 

①jdbc.properties中的配置有问题

 

   driverClassName

 

   url

 

②没有对应数据库。

检查发现:URL的端口写错了!!!

url = jdbc:mysql://127.0.0.1:3306/tt;(tt是数据库名称)

注意mysql的端口号必须是3306,不是8080!8080是服务器的端口号。

2.提示没找到jar包文件:

exception java.io.FileNotFoundException: E:\apache-tomcat-6.0.30\apache-tomcat-6.0.30\apache-tomcat-6.0.30\webapps\WEB-INF\lib\c3p0-0.9.1.2.jar(系统找不到指定的文件。)
java.io.FileInputStream.open(Native Method)
java.io.FileInputStream.<init>(FileInputStream.java:106)
com.chdmy.sysmgr.fill.action.FillAqAction.downFile(FillAqAction.java:957)
sun.reflect.GeneratedMethodAccessor374.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:274)
org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:194)
org.springframework.web.struts.DelegatingActionProxy.execute(DelegatingActionProxy.java:110)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.extremecomponents.table.filter.AbstractExportFilter.doFilter(AbstractExportFilter.java:49)
com.chdmy.filter.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:57)
com.chdmy.filter.SessionCheck.doFilter(SessionCheck.java:96)

症状原因:明明是在WEB-INF\lib目录下加入了c3p0-0.9.1.2.jar包的,但是需要再加入mchange-common.jar包

解决办法:

3.报错连接无法被访问

error:

java.sql.SQLException: Connections could not be acquired from the underlying database! [See nested exception: java.sql.SQLException: Connections could not be acquired from the underlying database!]]
 at org.quartz.impl.jdbcjobstore.JobStoreSupport.schedulerStarted(JobStoreSupport.java:693)
 at org.quartz.core.QuartzScheduler.start(QuartzScheduler.java:580)
 at org.quartz.impl.StdScheduler.start(StdScheduler.java:142)
 at org.quartz.ee.servlet.QuartzInitializerListener.contextInitialized(QuartzInitializerListener.java:198)
 at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3830)
 at org.apache.catalina.core.StandardContext.start(StandardContext.java:4337)
 at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
 at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
 at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
 at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
 at org.apache.catalina.core.StandardService.start(StandardService.java:516)
 at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
 at org.apache.catalina.startup.Catalina.start(Catalina.java:566)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at java.lang.reflect.Method.invoke(Method.java:597)
 at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
 at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
Caused by: org.quartz.JobPersistenceException: Failed to obtain DB connection from data source ‘myDS‘: java.sql.SQLException: Connections could not be acquired from the underlying database! [See nested exception: java.sql.SQLException: Connections could not be acquired from the underlying database!]
 at org.quartz.impl.jdbcjobstore.JobStoreSupport.getConnection(JobStoreSupport.java:777)
 at org.quartz.impl.jdbcjobstore.JobStoreTX.getNonManagedTXConnection(JobStoreTX.java:71)
 at org.quartz.impl.jdbcjobstore.JobStoreSupport.executeInNonManagedTXLock(JobStoreSupport.java:3774)
 at org.quartz.impl.jdbcjobstore.JobStoreSupport.executeInNonManagedTXLock(JobStoreSupport.java:3739)
 at org.quartz.impl.jdbcjobstore.JobStoreSupport.recoverJobs(JobStoreSupport.java:833)
 at org.quartz.impl.jdbcjobstore.JobStoreSupport.schedulerStarted(JobStoreSupport.java:691)
 ... 18 more
Caused by: java.sql.SQLException: Connections could not be acquired from the underlying database!
 at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:106)
 at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:529)
 at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:128)
 at org.quartz.utils.PoolingConnectionProvider.getConnection(PoolingConnectionProvider.java:247)
 at org.quartz.utils.DBConnectionManager.getConnection(DBConnectionManager.java:108)
 at org.quartz.impl.jdbcjobstore.JobStoreSupport.getConnection(JobStoreSupport.java:774)
 ... 23 more
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:1319)
 at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:557)
 at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:477)
 at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:525)
 ... 27 more

以上问题可能有一下原因造成:

①驱动配置有误:driver=com.mysql.jdbc.Driver

②数据库连接地址有误:url=jdbc:mysql://localhost:3306/tt

数据库密码或帐号有误

④数据库未启动或无权访问          

⑤项目未引入对应的数据库驱动jar包:mysql-connector-java-5.1.6-bin.jar

⑥mysql root没有远程访问的权限,需要增加权限,增加权限的步骤如下:

进入mysql数据库:
grant all privileges on *.* to ‘root‘@‘%‘ identified by ‘root‘ with grant option;
flush privileges;

(我就犯了其中的两个错误:1.没有加入mysql-connector-java-5.1.6-bin.jar包

                                       2.数据库连接端口错了:写成jdbc:mysql://localhost:8080/tt了,端口应                                             该是3036)

4.虽然程序运行成功,数据可以查出来,但是控制器还是会出现如下警告:

警告: 

com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask@6ff9129c -- 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: The server time zone value is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:695)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:663)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:653)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:638)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:606)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:624)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:620)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:68)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:1683)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:656)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:349)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:221)

解决办法:

在c3p0-config.xml的connecturl配置中添加&serverTimezone=UTC

例如: <property name="jdbcUrl">jdbc:mysql://127.0.0.1:8080/tt&serverTimezone=UTC</property>

 

 

多谢:http://jyao.iteye.com/blog/1915561和http://www.cnblogs.com/God-/p/5845301.html的分享。

C3P0数据库连接池的相关bug解决