Solved

is it required to flush and clear hibernateTemplate?

Posted on 2009-07-09
8
5,864 Views
Last Modified: 2013-11-11
Hi ,
I am using Hibernate and Spring. we are using getHibernateTemplate().find() in our DAOs. After the find() is called, do we have to explicitly call flush() and clear()? Will it lead to memory leak if we dont flush and clear?
0
Comment
Question by:cuttieveenz
8 Comments
 
LVL 10

Expert Comment

by:mahome
ID: 24811452
flush() don't help, becaus it only synchronizes with the DB
clear() helps if you are loading to much data
you can use evict() instead of clear() for specific objects
http://docs.jboss.org/hibernate/stable/core/reference/en/html/performance.html

It don't load to memory leaks but if you are loading to much data, you have to evict()/clear() so that the session-cache doesn't get too big.

Normaly the session is cleared when it will be closed and you don't have to clear it on you own.
0
 

Author Comment

by:cuttieveenz
ID: 24811599
actually the data consists of CLOB column data...hence it is very large and leads to memory leak
0
 
LVL 10

Expert Comment

by:mahome
ID: 24811663
Then I would advice to iterate through the result and evict each result

Iterator iter = session.createQuery("from CatImpl as cat").iterate();
 

while(iter.hasNext() ) {

	Cat currentCat = (Cat) iter.next();

	session.evict(currentCat);	

}

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:cuttieveenz
ID: 24813201
In my DAO code is as follows:

List emp = getHibernateTemplate().find("from Employee");
return emp;

Here emp is a list of objects which has CLOB data. I get a memory leak due to this clob in hibernate.

That's why i changed the code as follows:

List emp = getHibernateTemplate().find("from Employee");
getHibernateTemplate().flush();
getHibernateTemplate().clear();
return emp;

Will this help?
0
 
LVL 10

Accepted Solution

by:
mahome earned 500 total points
ID: 24813792
Again flush() is no help for you. It only writes the current status of the session in the DB. That is not what you want. The objects remain in the session after flushing().

Your solution only helps partially as after this line:
List emp = getHibernateTemplate().find("from Employee");

all is in the session cache. If that amount is to much it crashes in that line, you have to use my proposed solution.

Here the example for your need:

Iterator iter = session.createQuery("from Employee").iterate();
 

List emps = new ArrayList();

while( iter.hasNext() ) {

        Employee currentEmp = iter.next();

        session.evict(currentEmp); 

        emps.add(currentEmp);    

}

return emps;

Open in new window

0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 25434878
flush() is used for flush getHibernateTemplate() content u were used in  session factory creation
0
 
LVL 5

Expert Comment

by:josephtsang
ID: 25969920
Just a side question, do you experience LazyInitializationException when you call to get associated objects from the Employee you queried for, like employee.getDepartment().getName() ? What I try to ask is: is the Hibernate Session closed right after each call to HibernateTemplate?

Are you using CMT or BMT?? I ask this because the solution to your problem really depends on how you define the demarcation of a transaction and of a Hibernate session.

As mahome suggested, the Hibernate Session object will cache the result of any queries and will clear the cache out automatically when it is closed. But when does a Hibernate Session close? As you use the class  HibernateTemplate from Spring and make a query, if you setting is not correct, you will find that the Hibernate Session will always close itself right after each call.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
scores100 challenge 3 98
mergeTwo  challenge 13 84
mockito example issue 8 62
servlet example issue 6 29
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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 …
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

911 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now