I am not able to understand advantages of using Hibernate Callback method, are there any advantages or specific use case where we should go for it.
public List findRecentRequests(final int offset, final int length)
{
List list = getHibernateTemplate().executeFind(new HibernateCallback()
{
public Object doInHibernate(Session session) throws HibernateException
{
Query q = session.createQuery(FIND_RECENT_REQUESTS);
q.setFirstResult(offset);
q.setMaxResults(length);
return q.list();
}
});
return list;
}
Also one more important question is that does HibernateCallback method close session everytime after query returns values? I have use case where am calling this function multiple times on every refresh of status page and so will it everytime open session and query database or will it store query results in memory and then everytime I make call to this function, results would be popped out from memory.
I have read(Reference):
The spring HibernateTemplate.execute() by default closes any open sessions upon completion. When used with lazy initialization you may get a LazyInitializationException like the following
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
Any reference to relevant documentation part would be highly appreciated.
Update:
In my case am using ejb transactions and setting it to "support" and i believe in that case as transaction is set to support, it's optional and so everytime new session will be created and hibernate will query database to get results and so that's were am having bottleneck, will that an right assumptions to make?
See Question&Answers more detail:os