18

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?

Rachel
  • 100,387
  • 116
  • 269
  • 365
  • You're supposed to initialize lazy collections before you return from your data access layer where the session is open. Or use `OpenSessionInViewFilter` – millimoose Jan 23 '12 at 19:22
  • @Inerdial: I would really appreciate if you can elaborate more with an example? – Rachel Jan 23 '12 at 19:24
  • The Hibernate docs deal with [initialising collections and proxies](http://docs.jboss.org/hibernate/core/4.0/manual/en-US/html/performance.html#performance-fetching-initialization) already. Spring also provides an implementation of the ["Open Session in View filter"](http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/orm/hibernate4/support/OpenSessionInViewFilter.html) mentioned by the Hibernate docs. – millimoose Jan 23 '12 at 19:36

3 Answers3

22

To your point about why use HibernateCallback. Short answer - it allows you to access the current transactionally bound session in order to do perform more complex hibernate functions. Most of the time the simple methods on HibernateTemplate are sufficient, but sometimes you need to go down to the Session.

There's two parts to the puzzle.

The first is the transaction scope which is defined either by using PlatformTransactionManager / TransactionTemplate OR @Transactionalannotations. See the spring docs/google for more info.

The second is that when you are within a transaction HibernateTemplate will interact with the current transaction using a bit of magic.

So a simple operation like hibernateTemplate.save() will participate in the transaction. A more complex like your example will also participate in the transaction. In fact pretty much any method on hTemplate will participate.

So know to your question about when does the session get closed

  • If you are using transactions explicitly, see first point above, then when the transaction scope closes the transaction will be committed and the session will be closed.
  • Without transactions spring creates a session for you each time you call a HibernateTemplate method and closes it immediately afterwards. This is not the preferred approach as unless you are doing something very simple the results will be detached from the session and you will get LazyInit exceptions.

An important point to note in the second case above there is NO explicit transaction. You are at the mercy of the auto-commit mode of the connection so you might do, in a callback, save, save, throw exception. The first save MAY have been committed, without a transaction there's no guarantee.

My advice, when doing any updates, use a transaction.

If all the transaction stuff is new to you check the spring docs for the transaction chapter.

Gray
  • 115,027
  • 24
  • 293
  • 354
Mike Q
  • 22,839
  • 20
  • 87
  • 129
  • 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? – Rachel Jan 23 '12 at 19:57
  • Sorry I'm not following your question about bottlenecks, can you rephrase? – Mike Q Jan 23 '12 at 20:10
  • what i meant was that since am using "support" ejb transaction and so transaction is optional and because of that session is created everytime and in every session hibernate hits database to get result out and as function is called on every refresh, there are many hits to database retrieving thousands of records and bogging down system response time, clear little bit? – Rachel Jan 23 '12 at 20:12
  • Unless you have some sort of caching enabled in hibernate, no matter what transaction system you are using your code will always hit the database. – Mike Q Jan 23 '12 at 21:11
  • when we using hibernate, don't we store result in session and then get them from the session rather hitting underlying database? – Rachel Jan 23 '12 at 21:52
  • the hibernate session stores entities. So if you did "get object of type A with identifier X" twice with the same session then the second time would not hit the DB. The same does not hold for HQL queries which always hit the DB. – Mike Q Jan 23 '12 at 22:52
  • hmm, i was under the impression that HQL queries would also not hit database and get information from memory. – Rachel Jan 24 '12 at 02:37
  • I suggest you use a database trace tool or turn on hibernate sql logging to get an idea of when hibernate does/doesn't hit the DB – Mike Q Jan 24 '12 at 09:21
  • I am not sure how to do it using MySQL, is there any generic tool for that? – Rachel Jan 24 '12 at 11:37
  • I think you need to ask a new question. – Mike Q Jan 24 '12 at 12:14
6

If you're using Spring anyway, you should just use declarative transaction management around your Repository or Service layer to deal with this transparently. The PlatformTransactionManager implementation will do what's appropriate for the given persistence provider.

It's considered bad practice to rely on lazy collections being initialised after you're out of data access code - it usually means you've got some business logic in the controller / view layers of your app which should be moved into the service layer.

millimoose
  • 39,073
  • 9
  • 82
  • 134
0

There is no real advantage in using the HibernateCallback over obtaining the current session, since the first thing execute(...) does is:

...
session = obtainSessionFactory().getCurrentSession();
...

So it's a matter of taste to chose between

hibernateTemplate.execute(session -> session.createQuery(...));

or

hibernateTemplate.getSessionFactory().getCurrentSession().createQuery(...));

or using a helper method getSession()

getSession().createQuery(...));

Your code could also look like

@Transactional(readOnly = true)
public List findRecentRequests(final int offset, final int length) {
    Query q = getSession().createQuery(FIND_RECENT_REQUESTS);
    q.setFirstResult(offset);
    q.setMaxResults(length);
    return q.list();
}
bebbo
  • 2,830
  • 1
  • 32
  • 37