简体   繁体   English

没有可用的缓冲区空间,已达到最大连接数

[英]No buffer space available, maximum connection reached

package Simple;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.text.*;

public class CurrentProg {
    //connecting to the database

  private static final String DB_DRIVER = "com.mysql.jdbc.Driver";
  private static final String DB_CONNECTION ="jdbc:mysql://localhost:3306/db?autoReconnect=true"; 
  private static final String DB_USER = "root";
  private static final String DB_PASSWORD = "root";
  static Connection dbConnection = null;
  static Statement statement = null;

  static int total=1;

  //Searching between startdate and enddate

  public static java.util.LinkedList searchBetweenDates(java.util.Date startDate, java.util.Date endDate) {

        java.util.Date begin = new Date(startDate.getTime());
        java.util.LinkedList list = new java.util.LinkedList();
        list.add(new Date(begin.getTime()));
        java.util.Date end = new Date(endDate.getTime());
        endDate.setTime(endDate.getTime() + 24*3600*1000);
        Calendar cal = Calendar.getInstance();
        cal.setTime(begin);
       dbConnection = getDBConnection();

        while(begin.compareTo(endDate)<0){
        begin = new Date(begin.getTime() + 86400000);
        list.add(new Date(begin.getTime()));        
        Timestamp timestamp = new Timestamp(new Date().getTime());

        //For a single day calculation: 24hours*60mins=1440 /2 (2 mins time difference as per the requirement) = 720 

                for (int j = 0; j < 720; j++) {
                cal.add(Calendar.MINUTE, 2);            
                timestamp = new Timestamp(cal.getTime().getTime());
                String S = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(timestamp);               
                String[] parts = S.split(" ");
                String date=parts[0];
                String time=parts[1];
                cal.getTime().toString();

           // To create data loop into a List
            List<String> records = new ArrayList<String>();
            StringBuffer record = new StringBuffer();
            for (int i = 1; i <= total; i++) {
            records = new ArrayList<String>(total);         

            int a2 = 220 + j % 31; // 230 - 244 by 1
            String wString = Integer.toString(a2);
            String a = String.valueOf(a2);   
            double b2 = 0.00 + j % 3.7  ; // 1.3 - 3.9 by 0.1 
            String aString = Double.toString(b2);
            String b = String.valueOf(b2);
            b = b.substring(0, Math.min(b.length(), 5));


          record.delete(0, record.length());
          record.append(a + "," + b  +  ",'"+  date + "', '"+  time + "'");
          record.append("\t\t");      
          record.append("\n");
          records.add(record.toString()); 

          //Insert Query
          String insertTableSQL = "INSERT INTO cmd1"
                  + "(a, b, date, time) " + "VALUES"
                  + "("+record.toString()+")";
                  System.out.println("insertTableSQL - " + insertTableSQL);             //  Statement.executeUpdate(insertTableSQL);

          try {

                  statement = dbConnection.createStatement();
                  statement.executeUpdate(insertTableSQL);
                  System.out.println("Record is inserted into Db table!");
                  } catch (SQLException e) {
                  System.out.println(e.getMessage());
                  } 
      try {

            //  dbConnection = getDBConnection();
              statement = dbConnection.createStatement();
              statement.executeUpdate(insertTableSQL);
              System.out.println("Record is inserted into Db table!");
              } catch (SQLException e) {
              System.out.println(e.getMessage());
              } 
        finally {
        //  httpPost.releaseConnection()
            try{
                 if(statement!=null)
                    statement.close();
              }
             finally{
             }

              try{
                 if(dbConnection!=null)
                    dbConnection.close();
              }
              finally{

              }

              }                                            
         }
        }
        }
        return list;
        }

