开发者

Hibernate failed to execute query afte 1 day

开发者 https://www.devze.com 2023-04-12 19:28 出处:网络
I am getting following error in my web application after one day. I have dont lot of search on it for couldn\'t fix this problem yet. Can anybody help me out?

I am getting following error in my web application after one day. I have dont lot of search on it for couldn't fix this problem yet. Can anybody help me out?

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.h开发者_运维问答andleNewInstance(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


You need to configure hibernate to execute dummy query so that your connection stays alive .

<!--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> 


If You use c3p0 connection pool set testConnectionOnCheckout=true and idle_test_period=30 it means that connection will be checked in background in every 30 sec.

        <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 values :

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

To test your configuration et Your logger to debug :

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

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号