• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 925
  • Last Modified:

Reuse database connection without leading to memory leak

Let me just describe the whole situation before I talk about my problem.

We developed a software that connects to a database (MySQL) through a very, very slow connection (There is nothing that can be done to increase the connection speed).

Knowing that the connection is slow, we created a class (Singleton) that handles all MySQL queries and that reuses the Connection.
The way it is done can be seen in the code snippet. There is one method (getStatement) that tries to ask the current Connection for a statement. If that fails (the connection is gone) it reconnects to the database before returning the statement.

This technique worked very well and we were able to reduce the queries delay significantly. But then came our current problem:
The software had a memory leak that caused it to consume all available heap space in a couple of hours.

After investigation we discovered that even though there are no references to the Statement or ResultSet objects, the Garbage Collector does not purge them until the connection is closed ...
To test this theory we closed the connection after each use (as seen on the code snippet) and the memory leak was gone.

Finally, my question:
- Is there a way to free the results of a query (Statement and ResultSet) without closing the connection?
- Is there another way to do achieve this goal (reduce the connection overhead) without these consequences (memory leak)?

Thanks in advance
public ResultSet selectQuery(String sql){
		Statement ts = getStatement();
		if(ts == null) return null;
		try {
			ResultSet res = ts.executeQuery(sql);
			
			lc.close(); // ADDED LATER!!! 
			return res;
		} catch (SQLException e) {
			if(debug) e.printStackTrace();
			return null;
		} catch (Exception ee){ // ADDED LATER!!!
			return null;
		}
	}

Open in new window

0
b_loco
Asked:
b_loco
  • 2
  • 2
1 Solution
 
NelliosCommented:
You can use connection poolling for that. With pooling you don't actually close the connection when you invoke connection.close() but you return it to the pool. You can get connections from the pool without having to create new connections etc.

Give dbcp a glimpse.
0
 
objectsCommented:
Are you closing the result set and statement once you are done with them?

0
 
objectsCommented:
don't see how connection pool (of effectively 1) will make any difference to how your app is currently bahaving. You effectively have a connection pool at the moment.
0
 
NelliosCommented:
@objects:
If I get it right, the problem lies in the the database connection is slow and it costs too much to open and close connections. To that problem connection pooling is the answer.
0

Featured Post

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now