tags:

views:

114

answers:

5

i get this error when calling a mysql Prepared Statement every 30 seconds this is the code which is been called:

public static int getUserConnectedatId(Connection conn, int i) throws SQLException {
    pstmt = conn.prepareStatement("SELECT UserId from connection where ConnectionId ='" + i + "'");
    ResultSet rs = pstmt.executeQuery();
    int id = -1;
    if (rs.next()) {
        id = rs.getInt(1);
    }
    pstmt = null;
    rs = null;
    return id;
}

not sure what the problem is :s thanks in advanced.

+6  A: 

You need to close all the resources you create - prepared statement, resultset, etc.

dty
ahh of course! when you look at something for so long you dont see the obvious! :p
A: 

check your memory.

linux command: free

chenge
A: 

Because you are running out of memory as your ResultSet is occupyng more memory compared to default memory.

Solution: At the time of starting the application use this argument java or javaw -Xms 25M -Xmx 1024M

Rahul Sharma
This is completely ridiculous advice. How do you know the process isn't already started with -Xmx2g? How do you know that -Xms25m is not going to cause the process to thrash when it starts? How do you know -Xmx1024m is going to be enough to solve the problem?
dty
A: 

I will add some lines to my previous answer

Force yor Garbage collection by making the main thread sleep for 2s.

Rahul Sharma
More ridiculous advice. Sleeping does not force garbage collection. Garbage collection happens when it needs to. If you get an OOM, the garbage collector has already tried, and failed, to free up memory.
dty
+1  A: 

Why not make the query parameterized and simply change the connection ID? That's how prepared statements were intended to be used. That way you only compile the statement once, and re-use the compiled query plan (or whatever your DB compiles your query into).

TMN