2009-02-03

JDBC中的Communications link failure due to underlying exception错误的解决办法

Posted in Java, spring+struts+hibernate at 16:50 Author:仲远

标签:

问题描述:使用JDBC连接Mysql数据库时,出现“Connection reset”错误,抛出如下异常:

Connection couldn’t be established to jdbc:mysql://***/***?user=***&password=********
com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception: 

** BEGIN NESTED EXCEPTION ** 

com.mysql.jdbc.CommunicationsException
MESSAGE: Communications link failure due to underlying exception: 
** BEGIN NESTED EXCEPTION ** 

java.net.SocketException
MESSAGE: Connection reset

STACKTRACE:java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at com.mysql.jdbc.util.ReadAheadInputStream.fill(ReadAheadInputStream.java:113)
at com.mysql.jdbc.util.ReadAheadInputStream.readFromUnderlyingStreamIfNecessary(ReadAheadInputStream.java:160)
at com.mysql.jdbc.util.ReadAheadInputStream.read(ReadAheadInputStream.java:188)
at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1910)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2304)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2803)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1573)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1665)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3170)
at com.mysql.jdbc.Connection.configureClientCharacterSet(Connection.java:2436)
at com.mysql.jdbc.Connection.initializePropsFromServer(Connection.java:3913)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2683)
at com.mysql.jdbc.Connection.<init>(Connection.java:1531)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)

……** END NESTED EXCEPTION **

Last packet sent to the server was 203 ms ago.

STACKTRACE:

com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception: 

** BEGIN NESTED EXCEPTION **  java.net.SocketException
MESSAGE: Connection reset

STACKTRACE:

java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at com.mysql.jdbc.util.ReadAheadInputStream.fill(ReadAheadInputStream.java:113)
at com.mysql.jdbc.util.ReadAheadInputStream.readFromUnderlyingStreamIfNecessary(ReadAheadInputStream.java:160)
at com.mysql.jdbc.util.ReadAheadInputStream.read(ReadAheadInputStream.java:188)
at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1910)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2304)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2803)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1573)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1665)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3170)
at com.mysql.jdbc.Connection.configureClientCharacterSet(Connection.java:2436)
at com.mysql.jdbc.Connection.initializePropsFromServer(Connection.java:3913)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2683)
at com.mysql.jdbc.Connection.<init>(Connection.java:1531)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
……

** END NESTED EXCEPTION **

问题分析:这个错误是Java程序没有连接上Mysql,导致无法进行任何与数据库相关的操作。这个错误的本质是一个网络的问题。

问题解决:首先,需要分别排查以下情况:
(1)MySql服务器是否支持TCP/IP连接;
(2)服务器端以及运行Java程序的客户端是否启用了防火墙;
(3)JDBC的驱动是否正确合适;
(4)是否启用了IPv6?如果现在使用的IPv6,那么换成IPv4再试试看;

如果上述情况都排除之后,还是出现如上的异常错误,那么很有可能是你电脑所处的网络存在问题,你可以尝试一下换个地方上网运行这个包含JDBC的Java程序,或者换个时间再次运行。

