当前位置: 首页 > 面试题库 >

获取org.hibernate.exception.JDBCConnectionException:即使通过JNDI也无法执行查询

颛孙博易
2023-03-14
问题内容

我使用Struts2 + JSP + Tomcat6+Hibernate+Mysql作为我的J2EE应用程序框架。按照这个]主题,我遇到了出现此错误的问题:

org.hibernate.exception.JDBCConnectionException: could not execute query

看来是由于mysql在n小时后关闭了它的连接。当人们回答时,我更改了hibernate配置以通过JNDI获得数据库连接。这是我采取的操作步骤:

我的hibernate.cfg.xml:

<hibernate-configuration>
  <session-factory>

    <property name="connection.datasource">java:comp/env/jdbc/hposg</property>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="show_sql">true</property>
    <property name="current_session_context_class">thread</property>
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    <property name="hbm2ddl.auto">update</property>
    <property name="hibernate.max_fetch_depth">3</property>

      <!-- Mapping files -->

  </session-factory>
</hibernate-configuration>

我放入META-INF详细信息中的context.xml文件:

<Context>
  <Resource name="jdbc/hposg" 
            global="jdbc/hposg"
            auth="Container"
            type="javax.sql.DataSource" 
            username="root"
            password=""
            driverClassName="com.mysql.jdbc.Driver"
            url="jdbc:mysql://localhost:3306/hposg?autoReconnect=true&amp;characterEncoding=UTF-8"
            maxActive="8" 
            maxIdle="4"/>
</Context>

以及web.xml中的这些修改:

<resource-ref>
    <description>DB Connection</description>
    <res-ref-name>jdbc/hposg</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

奇怪的是,我仍然遇到相同的错误!这是堆栈跟踪:

'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.PortfolioLoadAction.execute(PortfolioLoadAction.java:72)
    at sun.reflect.GeneratedMethodAccessor1032.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.CommunicationsException: Communications link failure

Last packet sent to the server was 0 ms ago.
    at sun.reflect.GeneratedConstructorAccessor109.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.SQLError.createCommunicationsException(SQLError.java:1074)
    at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:2720)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1582)
    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.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:93)
    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)
    ... 74 more
Caused by: java.net.SocketException: Broken pipe
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
    at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
    at java.io.BufferedOutputStream.write(BufferedOutputStream.java:126)
    at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:2703)
    ... 85 more
'

[编辑]我对context.xml进行了一些更改,该问题似乎已解决:

maxActive="-1" 
maxIdle="30"
validationQuery="SELECT 1"
testWhileIdle="true"

[EDIT_2]尽管我之前提到了修改,但不幸的是我仍然遇到相同的错误。有任何想法吗?


问题答案:

在使用Tomcat6 + Hibernate +
MySQL(在本例中为Spring)的生产系统中,我们遇到了同样的问题,我们发现的唯一可靠的解决方案是在MySQL配置中将连接超时设置为较大的值。我不记得具体情况,但我相信底层的公共池代码在验证池中的资源有效时存在问题。

我们未尝试的另一种可能性是使用DBCP以外的替代连接池机制。接下来尝试的是C3PO



 类似资料:
  • 问题内容: 我在设置时遇到问题,我的登录名已通过验证,但未调用execute函数 LoginAction.java: struts.xml: login.jsp: 我只能看到“ 4”被打印(意味着它已通过验证),仅此而已,它并没有转到“ 5” 编辑: 添加了代码段 问题答案: 从Struts2 Spring插件文档中: 通常,在struts.xml中,为每个Action指定类。使用default时

  • 问题内容: 我正在运行一个简单的Calculator应用程序来学习Java的异常处理。我设置了两个要处理的异常:InputMismatchException和ArithmeticException,用于除以零。 ArithmeticException得到处理,并且do-while循环继续。但是在捕获InputMismatchException之后,执行将终止而不是继续循环。 码: 输出: 预期:在

  • 考虑和<代码>雇员< /代码>和<代码>地址< /代码>关系。在和之间有一对一的映射。以下是模型: 现在,当我执行以下HQL查询时,它会在内部生成两个查询。一个取员工,另一个取地址。 由Hibernate生成的SQL查询 因为我使用的是,所以我希望Hibernate只执行一个带有JOIN的查询,一次性获取员工和地址数据。 你知道为什么它要执行两个查询吗?我如何让Hibernate使用join只执行

  • 问题内容: 我正在尝试使用JNDI创建与ActiveMQ的简单连接。 我有 队列名为“ example.A”。 根据接触JNDI的ActiveMQ文档,如果我想通过JNDI使用ConectionFactories和Queues(主题),则必须将jndi.properties文件放在我的类路径中。据我了解,默认情况下,activeMQ类路径是%activemq%/ conf目录。我没有改变。所以我的

  • 问题内容: 今天尝试升级詹金斯。它不再开始了。 即使我尝试删除其目录,它也会干净地重新创建它,但是随后出现以下日志崩溃。 詹金斯1.512在Glassfish 3.1.2.2上 编辑:版本1.421起作用。版本1.422失败。即使擦除了jenkins目录,这也是一致的。 问题答案: 找到了这个,我有两个虚拟服务器: https://gist.github.com/andrewg4153/36935