Solved

Reuse database connection without leading to memory leak

Posted on 2008-06-26
4
912 Views
Last Modified: 2013-11-05
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
Comment
Question by:b_loco
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 10

Accepted Solution

by:
Nellios earned 250 total points
ID: 21872894
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
 
LVL 92

Expert Comment

by:objects
ID: 21872962
Are you closing the result set and statement once you are done with them?

0
 
LVL 92

Expert Comment

by:objects
ID: 21873063
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
 
LVL 10

Expert Comment

by:Nellios
ID: 21873247
@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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Cisco ASA: Java web start no go, asdm launcher no go 3 57
jmeter usage 4 34
Formating field in mysql Advance formatting 1 39
batch vs regular insert in spring DAO 2 21
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question