本文可以自由转载,转载时请保留全文并注明出处:
转载自仲子说 [ http://www.wangzhongyuan.com/ ]
原文链接:

5 Comments »

  1. yan said,

    2009年February5日 at 9:18

    你好!
    我最近也遇到这样的问题
    会报at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1905)
    at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2351)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2862)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1571)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1666)
    at com.mysql.jdbc.Connection.execSQL(Connection.java:2988)
    at com.mysql.jdbc.Connection.execSQL(Connection.java:2917)
    at com.mysql.jdbc.Statement.executeQuery(Statement.java:824)
    at com.zycy.db.DbTrans.executeQuery(DbTrans.java:127)
    这样的错误,重启Tomcat服务后就好了,但是过一晚上就有出现这样的问题
    请楼主指教一下!谢谢了!

  2. 仲远 said,

    2009年February5日 at 10:49

    你的这个问题有可能是Mysql超过8个小时后出错,可能和我文中提到的错误还不太一样。你可以参看下面的文章:
    http://andyao.javaeye.com/blog/38506

  3. kkk said,

    2009年June22日 at 10:09

    你好:
    我的问题是程序有点时候会出现这样的异常,重启web服务器就好了。
    [08:40:31.252] {http–8080-17} Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
    [08:40:31.252] {http–8080-17}
    [08:40:31.252] {http–8080-17} Last packet sent to the server was 14397867 ms ago.
    [08:40:31.252] {http–8080-17} at sun.reflect.GeneratedConstructorAccessor73.newInstance(Unknown Source)
    [08:40:31.252] {http–8080-17} at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    [08:40:31.252] {http–8080-17} at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    [08:40:31.252] {http–8080-17} at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
    [08:40:31.252] {http–8080-17} at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
    [08:40:31.252] {http–8080-17} at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2873)
    [08:40:31.252] {http–8080-17} at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2763)
    [08:40:31.252] {http–8080-17} at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3299)
    [08:40:31.252] {http–8080-17} at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1837)
    [08:40:31.252] {http–8080-17} at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1961)
    [08:40:31.252] {http–8080-17} at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2543)
    [08:40:31.252] {http–8080-17} at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1737)
    [08:40:31.252] {http–8080-17} at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1888)
    [08:40:31.252] {http–8080-17} at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:92)
    [08:40:31.252] {http–8080-17} at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208)
    [08:40:31.252] {http–8080-17} at org.hibernate.loader.Loader.getResultSet(Loader.java:1808)
    [08:40:31.252] {http–8080-17} at org.hibernate.loader.Loader.doQuery(Loader.java:697)
    [08:40:31.252] {http–8080-17} at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
    [08:40:31.252] {http–8080-17} at org.hibernate.loader.Loader.doList(Loader.java:2228)
    [08:40:31.252] {http–8080-17} … 35 more
    [08:40:31.252] {http–8080-17} Caused by: java.net.SocketException: Connection reset
    [08:40:31.252] {http–8080-17} at java.net.SocketInputStream.read(SocketInputStream.java:168)
    [08:40:31.252] {http–8080-17} at com.mysql.jdbc.util.ReadAheadInputStream.fill(ReadAheadInputStream.java:113)
    [08:40:31.252] {http–8080-17} at com.mysql.jdbc.util.ReadAheadInputStream.readFromUnderlyingStreamIfNecessary(ReadAheadInputStream.java:160)
    [08:40:31.252] {http–8080-17} at com.mysql.jdbc.util.ReadAheadInputStream.read(ReadAheadInputStream.java:188)
    [08:40:31.252] {http–8080-17} at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:2329)
    [08:40:31.252] {http–8080-17} at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2774)
    [08:40:31.252] {http–8080-17} … 48 more

  4. Sparda said,

    2011年October13日 at 23:07

    com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

    The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
    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:411)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1116)
    at com.mysql.jdbc.MysqlIO.(MysqlIO.java:344)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2332)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2369)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2153)
    at com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:792)
    at com.mysql.jdbc.JDBC4Connection.(JDBC4Connection.java:47)
    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:411)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:381)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
    at java.sql.DriverManager.getConnection(DriverManager.java:582)
    at java.sql.DriverManager.getConnection(DriverManager.java:185)
    at TalkMe.DBConnection.setConnection(DBConnection.java:30)
    at TalkMe.DBConnection.IsValidUser(DBConnection.java:108)
    at TalkMe.DBConnection.main(DBConnection.java:198)
    Caused by: java.net.ConnectException: Connection timed out: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
    at java.net.Socket.connect(Socket.java:529)
    at java.net.Socket.connect(Socket.java:478)
    at java.net.Socket.(Socket.java:375)
    at java.net.Socket.(Socket.java:218)
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:257)
    at com.mysql.jdbc.MysqlIO.(MysqlIO.java:294)
    … 17 more
    java.lang.NullPointerException
    at TalkMe.DBConnection.IsValidUser(DBConnection.java:112)
    at TalkMe.DBConnection.main(DBConnection.java:198)

    超过8个小时的那个我也该了,本机可以连上,在虚拟机里面也可以连上,但是换另外一台电脑怎么都连不上,防火墙关了也连不上,是在是找不出什么问题,求助~~

  5. Sparda said,

    2011年October13日 at 23:08

    如果可以希望能发个邮件,谢谢~~

Leave a Comment

*
To prove you're a person (not a spam script), type the security text shown in the picture. Click here to regenerate some new text.
Click to hear an audio file of the anti-spam word