我正在尝试将项目从Java 7升级到Java 8,并且似乎无法获得c3p0来处理迁移。似乎c3p0正在等待连接到MySQL,所以当我的Web应用程序遇到BasicResourcePool类中的第1459行时,它将永远挂起:'this.wait(timeout);'。我的项目在Java 7中运行良好。我尝试将MySQL-connector jar更新为5.1.33并将c3p0更新为0.9.5-pre9。
我只能假设Java 8改变了JDBC连接处理,因此我需要改变一些东西。这就是我设置MySQL连接的方式。我已经验证了属性文件中的所有值都被正确抓取。
properties = Locator.getInternalProperties(Locator.MYSQL_PROPERTIES);
cpds = new ComboPooledDataSource();
cpds.setDriverClass("com.mysql.jdbc.Driver");
cpds.setJdbcUrl("jdbc:mysql://" + Locator.getValue(properties, Locator.HOST) + "/"
+ Locator.getValue(properties, Locator.SCHEMA)
+ "?autoReconnect=true&failOverReadOnly=false&maxReconnects=10&characterEncoding=UTF-8");
cpds.setUser(Locator.getValue(properties, Locator.USER));
String password = EncryptionUtils.decrypt(Locator.getValue(properties, Locator.PASSWORD));
cpds.setPassword(password);
cpds.setMaxPoolSize(50);
cpds.setMaxStatements(200);
cpds.setIdleConnectionTestPeriod(60);
cpds.setPreferredTestQuery("SELECT 1");
我不确定此时我还能尝试什么。
这是来自Java 8的线程转储:
"http-bio-8080-exec-9@8497" daemon prio=5 tid=0x52 nid=NA waiting
java.lang.Thread.State: WAITING
at java.lang.Object.wait(Object.java:-1)
at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1459)
at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:639)
at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:549)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutAndMarkConnectionInUse(C3P0PooledConnectionPool.java:756)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:683)
at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:140)
at com.attensity.saascore.shared.dao.BaseDao.getConnection(BaseDao.java:24)
at com.attensity.saascore.shared.dao.UserDao.getUserByUsernameAndPassword(UserDao.java:46)
at com.attensity.saascore.shared.manager.UserManager.getUserByUsernameAndPassword(UserManager.java:92)
at com.attensity.saascore.shared.manager.AuthenticationManager.authenticateUser(AuthenticationManager.java:60)
at com.attensity.saascore.rest.user.service.AuthenticationService.authenticate(AuthenticationService.java:112)
at sun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethodAccessorImpl.java:-1)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:708)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
- locked <0x2478> (a org.apache.tomcat.util.net.SocketWrapper)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
答案 0 :(得分:4)
看来我的问题与Java 8和localhost for MySQL有关。我创建了一个新的超级简单项目来测试c3p0。同样,Java 7没有问题,但Java 8没有。然后我创建了一个超级基本的JDBC应用程序,Java 7工作,Java 8没有,但我实际上可以看到一个错误,说我的连接被拒绝了。 c3p0没有正确处理连接拒绝,因此连接线程只是永远挂起。事实上,我确实通过让我的应用程序在一夜之间运行来测试,看看我是否最终在c3p0中获得连接超时但我没有。
最后,我使用127.0.0.1而不是localhost在MySQL中创建了一个用户连接,并将我的连接URL更改为127.0.0.1,神奇地说我的JDBC测试工作正常。然后我用127.0.0.1连接测试了c3p0并且它工作正常。我没有深入研究为什么localhost在Java 7中工作得很好而不是Java 8.希望在我的MySQL生产环境中将localhost更改为127.0.0.1不会有问题。
如果您希望我帮助排除任何问题或进行更多测试,我很乐意这样做。现在我要继续前进。希望比我聪明的人有更好的解决方案。