Solved

Hibernate connection memory leak problem

Posted on 2010-11-08
4
1,018 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 500 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse package explorer vs project explorer view 2 129
thymeleaf natural templating vs JSP 2 95
Java Options in Zimbra Server 2 52
Eclipse with gitlab 1 41
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

770 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