java.sql.SQLException:createStatement() 在闭合连接时调用



在我的项目中,我希望有api端点来获取订单详细信息。在内部,它使用ORM和数据库连接池。该过程有效,但十天一次,我收到以下错误消息并且 api 处理失败。知道可能是什么问题和可能的解决方案吗?

使用的工具

阿帕奇卡拉夫 (4.1.2(

Apache cxf for api

休眠 5.2.10

其他捆绑包

167 │ Active   │  80 │ 2.1.0               │ Apache Aries Transaction Blueprint
168 │ Active   │  80 │ 1.3.3               │ Apache Aries Transaction Manager
244 │ Active   │  80 │ 1.1.0               │ OPS4J Pax JDBC Pooling Aries Transaction JDBC

连接池(OPS4J Pax Jdbc(

osgi.jdbc.driver.name=mariadb
dataSourceName=Order
databaseName=order_database
user=user1
password=user1
pool=aries
xa=true
url=jdbc:mysql://ip/mydb?characterEncoding=UTF-8&autoReconnect=true&autoReconnectForPools=true

坚持不懈.xml

<persistence-unit name="myorder" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=myorder)</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
</properties>
</persistence-unit>

错误日志堆栈跟踪

| ERROR | qtp1522571225-12593 | OrderServiceRest                 | 126 - com.mycompany.olm.Order-application - 1.0.0.0 | Get Order Api failed for orderNo=1101
javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not prepare statement
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:147) [230:org.hibernate.core:5.2.10.Final]
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:155) [230:org.hibernate.core:5.2.10.Final]
at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1423) [230:org.hibernate.core:5.2.10.Final]
at org.hibernate.query.internal.AbstractProducedQuery.getSingleResult(AbstractProducedQuery.java:1463) [230:org.hibernate.core:5.2.10.Final]
at org.hibernate.query.criteria.internal.compile.CriteriaQueryTypeQueryAdapter.getSingleResult(CriteriaQueryTypeQueryAdapter.java:107) [230:org.hibernate.core:5.2.10.Final]
at com.mycompany.olm.order.repository.GenericRepository.findUniqueExistingEntityByPropertyValueEquality(GenericRepository.java:109) [130:com.mycompany.olm.Order-infrastructure:1.0.0.0]
at sun.reflect.GeneratedMethodAccessor378.invoke(Unknown Source) ~[?:?]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:?]
at org.apache.aries.proxy.impl.ProxyHandler$1.invoke(ProxyHandler.java:54) [36:org.apache.aries.proxy:1.1.1]
at org.apache.aries.proxy.impl.ProxyHandler.invoke(ProxyHandler.java:119) [36:org.apache.aries.proxy:1.1.1]
at com.mycompany.olm.order.repository.$OrderRepo1496888859.findUniqueExistingEntityByPropertyValueEquality(Unknown Source) [130:com.mycompany.olm.Order-infrastructure:1.0.0.0]
at com.mycompany.olm.persistence.OrderPersistenceServiceImpl.findByOrderNumber(OrderPersistenceServiceImpl.java:23) [130:com.mycompany.olm.Order-infrastructure:1.0.0.0]
at Proxy4e2856b2_5257_441c_8dcd_b768d177afda.findByOrderNumber(Unknown Source) [?:?]
at com.mycompany.olm.order.service.impl.OrderServiceImpl.findByOrderNumber(OrderServiceImpl.java:27) [131:com.mycompany.olm.Order-service:1.0.0.0]
at Proxy3f8ccbf8_c3f9_4d67_9aac_c3e79875e323.findByOrderNumber(Unknown Source) [?:?]
at Proxyf1210a3a_710e_42c7_af23_7e762904107c.findByOrderNumber(Unknown Source) [?:?]
at com.mycompany.olm.order.application.boundary.OrderServiceRest.getOrder(OrderServiceRest.java:61) [126:com.mycompany.olm.Order-application:1.0.0.0]
at sun.reflect.GeneratedMethodAccessor377.invoke(Unknown Source) ~[?:?]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:?]
at org.apache.cxf.service.invoker.AbstractInvoker.performInvocation(AbstractInvoker.java:180) [51:org.apache.cxf.cxf-core:3.1.11]
at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:96) [51:org.apache.cxf.cxf-core:3.1.11]
at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:189) [52:org.apache.cxf.cxf-rt-frontend-jaxrs:3.1.11]
at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:99) [52:org.apache.cxf.cxf-rt-frontend-jaxrs:3.1.11]
at org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:59) [51:org.apache.cxf.cxf-core:3.1.11]
at org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:96) [51:org.apache.cxf.cxf-core:3.1.11]
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308) [51:org.apache.cxf.cxf-core:3.1.11]
at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121) [51:org.apache.cxf.cxf-core:3.1.11]
at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:262) [59:org.apache.cxf.cxf-rt-transports-http:3.1.11]
at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:234) [59:org.apache.cxf.cxf-rt-transports-http:3.1.11]
at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:208) [59:org.apache.cxf.cxf-rt-transports-http:3.1.11]
at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:160) [59:org.apache.cxf.cxf-rt-transports-http:3.1.11]
at org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:180) [59:org.apache.cxf.cxf-rt-transports-http:3.1.11]
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:299) [59:org.apache.cxf.cxf-rt-transports-http:3.1.11]
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doGet(AbstractHTTPServlet.java:223) [59:org.apache.cxf.cxf-rt-transports-http:3.1.11]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:687) [30:javax.servlet-api:3.1.0]
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:274) [59:org.apache.cxf.cxf-rt-transports-http:3.1.11]
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:845) [92:org.eclipse.jetty.servlet:9.3.14.v20161028]
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:584) [92:org.eclipse.jetty.servlet:9.3.14.v20161028]
at org.ops4j.pax.web.service.jetty.internal.HttpServiceServletHandler.doHandle(HttpServiceServletHandler.java:71) [115:org.ops4j.pax.web.pax-web-jetty:6.0.6]
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143) [91:org.eclipse.jetty.server:9.3.14.v20161028]
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:548) [89:org.eclipse.jetty.security:9.3.14.v20161028]
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:226) [91:org.eclipse.jetty.server:9.3.14.v20161028]
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1180) [91:org.eclipse.jetty.server:9.3.14.v20161028]
at org.ops4j.pax.web.service.jetty.internal.HttpServiceContext.doHandle(HttpServiceContext.java:284) [115:org.ops4j.pax.web.pax-web-jetty:6.0.6]
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:512) [92:org.eclipse.jetty.servlet:9.3.14.v20161028]
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185) [91:org.eclipse.jetty.server:9.3.14.v20161028]
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1112) [91:org.eclipse.jetty.server:9.3.14.v20161028]
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) [91:org.eclipse.jetty.server:9.3.14.v20161028]
at org.ops4j.pax.web.service.jetty.internal.JettyServerHandlerCollection.handle(JettyServerHandlerCollection.java:80) [115:org.ops4j.pax.web.pax-web-jetty:6.0.6]
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:134) [91:org.eclipse.jetty.server:9.3.14.v20161028]
at org.eclipse.jetty.server.Server.handle(Server.java:534) [91:org.eclipse.jetty.server:9.3.14.v20161028]
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:320) [91:org.eclipse.jetty.server:9.3.14.v20161028]
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:251) [91:org.eclipse.jetty.server:9.3.14.v20161028]
at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:273) [84:org.eclipse.jetty.io:9.3.14.v20161028]
at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:95) [84:org.eclipse.jetty.io:9.3.14.v20161028]
at org.eclipse.jetty.io.SelectChannelEndPoint$2.run(SelectChannelEndPoint.java:93) [84:org.eclipse.jetty.io:9.3.14.v20161028]
at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.executeProduceConsume(ExecuteProduceConsume.java:303) [94:org.eclipse.jetty.util:9.3.14.v20161028]
at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceConsume(ExecuteProduceConsume.java:148) [94:org.eclipse.jetty.util:9.3.14.v20161028]
at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.run(ExecuteProduceConsume.java:136) [94:org.eclipse.jetty.util:9.3.14.v20161028]
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:671) [94:org.eclipse.jetty.util:9.3.14.v20161028]
at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:589) [94:org.eclipse.jetty.util:9.3.14.v20161028]
at java.lang.Thread.run(Thread.java:745) [?:?]
Caused by: org.hibernate.exception.GenericJDBCException: could not prepare statement
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47) ~[230:org.hibernate.core:5.2.10.Final]
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111) ~[230:org.hibernate.core:5.2.10.Final]
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:182) ~[230:org.hibernate.core:5.2.10.Final]
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareQueryStatement(StatementPreparerImpl.java:148) ~[230:org.hibernate.core:5.2.10.Final]
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1940) ~[230:org.hibernate.core:5.2.10.Final]
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1909) ~[230:org.hibernate.core:5.2.10.Final]
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1887) ~[230:org.hibernate.core:5.2.10.Final]
at org.hibernate.loader.Loader.doQuery(Loader.java:932) ~[230:org.hibernate.core:5.2.10.Final]
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:349) ~[230:org.hibernate.core:5.2.10.Final]
at org.hibernate.loader.Loader.doList(Loader.java:2615) ~[230:org.hibernate.core:5.2.10.Final]
at org.hibernate.loader.Loader.doList(Loader.java:2598) ~[230:org.hibernate.core:5.2.10.Final]
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2430) ~[230:org.hibernate.core:5.2.10.Final]
at org.hibernate.loader.Loader.list(Loader.java:2425) ~[230:org.hibernate.core:5.2.10.Final]
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:502) ~[230:org.hibernate.core:5.2.10.Final]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:379) ~[230:org.hibernate.core:5.2.10.Final]
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:216) ~[230:org.hibernate.core:5.2.10.Final]
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1488) ~[230:org.hibernate.core:5.2.10.Final]
at org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1445) ~[230:org.hibernate.core:5.2.10.Final]
at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1414) ~[230:org.hibernate.core:5.2.10.Final]
... 61 more
Caused by: java.sql.SQLException: createStatement() is called on closed connection
at org.mariadb.jdbc.MariaDbConnection.checkConnection(MariaDbConnection.java:240) ~[239:org.mariadb.jdbc:1.5.9.0]
at org.mariadb.jdbc.MariaDbConnection.internalPrepareStatement(MariaDbConnection.java:430) ~[239:org.mariadb.jdbc:1.5.9.0]
at org.mariadb.jdbc.MariaDbConnection.prepareStatement(MariaDbConnection.java:271) ~[239:org.mariadb.jdbc:1.5.9.0]
at org.tranql.connector.jdbc.ConnectionHandle.prepareStatement(ConnectionHandle.java:241) ~[?:?]
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$5.doPrepare(StatementPreparerImpl.java:146) ~[230:org.hibernate.core:5.2.10.Final]
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:172) ~[230:org.hibernate.core:5.2.10.Final]
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareQueryStatement(StatementPreparerImpl.java:148) ~[230:org.hibernate.core:5.2.10.Final]
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1940) ~[230:org.hibernate.core:5.2.10.Final]
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1909) ~[230:org.hibernate.core:5.2.10.Final]
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1887) ~[230:org.hibernate.core:5.2.10.Final]
at org.hibernate.loader.Loader.doQuery(Loader.java:932) ~[230:org.hibernate.core:5.2.10.Final]
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:349) ~[230:org.hibernate.core:5.2.10.Final]
at org.hibernate.loader.Loader.doList(Loader.java:2615) ~[230:org.hibernate.core:5.2.10.Final]
at org.hibernate.loader.Loader.doList(Loader.java:2598) ~[230:org.hibernate.core:5.2.10.Final]
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2430) ~[230:org.hibernate.core:5.2.10.Final]
at org.hibernate.loader.Loader.list(Loader.java:2425) ~[230:org.hibernate.core:5.2.10.Final]
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:502) ~[230:org.hibernate.core:5.2.10.Final]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:379) ~[230:org.hibernate.core:5.2.10.Final]
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:216) ~[230:org.hibernate.core:5.2.10.Final]
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1488) ~[230:org.hibernate.core:5.2.10.Final]
at org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1445) ~[230:org.hibernate.core:5.2.10.Final]
at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1414) ~[230:org.hibernate.core:5.2.10.Final]
... 61 more

该过程有效,但十天一次,我收到以下错误消息并且 api 处理失败。知道可能是什么问题和可能的解决方案吗?

许多数据库连接具有最长生存期。一旦它们打开了这么长时间,它们就会自动关闭。MariaDB(您引用的连接(还会关闭已空闲一段时间的连接。如果池未配置为逐出太旧的连接,或者逐出空闲连接的频率高于后端关闭它们的频率,则最终可能会遇到池中存在死连接的情况。如果池在返回连接之前测试连接,通常会选取这些查询,但您可能也没有注册测试查询。

总之,您可以通过在池上设置最大连接生存期和/或空闲超时来解决此问题。

这些细节实际上包含在最新的OSGi R7标准中,称为事务控制。这为资源(包括 JPA(提供了一个健壮的、受管理的生命周期,并为模块化事务管理提供了更可靠的集成模型。您可能希望查看此内容,或者只是查看池提供程序的文档。

相关内容

  • 没有找到相关文章

最新更新