【问题标题】:Communications link failure when execute a procedure in mysql在mysql中执行过程时通信链接失败
【发布时间】:2014-01-03 08:25:30
【问题描述】:

我有一个带有 mysql 的 web 服务器,一些配置如下:

mysql: mysql  Ver 14.12 Distrib 5.0.95, for redhat-linux-gnu (x86_64) using readline 5.1
spring: 3.05
mysql-driver:5.1.13

第一次出现这个异常是关于2013-12-13 02:33,然后我做了一些事情:
1、在mysql url中添加autoReconnect=true
2、在ProxoolDataSource配置中添加<property name="houseKeepingTestSql" value="select CURRENT_DATE"></property>

我以为bug已经修复了,但是我错了,这个异常又出现了,而且几乎是同时出现的(2013-12-16 02:33)。

这是堆栈跟踪:

The last packet successfully received from the server was 131,609 milliseconds ago.  The last packet sent successfully to the server was 2 milliseconds ago.
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.6.0_41]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) ~[na:1.6.0_41]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) ~[na:1.6.0_41]
        at java.lang.reflect.Constructor.newInstance(Constructor.java:513) ~[na:1.6.0_41]
        at com.mysql.jdbc.Util.handleNewInstance(Util.java:409) ~[mysql-connector-java-5.1.13-bin.jar:na]
        at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1118) ~[mysql-connector-java-5.1.13-bin.jar:na]
        at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3055) ~[mysql-connector-java-5.1.13-bin.jar:na]
        at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2941) ~[mysql-connector-java-5.1.13-bin.jar:na]
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3489) ~[mysql-connector-java-5.1.13-bin.jar:na]
        at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959) ~[mysql-connector-java-5.1.13-bin.jar:na]
        at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2113) ~[mysql-connector-java-5.1.13-bin.jar:na]
        at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2568) ~[mysql-connector-java-5.1.13-bin.jar:na]
        at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2113) ~[mysql-connector-java-5.1.13-bin.jar:na]
        at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1364) ~[mysql-connector-java-5.1.13-bin.jar:na]
        at com.mysql.jdbc.CallableStatement.execute(CallableStatement.java:879) ~[mysql-connector-java-5.1.13-bin.jar:na]
        at sun.reflect.GeneratedMethodAccessor121.invoke(Unknown Source) ~[na:na]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[na:1.6.0_41]
        at java.lang.reflect.Method.invoke(Method.java:597) ~[na:1.6.0_41]
        at org.logicalcobwebs.proxool.ProxyStatement.invoke(ProxyStatement.java:100) ~[proxool-0.9.1.jar:na]
        at org.logicalcobwebs.proxool.ProxyStatement.intercept(ProxyStatement.java:57) ~[proxool-0.9.1.jar:na]
        at $java.sql.Wrapper$$EnhancerByProxool$$df6af44.execute(<generated>) ~[proxool-cglib.jar:na]
        at sun.reflect.GeneratedMethodAccessor74.invoke(Unknown Source) ~[na:na]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[na:1.6.0_41]
        at java.lang.reflect.Method.invoke(Method.java:597) ~[na:1.6.0_41]
        at org.apache.ibatis.logging.jdbc.PreparedStatementLogger.invoke(PreparedStatementLogger.java:58) ~[mybatis-3.1.1.jar:3.1.1]
        at com.sun.proxy.$Proxy39.execute(Unknown Source) ~[na:na]
        at org.apache.ibatis.executor.statement.CallableStatementHandler.query(CallableStatementHandler.java:63) ~[mybatis-3.1.1.jar:3.1.1]
        at org.apache.ibatis.executor.statement.RoutingStatementHandler.query(RoutingStatementHandler.java:70) ~[mybatis-3.1.1.jar:3.1.1]
        at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:57) ~[mybatis-3.1.1.jar:3.1.1]
        at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:267) ~[mybatis-3.1.1.jar:3.1.1]
        at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:141) ~[mybatis-3.1.1.jar:3.1.1]
        at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:105) ~[mybatis-3.1.1.jar:3.1.1]
        at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:81) ~[mybatis-3.1.1.jar:3.1.1]
        at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:101) ~[mybatis-3.1.1.jar:3.1.1]
        at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:95) ~[mybatis-3.1.1.jar:3.1.1]
        at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:59) ~[mybatis-3.1.1.jar:3.1.1]
        at sun.reflect.GeneratedMethodAccessor91.invoke(Unknown Source) ~[na:na]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[na:1.6.0_41]
        at java.lang.reflect.Method.invoke(Method.java:597) ~[na:1.6.0_41]
        at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:355) ~[mybatis-spring-1.1.1.jar:1.1.1]
        ... 28 common frames omitted