  @SuppressWarnings("unused")
public static void main(String[] args) throws Exception {

      //To enter startDate and enddate
    //  EntityManagerFactory.getCache().evictAll;


    SimpleDateFormat  startDate=new java.text.SimpleDateFormat("yyyy-MM-dd");
    SimpleDateFormat  endDate=new java.text.SimpleDateFormat("yyyy-MM-dd");

                java.util.LinkedList hitList = searchBetweenDates(
                startDate.parse("2016-01-01"),
                endDate.parse("2016-03-01"));
                String[] combo = new String[hitList.size()];
                for(int i=0; i<hitList.size(); i++)
                combo[i] = new java.text.SimpleDateFormat("yyyy-MM-dd").format(((java.util.Date)hitList.get(i)));

  }

private static void insertRecordIntodb() {
    //
  }
  private static Connection getDBConnection() {
    Connection dbConnection = null;
    try {
      Class.forName(DB_DRIVER);
    } catch (ClassNotFoundException e) {
      System.out.println(e.getMessage());
    }
    try {
      dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
      return dbConnection;
    } catch (SQLException e) {
      System.out.println(e.getMessage());
    }
    return dbConnection;
  }  
} 

Server connection failure during transaction. 事务期间服务器连接失败。 Due to underlying exception: 'java.net.SocketException: java.net.SocketException: No buffer space available (maximum connections reached?): connect'. 由于底层异常:'java.net.SocketException:java.net.SocketException:没有可用的缓冲区空间(已达到最大连接数?):connect'。

** BEGIN NESTED EXCEPTION ** 

java.net.SocketException
MESSAGE: java.net.SocketException: No buffer space available (maximum connections reached?): connect

STACKTRACE:

java.net.SocketException: java.net.SocketException: No buffer space available (maximum connections reached?): connect
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:156)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:276)
    at com.mysql.jdbc.Connection.createNewIO(Connection.java:2717)
    at com.mysql.jdbc.Connection.<init>(Connection.java:1509)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at Simple.CurrentProg.getDBConnection(CurrentProg.java:186)
    at Simple.CurrentProg.searchBetweenDates(CurrentProg.java:126)
    at Simple.CurrentProg.main(CurrentProg.java:164)


** END NESTED EXCEPTION **


Attempted reconnect 3 times. Giving up.
Exception in thread "main" java.lang.NullPointerException
    at Simple.CurrentProg.searchBetweenDates(CurrentProg.java:137)
    at Simple.CurrentProg.main(CurrentProg.java:164)

Here I am trying to connect java program with database but when i am trying to insert large data say for 1 month so its only fetching 16000 records not more than that i want the data should be inserted as per the given date range what should i do to get that . 在这里,我试图将Java程序与数据库连接起来,但是当我试图插入1个月的大数据时,它只能读取16000条记录,但不超过我希望应按照给定的日期范围插入数据,我该怎么办得到那个。 In stacktrace its showing an exception as no buffer space available maximum connection reached. 在stacktrace中,它显示异常,因为没有可用的缓冲区空间已达到最大连接数。 Thanks In advance 提前致谢

Here is the modified code. 这是修改后的代码。 I removed a lot of code which is not necessary: 我删除了很多不必要的代码:

public static LinkedList<Date> searchBetweenDates(Date startDate, Date endDate) throws SQLException {
    Date begin = new Date(startDate.getTime());
    LinkedList<Date> list = new LinkedList<Date>();

    list.add(new Date(begin.getTime()));

    endDate.setTime(endDate.getTime() + 24 * 3600 * 1000);
    Calendar cal = Calendar.getInstance();
    cal.setTime(begin);

    dbConnection = getDBConnection();
    PreparedStatement ps = dbConnection.prepareStatement("INSERT INTO cmd1(aaaa, bbbb, datee, timee) VALUES(?, ?, ?, ?)");
    while (begin.compareTo(endDate) < 0) {
        begin = new Date(begin.getTime() + 86400000);
        list.add(new Date(begin.getTime()));
        Timestamp timestamp = new Timestamp(new Date().getTime());

        // For a single day calculation: 24hours*60mins=1440 /2 (2 mins time
        // difference as per the requirement) = 720

        for (int j = 0; j < 720; j++) {
            cal.add(Calendar.MINUTE, 2);
            timestamp = new Timestamp(cal.getTime().getTime());
            String S = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(timestamp);
            String[] parts = S.split(" ");
            String date = parts[0];
            String time = parts[1];
            cal.getTime().toString();

            // To create data loop into a List
            for (int i = 1; i <= total; i++) {

                int a2 = 220 + j % 31; // 230 - 244 by 1
                String a = String.valueOf(a2);
                double b2 = 0.00 + j % 3.7; // 1.3 - 3.9 by 0.1
                String b = String.valueOf(b2);
                b = b.substring(0, Math.min(b.length(), 5));

                ps.setString(1, a);
                ps.setString(2, b);
                ps.setString(3, date);
                ps.setString(4, time);

                ps.execute();
            }
        }
    }
    if (ps != null)
        ps.close();
    if (dbConnection != null)
        dbConnection.close();

    return list;
}

