如何解决MySQL jdbc socket关闭SocketException?

时间:2013-10-07 21:39:30

标签: java mysql sql sockets jdbc

每次在我的日志文件中出现错误:

com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception: 
** BEGIN NESTED EXCEPTION ** 
java.net.SocketException
MESSAGE: Socket closed
STACKTRACE:
java.net.SocketException: Socket closed
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.read(SocketInputStream.java:129)
    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:1994)
    at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2411)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2916)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
    at com.mysql.jdbc.Connection.execSQL(Connection.java:3250)
    at com.mysql.jdbc.Connection.execSQL(Connection.java:3179)
    at com.mysql.jdbc.Statement.executeQuery(Statement.java:1207)
    at com.mchange.v2.c3p0.impl.NewProxyStatement.executeQuery(NewProxyStatement.java:35)

当应用程序在MySQL数据库中执行特定查询时,它始终位于代码的同一点。

查询就像这样:

SELECT M.*, O.id 
FROM order_message M 
INNER JOIN orders O ON M.order_id = O.id 
WHERE O.seller_id = 14224 AND M.sender_user_id <> 14224 
ORDER BY M.creation_date DESC 
LIMIT 5

我注意到(通过EXPLAINING这个查询)它总是使用临时/ filesort来执行。所有索引都已正确创建,我认为这些都无法改善这一点,但我怀疑查询性能或资源利用率是否导致异常错误。

我正在使用亚马逊RDS

1 个答案:

答案 0 :(得分:0)

问题在于我的连接池的配置方式是连接池(c3p0)将丢弃花费超过10秒的任何数据库连接。我正在使用unreturnedConnectionTimeout参数。

c3p0文档页面不鼓励使用此参数。理想情况下,所有连接都应正确关闭(并因此返回池中)

http://www.mchange.com/projects/c3p0/#unreturnedConnectionTimeout

我已将参数增加到60秒,问题就解决了。