问题描述
我有一个J2EE应用程序,它使用JSP页面作为前端,Struts2作为控制器。我也使用Hibernate将我的对象映射到MySql DB中。当我在服务器上部署应用程序时它工作正常,但在无法预测的时间段(如1或2天)后,无论我使用hibernate查询,我都会收到此错误:
org.hibernate.exception.JDBCConnectionException:无法执行查询
让它更有趣的事实是,当这种情况发生时,并不是100%的查询最终会导致这种异常!有时它们中的一些被正确执行。
一个错误发生的代码片段:
public synchronized static Player getPlayerByID(String playerID)throws NoSuchPlayer {
Session session = HibernateUtil.getSessionFactory()。getCurrentSession();
session.beginTransaction();
Player player =(Player)session.createQuery(
from Player as player where player.loginName ='+ playerID
+').uniqueResult(); // THE ERROR HAPPENS HERE
session.getTransaction()。commit();
if(player == null)
throw new NoSuchPlayer();
返回玩家;
}
这是相应的堆栈跟踪:
'pre> org.hibernate.exception.JDBCConnectionException:在org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:99)$无法执行查询
b $ b在org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
在org.hibernate.loader.Loader.doList(Loader.java:2297)
在org.hibernate。 loader.Loader.listIgnoreQueryCache(Loader.java:2172)
在org.hibernate.loader.Loader.list(Loader.java:2167)
在org.hibernate.loader.hql.QueryLoader.list( QueryLoader.java:448)
在org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
在org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1258)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java: 102)
at org.hibernate.impl.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:859)
at com.hposg.domain.HPOSG.getPlayerByID(HPOSG.java:117)
at com .hposg.login.UserSession.getPlayerById(UserSession.java:39)
at com.hposg.controller.struts.BuyAction.execute(BuyAction.java:74)
at sun.reflect.GeneratedMethodAccessor1098.invoke (来源不明)美元,sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)b
$ b。在java.lang.reflect.Method.invoke(Method.java:616)
。在COM .opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:441)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke (DefaultActionInvocation.java:243)
at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(Defa ultWorkflowInterceptor.java:165)美元,com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87 B $ B)
在com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)在com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:252)
在org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony .xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymph ony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
处com.opensymphony.xwork2 com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
。 DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
在com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
在com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept( MethodFilterInterceptor.java:87)美元,com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237 b $ b)
在com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:179)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)在com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
在org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
。在COM .opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235)
at com.opensymphony.xwork2.DefaultActionInvocation .invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java :237)
at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:130)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
在org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
在com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
在com.opensymphony .xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.PrepareInterceptor .doIntercept(PrepareInterceptor.java:138)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java :237)
at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:165)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
在org.apach e.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor。 AliasInterceptor.intercept(AliasInterceptor.java:179)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor。的java:176)$在com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237 b $ b)
。在org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488)
at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)
at org .apache.catalina.core.ApplicationFilterChain.internalDoF ilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve。的java:233)$在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191 b $ b)
。在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
at org.apache .coyote.http11.Http11Protocol $ Http11ConnectionHandler.process(Http11Protocol.java:588)
在org.apache.tomcat.util.net.JIoEndpoint $ Worker.run(JIoEndp oint.java:489)
在java.lang.Thread.run(Thread.java:636)
引起:com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:连接关闭后不允许任何操作。由于潜在的异常/错误,连接被隐式关闭:
** BEGIN NESTED EXCEPTION **
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException
MESSAGE:通信链路故障
发送到服务器的最后一个数据包是0 ms前。
STACKTRACE:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链路故障
发送到服务器的最后一个数据包为0 ms前。
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl。 java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:532)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:353)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2583)
at com.mysql.jdbc.MysqlIO .checkErrorPacket(MysqlIO.java:2871)
在com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1601)在com.mysql.jdbc.MysqlIO.sqlQueryDirect
(MysqlIO.java:1710 )
at com.mysql.jdbc.Connection.execSQL(Connection.java:2436)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1402)
at com。 mysql.jdbc.PreparedS tatement.executeQuery(PreparedStatement.java:1556)
在org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208)
在org.hibernate.loader.Loader.getResultSet(Loader.java: 1849)
在org.hibernate.loader.Loader.doQuery(Loader.java:718)
在org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270)
。在组织.hibernate.loader.Loader.doList(Loader.java:2294)
在org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2172)
在org.hibernate.loader.Loader.list (Loader.java:2167)
在org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:448)
在org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java :363)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1258)
位于org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
位于com.hposg.domain.HPOSG。 getGameList(HPOSG.java:222)
at com.hposg.controller.struts.GamesLoadActionAjax.execute(GamesLoadActionAjax.java:23)
at sun.reflect.GeneratedMethodAccessor1047.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at com.opensymphony.xwork2.DefaultActionInvocation。 invokeAction(DefaultActionInvocation.java:441)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:243)
at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
在com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInv
at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:252)
at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor。的java:在com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87 68)
)
。在com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony .xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
在com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
在com.opensymphony.xwork2.DefaultActionInvocation .inv oke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor。的java:在com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237 87)
)
。在com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:179)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
at com.opensymphony .xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke (DefaultActionInvocation.java: 237)
在org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235)
在com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
在com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2 .interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:130)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at org.apache.struts2.interceptor.debugging.DebuggingInterceptor .intercept(DebuggingInterceptor.java:267)
在com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
在com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java :126)
在com.opensy mphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
处com.opensymphony.xwork2.interceptor com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)
。 MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor。 java:165)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:179)
at com.opensymphony .xwork2.DefaultActionInvocation.invoke(DEFAULTACTION Invocation.java:237)
at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)在org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
在org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488)
在org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)
在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
在org.apache .catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core .StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValv e.java:127)
在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298 109)
)
。在org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
at org.apache.coyote.http11.Http11Protocol $ Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint $ Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:636)
引起:java.io.EOFException
在com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java: 1958)
在com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2372)
... 83 more
**结束嵌套异常* *
at sun.reflect.GeneratedConstructorAccessor139.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccess orImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
在java.lang.reflect.Constructor.newInstance(Constructor.java:532)
在com.mysql.jdbc.Util.handleNewInstance(Util.java:在com.mysql.jdbc.Util.getInstance(Util.java:336 353)
)
在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:985)
。在COM .mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
在com.mysql.jdbc.Connection.checkClosed (Connection.java:1103)
at com.mysql.jdbc.Connection.prepareStatement(Connection.java:3865)
at com.mysql.jdbc.Connection.prepareStatement(Connection.java:3831)
。在org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:534)
处org.hibernate作为org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:452)
。 jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:161)
在org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1596)
在org.hibernate.loader.Loader.doQuery(Loader.java:717)
在org.hibernate.loader.Loader .doQueryAndInitializeNonLazyCollections(Loader.java:270)在org.hibernate.loader.Loader.doList(Loader.java:2294)
... 74更
'
最后这是我的hibernate配置文件:
<?xml version ='1.0'encoding ='utf-8'?>
<! - 数据库连接设置 - >
< property name =connection.driver_class> com.mysql.jdbc.Driver< / property>
< property name =hibernate.connection.url>
jdbc:mysql:// localhost / hposg?characterEncoding = UTF-8
< / property>
< property name =connection.username> root< / property>
< property name =connection.password>< / property>
< property name =connection.pool_size> 1< / property>
< property name =show_sql> true< / property>
< property name =dialect> org.hibernate.dialect.MySQLDialect< / property>
< property name =current_session_context_class>线程< / property>
< property name =cache.provider_class> org.hibernate.cache.NoCacheProvider< / property>
< property name =hbm2ddl.auto>更新< / property>
< mapping resource =com / hposg / hibernate / resources / Player.hbm.xml/>
< mapping resource =com / hposg / hibernate / resources / Game.hbm.xml/>
< mapping resource =com / hposg / hibernate / resources / Portfolio.hbm.xml/>
< mapping resource =com / hposg / hibernate / resources / Purchase.hbm.xml/>
< mapping resource =com / hposg / hibernate / resources / Sale.hbm.xml/>
< mapping resource =com / hposg / hibernate / resources / Message.hbm.xml/>
< mapping resource =com / hposg / hibernate / resources / Admin.hbm.xml/>
< mapping resource =com / hposg / hibernate / resources / News.hbm.xml/>
< mapping resource =com / hposg / hibernate / resources / Invitation.hbm.xml/>
< mapping resource =com / hposg / hibernate / resources / CustomGameCreationRequest.hbm.xml/>
< mapping resource =com / hposg / hibernate / resources / Alert.hbm.xml/>
< mapping resource =com / hposg / hibernate / resources / AlertLimits.hbm.xml/>
< mapping resource =com / hposg / hibernate / resources / StockIndex.hbm.xml/>
< mapping resource =com / hposg / hibernate / resources / HPOSG.hbm.xml/>
< mapping resource =com / hposg / hibernate / resources / Tradeable.hbm.xml/>
< mapping resource =com / hposg / hibernate / resources / GlobalGameConfig.hbm.xml/>
< mapping resource =com / hposg / hibernate / resources / Announcement.hbm.xml/>
< mapping resource =com / hposg / hibernate / resources / AdviserMsg.hbm.xml/>
< mapping resource =com / hposg / hibernate / resources / SystemConfiguration.hbm.xml/>
< / session-factory>
btw我没有使用c3p0连接池,我不知道这是否相关。只是觉得提及它。
任何想法? Hibernate使用原始连接池机制 rel =nofollow a>,这不适合于生产目的(它甚至在日志文件中说过!)。您应始终 使用连接池机制,无论是由您的容器提供的连接池机制,无论它是捆绑在您的应用程序中的机制(例如c3p0)。你看到的例外是因为MySQL关闭了Hibernate仍在使用的旧连接。你可以尝试添加一个JDBC URL选项,比如autoReconnect,但这不是一个长期的解决方案。
理想情况下,您应该配置容器以通过JNDI为应用程序提供连接。这很容易通过JBoss中的-ds.xml文件或Tomcat的context.xml来完成。
I've got a J2EE application which uses JSP pages as front end and Struts2 as controller. I also use Hibernate to map my objects into a MySql DB. when I deploy the application on the server It works fine but after an unpredictable period of time (like 1 or two days) wherever I've used hibernate queries I keep getting this error:
org.hibernate.exception.JDBCConnectionException: could not execute query
what makes it more interesting is the fact that when this situation happens, it's not like 100% of queries end up to this exception! sometimes some of them are executed properly.
an example piece of code in which the error occurs:
public synchronized static Player getPlayerByID(String playerID) throws NoSuchPlayer {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Player player = (Player) session.createQuery(
"from Player as player where player.loginName = '" + playerID
+ "'").uniqueResult();//THE ERROR HAPPENS HERE
session.getTransaction().commit();
if (player == null)
throw new NoSuchPlayer();
return player;
}
and this is the respective stacktrace:
'org.hibernate.exception.JDBCConnectionException: could not execute query
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:99)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.loader.Loader.doList(Loader.java:2297)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2172)
at org.hibernate.loader.Loader.list(Loader.java:2167)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:448)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1258)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
at org.hibernate.impl.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:859)
at com.hposg.domain.HPOSG.getPlayerByID(HPOSG.java:117)
at com.hposg.login.UserSession.getPlayerById(UserSession.java:39)
at com.hposg.controller.struts.BuyAction.execute(BuyAction.java:74)
at sun.reflect.GeneratedMethodAccessor1098.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:441)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:243)
at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:252)
at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:179)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:130)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:165)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:179)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488)
at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:636)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.Connection was implicitly closed due to underlying exception/error:
** BEGIN NESTED EXCEPTION **
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException
MESSAGE: Communications link failure
Last packet sent to the server was 0 ms ago.
STACKTRACE:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
Last packet sent to the server was 0 ms ago.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:532)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:353)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2583)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2871)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1601)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1710)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2436)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1402)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1556)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1849)
at org.hibernate.loader.Loader.doQuery(Loader.java:718)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270)
at org.hibernate.loader.Loader.doList(Loader.java:2294)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2172)
at org.hibernate.loader.Loader.list(Loader.java:2167)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:448)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1258)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
at com.hposg.domain.HPOSG.getGameList(HPOSG.java:222)
at com.hposg.controller.struts.GamesLoadActionAjax.execute(GamesLoadActionAjax.java:23)
at sun.reflect.GeneratedMethodAccessor1047.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:441)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:243)
at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:252)
at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:179)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:130)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:165)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:179)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488)
at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:636)
Caused by: java.io.EOFException
at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1958)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2372)
... 83 more
** END NESTED EXCEPTION **
at sun.reflect.GeneratedConstructorAccessor139.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:532)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:353)
at com.mysql.jdbc.Util.getInstance(Util.java:336)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:985)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
at com.mysql.jdbc.Connection.checkClosed(Connection.java:1103)
at com.mysql.jdbc.Connection.prepareStatement(Connection.java:3865)
at com.mysql.jdbc.Connection.prepareStatement(Connection.java:3831)
at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:534)
at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:452)
at org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:161)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1596)
at org.hibernate.loader.Loader.doQuery(Loader.java:717)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270)
at org.hibernate.loader.Loader.doList(Loader.java:2294)
... 74 more
'
and finally this is my hibernate config file:
<?xml version='1.0' encoding='utf-8'?>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost/hposg?characterEncoding=UTF-8
</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<!-- Mapping files -->
<mapping resource="com/hposg/hibernate/resources/Player.hbm.xml"/>
<mapping resource="com/hposg/hibernate/resources/Game.hbm.xml"/>
<mapping resource="com/hposg/hibernate/resources/Portfolio.hbm.xml"/>
<mapping resource="com/hposg/hibernate/resources/Purchase.hbm.xml"/>
<mapping resource="com/hposg/hibernate/resources/Sale.hbm.xml"/>
<mapping resource="com/hposg/hibernate/resources/Message.hbm.xml"/>
<mapping resource="com/hposg/hibernate/resources/Admin.hbm.xml"/>
<mapping resource="com/hposg/hibernate/resources/News.hbm.xml"/>
<mapping resource="com/hposg/hibernate/resources/Invitation.hbm.xml"/>
<mapping resource="com/hposg/hibernate/resources/CustomGameCreationRequest.hbm.xml"/>
<mapping resource="com/hposg/hibernate/resources/Alert.hbm.xml"/>
<mapping resource="com/hposg/hibernate/resources/AlertLimits.hbm.xml"/>
<mapping resource="com/hposg/hibernate/resources/StockIndex.hbm.xml"/>
<mapping resource="com/hposg/hibernate/resources/HPOSG.hbm.xml"/>
<mapping resource="com/hposg/hibernate/resources/Tradeable.hbm.xml"/>
<mapping resource="com/hposg/hibernate/resources/GlobalGameConfig.hbm.xml"/>
<mapping resource="com/hposg/hibernate/resources/Announcement.hbm.xml"/>
<mapping resource="com/hposg/hibernate/resources/AdviserMsg.hbm.xml"/>
<mapping resource="com/hposg/hibernate/resources/SystemConfiguration.hbm.xml"/>
</session-factory>
btw I'm not using c3p0 connection pool, I don't know if this is relevant or not. just felt to mention it.
Any ideas?
Hibernate uses a primitive connection pooling mechanism, which is not suitable for production purposes (it even says that in the log file!). You should always use a connection pooling mechanism, be it the one provided by your container, be it a mechanism bundled in your application (c3p0, for instance). The exception you are seeing is because MySQL closed an "old" connection which Hibernate is still using. You can try to add a JDBC URL option, like "autoReconnect", but that's really not a long-term solution.
Ideally, you should configure your container to supply the connections to your application through JNDI. This is easily done with a "-ds.xml" file in JBoss or with a context.xml for Tomcat.
这篇关于继续收到org.hibernate.exception.JDBCConnectionException:无法执行查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!