Reputation: 63
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());
}
}
Upvotes: 0
Views: 155
Reputation: 795
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.
Upvotes: 1
Reputation: 310957
PreparedStatement
if you use batches.Upvotes: 0