Hibernate连接数据库超时设置autoReconnect=true

 Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 43,369,020 milliseconds ago. The last packet sent successfully to the server was 43,369,020 milliseconds ago. 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:3313)
     at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1940)
     at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2109)
     at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2648)
     at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2077)
     at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2228)
     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)
     at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2095)
     at org.hibernate.loader.Loader.list(Loader.java:2090)
     at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:388)
     at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:338)
     at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)
     at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121)
     at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
     at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:64)
     at com.worldepop.core.business.impl.JPAVirtualTransactionManagerImpl.getAllTransaction(JPAVirtualTransactionManagerImpl.java:86)
     at com.worldepop.web.timer.VirtualTransactionTimer.run(VirtualTransactionTimer.java:51)
     at java.util.TimerThread.mainLoop(Timer.java:512)
     at java.util.TimerThread.run(Timer.java:462)
    Caused by: java.net.SocketException: 断开的管道
     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:3294)
解决办法:

如果连接闲置过长 (长时间内没有进行数据库操作), mysql就会自动断开连接, 要重启tomcat.  
    不用hibernate的话, connection url加参数: autoReconnect=true

    用hibernate的话, 加如下属性: 

        

<property name="connection.autoReconnect">true</property>

        <property name="connection.autoReconnectForPools">true</property>  
        <property name="connection.is-connection-validation-required">true</property>

   
    要是还用c3p0连接池: 

        

<property name="hibernate.c3p0.acquire_increment">1</property>  
        <property name="hibernate.c3p0.idle_test_period">0</property> 

        <property name="hibernate.c3p0.timeout">0</property>

<property name="hibernate.c3p0.validate">true</property>

   

由于问题产生的根本原因在于服务到数据库的连接长时间没活动,既然重新连接的办法无效,就可以尝试另外一种办法,就是反空闲。
自己写一个线程来反空闲的话,比较麻烦。
最后在网上找到一个办法。为hibernate配置连接池,推荐用c3p0,然后配置c3p0的反空闲设置idle_test_period,只要小于MySQL的wait timeout即可。
在hibernate.cfg.xml中的c3p0链接池的配置中增加下面项:
<!-- configuration pool via c3p0-->

<property name="c3p0.idle_test_period">120</property>  <!-- seconds --><!-- default: 0 --> 

 

你可能感兴趣的:(java,.net,Hibernate,mysql,jdbc)