Solved

Hibernate connection memory leak problem

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
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…
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.

624 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