mysql中jdbc使用批处理插入数据时,在4000条时抛异常。
com.mysql.jdbc.CommunicationsException: The driver was unable to create a connection due to an inability to establish the client portion of a socket.This is usually caused by a limit on the number of sockets imposed by the operating system. This limit is usually configurable. For Unix-based platforms, see the manual page for the 'ulimit' command. Kernel or system reconfiguration may also be required.数据源是txt文本,经过java程序读取处理后写入数据库。在4000条左右的public static void addIpInfo(List<IpInfo> ipList){
Connection conn = null;
PreparedStatement ps = null;
String sql = "insert into ip_library (ip1,ip2,startIp,endIp,country,city,areaId) value (?,?,?,?,?,?,?)";
try{
conn = DBHelper.getConnection();
conn.setAutoCommit(false);
ps = conn.prepareStatement(sql);
for(Iterator<IpInfo> iter = ipList.iterator();iter.hasNext();){
IpInfo ipInfo = iter.next();
ps.setDouble(1,ipInfo.getIp1());
ps.setDouble(2, ipInfo.getIp2());
ps.setString(3, ipInfo.getStartIp());
ps.setString(4, ipInfo.getEndIp());
ps.setString(5, ipInfo.getCountry());
ps.setString(6, ipInfo.getCity());
ps.setInt(7, ipInfo.getAreaId());
ps.addBatch();

}
ps.executeBatch();
conn.commit();

}catch(Exception e){
e.printStackTrace();
}finally{
DBHelper.close(ps, conn);
}
}时候报这个异常,执行完sql后都有把connection关闭,但上午曾经插入40多万条没有出错,同样的代码 ,不明白了

解决方案 »

  1.   

    你这个ipList有多少个元素?
    可以适当减少其批处理大小。
      

  2.   

    我认为是你线程的问题,可能是没有释放的链接太多所导致的。你查看一下你的OS规定是多少的线程。还有就是你的连接池的配置,timeout的时间或者最大连接是多少。还有一点是你在insert的时候用show status观测一下mysql的状态。
      

  3.   

    问题已经解决了,不是线程的问题,是JDBC连接的问题,这段插入代码是正常的,对他的调用也是正常。