Hibernate 1 天后无法执行查询

Posted

技术标签:

【中文标题】Hibernate 1 天后无法执行查询【英文标题】:Hibernate failed to execute query afte 1 day 【发布时间】:2011-12-07 15:18:45 【问题描述】:

一天后,我的 Web 应用程序出现以下错误。我还没有大量搜索无法解决这个问题。谁能帮帮我?

Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was59461 milliseconds ago.The last packet sent successfully to the server was 59461 milliseconds ago, which  is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
    at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3270)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1932)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2101)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2554)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1761)
    at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1912)
    at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:186)
    at org.hibernate.loader.Loader.getResultSet(Loader.java:1778)
    at org.hibernate.loader.Loader.doQuery(Loader.java:662)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
    at org.hibernate.loader.Loader.doList(Loader.java:2211)
    ... 26 more
Caused by: java.net.SocketException: Broken pipe
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
    at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:65)
    at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:123)
    at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3251)
    ... 36 more

【问题讨论】:

你的问题和这里***.com/questions/7565143/…的问题一模一样 【参考方案1】:

您需要配置休眠以执行虚拟查询,以便您的连接保持活动状态。

<!--connection pool--> 
<property name="hibernate.dbcp.maxActive">10</property> 
<property name="hibernate.dbcp.whenExhaustedAction">1</property> 
<property name="hibernate.dbcp.maxWait">20000</property> 
<property name="hibernate.dbcp.maxIdle">10</property> 

<!-- prepared statement cache--> 
<property name="hibernate.dbcp.ps.maxActive">10</property> 
<property name="hibernate.dbcp.ps.whenExhaustedAction">1</property> 
<property name="hibernate.dbcp.ps.maxWait">20000</property> 
<property name="hibernate.dbcp.ps.maxIdle">10</property> 

<!-- optional query to validate pooled connections:--> 
<property name="hibernate.dbcp.validationQuery">select 1</property> 
<property name="hibernate.dbcp.testOnBorrow">true</property> 
<property name="hibernate.dbcp.testOnReturn">true</property> 

【讨论】:

谁将根据您的配置执行虚拟查询?还是validationQuery 以特定的时间间隔执行? @Maksym 通常底层连接池子系统会自行配置并验证连接(定期或借用或取决于您的配置)【参考方案2】:

如果您使用 c3p0 连接池设置testConnectionOnCheckout=trueidle_test_period=30,则意味着每 30 秒将在后台检查一次连接。

        <property name="hibernate.connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider"/>
        <property name="hibernate.c3p0.min_size" value="5"/>
        <property name="hibernate.c3p0.max_size" value="30"/>
        <property name="hibernate.c3p0.acquire_increment" value="5"/>
        <property name="hibernate.c3p0.timeout" value="60"/>

        <property name="hibernate.c3p0.idle_test_period" value="30" /> <!-- seconds -->
        <property name="hibernate.c3p0.testConnectionOnCheckout" value="true"/>
        <property name="hibernate.c3p0.preferredTestQuery" value="SELECT 1" />

preferredTestQuery 值:

With MySQL: SELECT 1
With Oracle: SELECT 1 FROM DUAL
With Ms SQL Server: SELECT 1

测试您的配置和您的 logger 进行调试:

<logger name="com.mchange.v2.c3p0.impl">
    <level value="DEBUG"/>
</logger>

<logger name="com.mchange">
    <level value="DEBUG"/>
</logger>

【讨论】:

以上是关于Hibernate 1 天后无法执行查询的主要内容,如果未能解决你的问题,请参考以下文章

不断收到 org.hibernate.exception.JDBCConnectionException:无法执行查询

org.hibernate.exception.GenericJDBCException:无法执行查询] 根本原因

JPA/Hibernate 本机查询无法识别参数

SQLGrammarException:无法执行查询:找不到列?

Hibernate 5.1.0 错误无法执行解组并且找不到元素“休眠配置”的声明

无法在 Sql Azure 上删除表