Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Hibernate connection memory leak problem

Posted on 2010-11-08
4
Medium Priority
?
1,106 Views
Last Modified: 2012-05-10
Opening a session using a Hibernate SessionFactory is using lots of memory. Even when the session is closed the allocated memory doesn’t reduce. We observed, using the JProfiler/CPU tab view, that the buildSessionFactory used about 30% of the CPU time. This particular behavior is approximately similar in Hibernate 3.2.4, 3.3.1 and 3.5.4. We tested using the attached code.

Even this simple test used up HUGE memory.


public void useSession(String database)

throws Exception {

	Session session = null;

	try {

		Configuration myConfig = new  Configuration().setProperty(

					CONNECTION_DRIVER, SYBASE_DRIVER).setProperty(

					CONNECTION_DIALECT, SYBASE_DIALECT).setProperty(CONNECTION_URL,

					CommonConfiguration.get(database + CC_URL))

					.setProperty(CONNECTION_USERNAME,

									CommonConfiguration.get(CC_SYBASE_USER_ID))

					.setProperty(CONNECTION_PASSWORD,

									CommonConfiguration.get(CC_SYBASE_PASSWORD))

					.setProperty(CONNECTION_POOL_SIZE, ZERO).setProperty(

									CONNECTION_HOW_SQL, TRUE);



				SessionFactory factory = myConfig.buildSessionFactory();

				session = factory.openSession();

		}

		finally {

			if (session != null) {

					session.close();

			}

		}                               

	}

Open in new window

0
Comment
Question by:M_SYED
  • 2
4 Comments
 
LVL 10

Expert Comment

by:Hegemon
ID: 34086363
Not clear what your question is. Yes, SessionFactory is a heavyweight and slow object and you normally create it only once. It consumes a lot of memory as it uses a number of libraries.

". Even when the session is closed the allocated memory doesn’t reduce. "

That's [almost] correct, SessionFactory should be kept in memory once built. However, Session objects are GC'd after closing and going out of scope.
0
 
LVL 2

Accepted Solution

by:
sandeep1984 earned 1500 total points
ID: 34092331
You are creating session factory for every request which is wrong. This is a costly operation and need to be performed only once, and the same session factory should be used thereafter.

A sample util which will create session factory once. could be tweaked based on your needs.

http://www.koders.com/java/fid9CBB4F8729E37C58C6D34C7C5E0583BA9D0450D3.aspx?s=O#L3
0
 

Author Comment

by:M_SYED
ID: 34136898
Thank you for the comments.

We are in the process of reviewing and rewriting our code to ensure that SessionFactory is reused when possible.

I will keep this thred updated with the results.
0
 

Author Closing Comment

by:M_SYED
ID: 35408521
I had to do more research but the basic premise pointed out by the response was accurate.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

886 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