Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Hibernate connection memory leak problem

Posted on 2010-11-08
4
Medium Priority
?
1,077 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
[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
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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

715 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