cuttieveenz
asked on
is it required to flush and clear hibernateTemplate?
Hi ,
I am using Hibernate and Spring. we are using getHibernateTemplate().fin d() 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?
I am using Hibernate and Spring. we are using getHibernateTemplate().fin
ASKER
actually the data consists of CLOB column data...hence it is very large and leads to memory leak
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);
}
ASKER
In my DAO code is as follows:
List emp = getHibernateTemplate().fin d("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().fin d("from Employee");
getHibernateTemplate().flu sh();
getHibernateTemplate().cle ar();
return emp;
Will this help?
List emp = getHibernateTemplate().fin
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().fin
getHibernateTemplate().flu
getHibernateTemplate().cle
return emp;
Will this help?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
flush() is used for flush getHibernateTemplate() content u were used in session factory creation
Just a side question, do you experience LazyInitializationExceptio n when you call to get associated objects from the Employee you queried for, like employee.getDepartment().g etName() ? 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.
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.
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.