What I changed: 我改变了什么:

  • I removed your try/catch's because I wanted to make the code short so that I can edit it here easily. 我删除了try / catch的原因是因为我想使代码简短,以便可以在此处轻松编辑。 But you should handle exception correctly. 但是您应该正确处理异常。 And don't swallow exceptions ever. 并且永远不要吞下异常。 I mean the following is no go : 我的意思是以下内容不可行

     } catch (SQLException e) { System.out.println(e.getMessage()); } 

It is very bad Idea because you'll not see the real cause of the problem in this case; 这是一个非常糟糕的主意,因为在这种情况下您将看不到问题的真正原因。 at least do e.printStackTrace() eventhough it is not recommended in a real project. 即使在实际项目中不建议使用e.printStackTrace() ,至少也要这样做。

  • I exchanged the Statement object for PreparedStatement because it is much more efficient. 我将Statement对象替换为PreparedStatement因为它效率更高。
  • Removed package names and put import statements instead because it is not necessary to do so unless you have different classes from different packages with the same name. 删除软件包名称并放入import语句,因为没有必要这样做,除非您具有相同名称的不同软件包具有不同的类。
  • I changed column names because my DB does not want to accept them. 我更改了列名,因为我的数据库不想接受它们。 Column names like a are very bad. a这样a列名非常糟糕。 Choose instead descriptive names so that you might understand what it is for a couple of months later. 选择描述性名称,以便几个月后您可以理解它的含义。 Don't use column names like date because they are reserved words for some databases systems I know. 不要使用像date这样的列名,因为它们是我所知道的某些数据库系统的保留字。
  • Don't create database resources like Connection in a loop unless it is absolutely needed! 除非绝对需要,否则不要在循环中创建数据库资源(例如Connection) Otherwise, your program will go out of resources because these resources are very expensive! 否则,您的程序将耗尽资源,因为这些资源非常昂贵! That is exactly what you are experiencing right now. 这正是您现在正在经历的。

Hope it helps, otherwise, drop me a comment. 希望对您有所帮助,否则请给我留言。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 没有可用的缓冲区空间(已达到最大连接?)表格Postgres EDB驱动程序 - No Buffer Space available(maximum connection reached?) Form Postgres EDB Driver Java套接字:没有可用的缓冲区空间(达到最大连接数?) - Java Sockets: No buffer space available (maximum connections reached?) jmeter给出错误没有可用的缓冲区空间(已达到最大连接数?):connect - jmeter give an error No buffer space available (maximum connections reached?): connect 休息模板 - 没有可用的缓冲区空间(已达到最大连接数?) - Rest Template - No buffer space available (maximum connections reached?) java.net.SocketException:没有可用的缓冲区空间(已达到最大连接?):connect MongoDB JAVA API驱动程序 - java.net.SocketException: No buffer space available (maximum connections reached?): connect MongoDB JAVA API Driver Java套接字服务器提供java.net.SocketException:没有可用的缓冲区空间(已达到最大连接数?):connect异常 - Java socket server gives java.net.SocketException: No buffer space available (maximum connections reached?): connect exception 使用HTTPBuilder并获得“ java.net.SocketException:没有可用的缓冲区空间(已达到最大连接?):connect” - Using HTTPBuilder and getting “java.net.SocketException: No buffer space available (maximum connections reached?): connect” java.net.SocketException:没有可用的缓冲区空间(已达到最大连接数?):(添加netstat -a) - java.net.SocketException: No buffer space available (maximum connections reached?):(adding netstat -a) 本地异常失败:java.net.SocketException:没有可用的缓冲区空间(已达到最大连接数?): - Failed on local exception: java.net.SocketException: No buffer space available (maximum connections reached?): java.net.SocketException:没有可用的缓冲区空间(已达到最大连接数?):connect - java.net.SocketException: No buffer space available (maximum connections reached?): connect
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM