0

I'm getting an exception in my query for inserting data into my database, i am inserting a lot of data into my database around 15k lines. this is the exception message:

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. 

my function for the insert query:

public static void insertObject(int modelId,float x,float y,float z,float rx,float ry,float rz){

     try
        {
         con = DriverManager.getConnection(url, user, password);
          String query = " insert into wastobjects (modelid,x,y,z,rx,ry,rz)"
            + " values (?, ?, ?, ?, ?, ?, ?)";
          PreparedStatement preparedStmt = con.prepareStatement(query);
          preparedStmt.setInt (1, modelId);
          preparedStmt.setFloat (2, x);
          preparedStmt.setFloat (3, y);
          preparedStmt.setFloat (4, z);
          preparedStmt.setFloat (5, rx);
          preparedStmt.setFloat (6, ry);
          preparedStmt.setFloat (7, rz);

          preparedStmt.execute();
          con.close();
        }
        catch (Exception e)
        {
          System.err.println("Got an exception!");
          System.err.println(e.getMessage());
        }
}
3
  • You're running out of socket connections. You're creating a new connection with every row. You should either reuse your connection, or you can open up more sockets, with is an operating system command. Look up max socket connections. You should be aware that each socket connection creates a small file, so there are resource constraints, but you can create a lot of connections if you increase the amount. Commented Sep 29, 2015 at 23:40
  • Post the full stack trace Commented Sep 29, 2015 at 23:41
  • i actually tough creating a new connection each time was a bad idea, is there anyway i can reuse the same connection? Commented Sep 29, 2015 at 23:42

2 Answers 2

1

You can reuse your connection instead of creating a new one each time you need to insert a new row.

And try to close the prepared statement and the connection in a finally block.

For example:

Connection con = null;
PreparedStatement ps = null;
try {
    // your code here.
    ps.execute();
    con.commit();
} catch (Exception e){

} finally {
    closeResource(ps)
    closeResource(con);
}

Your new method:

private void closeResource(AutoCloseable resource) {
    if (resource != null) {
        try {
            resource.close();
        } catch (Exception e) {
        }
    }
}

That's why when you get an exception the resources won't close and you will reach the limit of sockets.

Sign up to request clarification or add additional context in comments.

1 Comment

public void closeResources(AutoCloseable resource) { if (resource == null) { return; } try { resource.close(); } catch (Exception e) { System.err.println(e.getMessage()); } }
0
  1. Close the connection in a finally block.
  2. Don't use a new connection per row. Use the same connection.
  3. In fact you can use the same PreparedStatement if you use batches.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.