我正在使用Java + MySql。我正在尝试添加批量数据(大约4000万条记录)。我的应用程序适用于几十万条记录,但之后开始给我以下异常:
Exception in thread "main" 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(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.<init>(MysqlIO.java:344)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2333)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2370)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2154)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:792)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.GeneratedConstructorAccessor5.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.ConnectionImpl.getInstance(ConnectionImpl.java:381)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at net.jboss.Database.DatabaseConnection.getConnection(DatabaseConnection.java:48)
at net.jboss.emp.idm.adta(mde.java:96)
at net.jboss.emp.idm.main(mde.java:69)
Caused by: java.net.SocketException: No buffer space available (maximum connections reached?): connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:257)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:294)
... 16 more
我用Google搜索了这个问题并尝试了几乎所有选项(更新Windows注册表,更改my.cnf文件,更改mysql全局参数等)但是它无效。
答案 0 :(得分:2)
然后我分解了1000条记录(我的应用程序要求允许)的事务,并且在第15次迭代后我仍然收到了消息。我的应用程序正在努力创建一个新的EntityManager并为每次迭代关闭它。
解决方案是在迭代之间清除特定类型实体的缓存(您也可以按实体清除缓存)。
EntityManagerFactory.getCache()。evictAll或evict(...)
答案 1 :(得分:1)
答案 2 :(得分:0)
这可能是与mysql或者丢失的连接 您的代码无法关闭现有的连接,因为您说它可用于启动某些记录。因此,如果mysql接受来自其他程序的连接,当你收到此错误消息时,请修改与mysql的最大连接或尝试与其他程序一起检查。
答案 3 :(得分:0)
我遇到了完全相同的问题,每次调用可执行语句后我都会用connectionname.close();
语句修复它。
答案 4 :(得分:-1)
这可以解决你现在的问题:) CommunicationsException