简体   繁体   English

休眠2:mysql:套接字写入错误

[英]hibernate 2 : mysql : socket write error

I'm developping a web application in jee and i got this problem only on the server. 我正在jee中开发Web应用程序,并且仅在服务器上遇到了此问题。 I'm using a tomcat 7, hibernate 2 ,mysql, and c3p0 to manage pool connection. 我正在使用tomcat 7,hibernate 2,mysql和c3p0来管理池连接。 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 这是我对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. 问题是MySQL服务器在一定的空闲时间后关闭了连接,但是连接池无法感知到这一点。 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. 原因之一是配置中的连接超时过大:3000秒太大了,因此连接池将使连接保持比MySQL服务器更长的时间。 Reduce it drastically (eg to 100 or so)! 大幅降低(例如减少至100左右)!

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. 然后,您应该配置一个可由C3P0使用的测试查询,以在连接移交之前检查连接是否仍然有效。 This should be a very simple/efficient SQL query. 这应该是一个非常简单/高效的SQL查询。 For MySQL, you can use: 对于MySQL,您可以使用:

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

Note that MySQL also has a special connection tester class for C3P0. 请注意,MySQL还为C3P0提供了一个特殊的连接测试器类。 You can configure C3P0 to use this class to test open connections: 您可以配置C3P0以使用此类来测试打开的连接:

<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. 因此,如果可行,您可以跳过测试查询属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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