简体   繁体   中英

hibernate 2 : mysql : socket write error

I'm developping a web application in jee and i got this problem only on the server. I'm using a tomcat 7, hibernate 2 ,mysql, and c3p0 to manage pool connection. This error happens evertime.

nov. 22, 2013 3:51:07 PM net.sf.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: 0, SQLState: 08S01
nov. 22, 2013 3:51:07 PM net.sf.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: The last packet successfully received from the server was 232 158 milliseconds ago.  The last packet sent successfully to the server was 232 158 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.
nov. 22, 2013 3:51:07 PM net.sf.hibernate.JDBCException <init>
SEVERE: Cannot open connection
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 232 158 milliseconds ago.  The last packet sent successfully to the server was 232 158 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.GeneratedConstructorAccessor92.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1116)
    at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3364)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1983)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2163)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2618)
    at com.mysql.jdbc.ConnectionImpl.setAutoCommit(ConnectionImpl.java:5000)
    at com.mchange.v2.c3p0.impl.NewProxyConnection.setAutoCommit(NewProxyConnection.java:912)
    at net.sf.hibernate.connection.C3P0ConnectionProvider.getConnection(C3P0ConnectionProvider.java:35)
    at net.sf.hibernate.impl.BatcherImpl.openConnection(BatcherImpl.java:286)
    at net.sf.hibernate.impl.SessionImpl.connect(SessionImpl.java:3326)
    at net.sf.hibernate.impl.SessionImpl.connection(SessionImpl.java:3286)
    at net.sf.hibernate.impl.BatcherImpl.prepareQueryStatement(BatcherImpl.java:65)
    at net.sf.hibernate.loader.Loader.prepareQueryStatement(Loader.java:779)
    at net.sf.hibernate.loader.Loader.doQuery(Loader.java:265)
    at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
    at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:911)
    at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:931)
    at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:59)
    at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:51)
    at net.sf.hibernate.persister.EntityPersister.load(EntityPersister.java:419)
    at net.sf.hibernate.impl.SessionImpl.doLoad(SessionImpl.java:2117)
    at net.sf.hibernate.impl.SessionImpl.doLoadByClass(SessionImpl.java:1991)
    at net.sf.hibernate.impl.SessionImpl.load(SessionImpl.java:1920)
    at com.apside.administration.C_User.GetUsersbyID(C_User.java:189)
    at com.apside.connexion.Login.doPost(Login.java:72)
    at com.apside.connexion.Login.doGet(Login.java:212)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1822)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.net.SocketException: Software caused connection abort: socket write error
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(Unknown Source)
    at java.net.SocketOutputStream.write(Unknown Source)
    at java.io.BufferedOutputStream.flushBuffer(Unknown Source)
    at java.io.BufferedOutputStream.flush(Unknown Source)
    at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3345)
    ... 41 more

here is my configuration of c3p0

<property name="hibernate.c3p0.min_size">5</property>
    <property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">3000</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">300</property>
<property name="current_session_context_class">thread</property>

The problem is that the MySQL server closes the connection after a certain idle time, but the connection pool does not perceive that. So it thinks the connection is still open and does not remove it (or reopen it).

One reason is the large connection timeout in your configuration: 3000 seconds is much too large, so the connection pool will keep the connection much longer than the MySQL server. Reduce it drastically (eg to 100 or so)!

Then you should configure a test query that can be used by C3P0 to check if a connection is still valid before it hands over the connection. This should be a very simple/efficient SQL query. For MySQL, you can use:

<property name="hibernate.c3p0.preferredTestQuery">select 1;</property>

Note that MySQL also has a special connection tester class for C3P0. You can configure C3P0 to use this class to test open connections:

<property name="hibernate.c3p0.connectionTesterClassName">com.mysql.jdbc.integration.c3p0.MysqlConnectionTester</property>

It should be more efficient than the generic approach using the test query. So if it works, you can skip the test query property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM