日期:2014-05-16  浏览次数:20928 次

求助!用Java对MySQL压力测试中并发连接>500,数据量>30000时报Communications link failure
MySQL版本:5.5.22 for Linux (x86_64)
设置了max connections=1000

写了一个Java程序,会同时启动500个线程操作数据库,每个线程保持一个Connection
500个线程要总共向数据库insert 30000条数据时,抛了下面的异常。

500线程*10000数据和200线程*100000数据的测试都通过了。

用最简单的 
conn = DriverManager.getConnection(path, username,password);
获取的连接。

试过用2台电脑,每台跑300个并发,也会出现同样的错误

感觉是MySQL哪的配置有问题,但就是百思不得其解!
百度相似的错误有人说是连接time out的问题,但是我程序启动就连接数据库并执行测试了,然后就出现异常,并不是在等待较长时间之后执行的测试。

求高人指点迷津!!先谢过了!

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

The last packet successfully received from the server was 19,176 milliseconds ago. The last packet sent successfully to the server was 19,176 milliseconds ago.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.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.reuseAndReadPacket(MysqlIO.java:3591)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3480)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4021)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2490)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2671)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2621)
at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:842)
at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:681)
at com.aikaka.performance.mysql.MySqlInsertThread.run(MySqlInsertThread.java:54)
at java.lang.Thread.run(Unknown Source)
Caused by: java.net.SocketException: Software caused connection abort: recv failed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(Unknown Source)
at com.mysql.jdbc.util.ReadAheadInputStream.fill(ReadAheadInputStream.java:114)
at com.mysql.jdbc.util.ReadAheadInputStream.readFromUnderlyingStreamIfNecessary(ReadAheadInputStream.java:161)
at com.mysql.jdbc.util.ReadAheadInputStream.read(ReadAheadInputStream.java:189)
at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:3038)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3491)
... 10 more

------解决方案--------------------
进mysql show processlist 看看是不是连接过多


还有看看back_log这个参数对你是否有帮助
------解决方案--------------------
这与 back_log 无关,是操作系统在监听队列中所能保持的连接数,有点类似一个缓冲,如果草果限制了就会
connection refused,从配置上来看 不是max_connections的问题,19,176 milliseconds ago大概是5小时,但是这时最后一次!你重启然后再连接试一下。
http://mysqlblog.fivefarmers.com/2010/09/06/debugging-communication-link-failure-exceptions-in-connectorj/
文章应该可以帮你。
------解决方案--------------------
connect_timeout
The number of seconds that the mysqld server waits for a connect packet before responding with Bad handshake.
interactive_timeout
The number of seconds the server waits for activity on an interactive connection before closing it.
wait_timeout
The number of seconds the server waits for activity on a noninteractive connection before closing it.
net_read_timeout
The number of seconds to wait for more data from a connection before aborting the read.
net_write_timeout
The number of seconds to wait for a block to be written to a connection before aborting the write.

从以上解释可以看出,connect_timeout在获取连接阶段(authenticate)起作用,interactive_timeout和wait_timeout在连