user322406
user322406

Reputation: 5

java.lang.OutOfMemoryError: Java heap space

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

Upvotes: 0

Views: 2941

Answers (3)

TMN
TMN

Reputation: 3070

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).

Upvotes: 1

Rahul Sharma
Rahul Sharma

Reputation: 77

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

Upvotes: -2

dty
dty

Reputation: 18998

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

Upvotes: 7

Related Questions