Caused by: java.net.SocketException: Socket closed
        at java.net.SocketInputStream.socketRead0(Native Method) ~[na:1.6.0_41]
        at java.net.SocketInputStream.read(SocketInputStream.java:129) ~[na:1.6.0_41]
        at com.mysql.jdbc.util.ReadAheadInputStream.fill(ReadAheadInputStream.java:114) ~[mysql-connector-java-5.1.13-bin.jar:na]
        at com.mysql.jdbc.util.ReadAheadInputStream.readFromUnderlyingStreamIfNecessary(ReadAheadInputStream.java:161) ~[mysql-connector-java-5.1.13-bin.jar:na]
        at com.mysql.jdbc.util.ReadAheadInputStream.read(ReadAheadInputStream.java:189) ~[mysql-connector-java-5.1.13-bin.jar:na]
        at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:2499) ~[mysql-connector-java-5.1.13-bin.jar:na]

并且总是程序querprize() 抛出此异常,其他查询似乎正常。 你能帮我解决这个问题吗?
先谢谢了。

编辑
我已经解决了这个问题,凌晨2点33分,这台服务器上的mysql正在备份数据,运行了很多I/O操作,而我项目中的maxActiveConnectionTime只有120000毫秒。 Proxool 数据源在程序完成并返回之前关闭了当前连接,这就是为什么在这种情况下会发生Socket closed 异常。所以我的解决方案是将maxActiveConnectionTime 设置得更长。

【问题讨论】:

  • MySQL 和您的应用程序是否在同一台主机上运行?
  • 不,它们在不同的服务器上。
  • querprize() 是 MySQL 存储过程,还是 Java 方法?如果是前者,您是否尝试过从 Java 外部调用它(例如 MySQL 命令行工具?如果是后者,我们可以看到它吗?
  • 您确定服务器没有重新启动并切断套接字连接吗?检查uptime

标签: java mysql procedure socketexception


【解决方案1】:

看看this

似乎除了autoReconnect=true url 附件之外,以下属性会有所帮助:

如果在 Spring 中使用 DBCP 作为连接池,请将以下条目添加到数据源定义中

<bean id="datasource" destroy-method="close">
<property name="driverClassName” value="${.jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="validationQuery" value="SELECT 1" />  <--- This
<property name="testOnBorrow" value="true" />         <---- and this
</bean>

如果您在 Spring 中使用 c3p0 作为连接池,请将以下条目添加到您的数据源定义中

<bean id=”pooledDataSource” class=”com.mchange.v2.c3p0.ComboPooledDataSource” destroy-method=”close”>

<property name=”driverClass” value=”${driver}”></property>
<property name=”jdbcUrl” value=”${url}” />
<property name=”user” value=”${username}” />
<property name=”password” value=”${password}” />
<property name=”initialPoolSize” value=”20″ />
<property name=”minPoolSize” value=”10″ />
<property name=”maxPoolSize” value=”50″ />
<property name=”checkoutTimeout” value=”1000″ />
<property name=”maxStatements” value=”500″ />
<property name=”testConnectionOnCheckin” value=”false” />   <---- this
<property name=”testConnectionOnCheckout” value=”true” />   <-    and this
<property name=”maxIdleTime” value=”1800″ />
<property name=”idleConnectionTestPeriod” value=”1000″ />
</bean>

【讨论】:

    猜你喜欢
    • 2012-12-21
    • 2023-02-01
    • 2015-01-27
    • 1970-01-01
    • 2021-01-12
    • 1970-01-01
    • 2017-07-31
    • 2014-06-09
    • 2016-09-25
    相关资源
    最近更新 更多