I'm not in a position to understand benefits of using Hibernate Callback method, what are the advantages or specific use situation where we ought to go for this.

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);
            return q.list();
    return list;

Also yet another real question is the fact that does HibernateCallback method close session each time after query returns values? I've use situation where am calling this function multiple occasions on every refresh of status page and thus does it each time open session and query database or does it store query leads to memory after which each time I make call for this function, results could be sprang from memory.

I've read(Reference):

The spring HibernateTemplate.execute() automatically shuts any open periods upon completion. When combined with lazy initialization you might get a LazyInitializationException such as the following

org.hibernate.LazyInitializationException: couldn't initialize proxy - no Session

Any mention of the relevant documentation part could be highly appreciated.


During my situation am using ejb transactions and setting it to "support" but for the reason that situation as transaction is placed to aid, it's optional and thus each time new session is going to be produced and hibernate will query database to obtain results and thus that's were am getting bottleneck, will that the right presumptions to create?

For your point about why would you use HibernateCallback. Short answer - it enables you to definitely access the present transactionally bound session to be able to do perform more complicated hibernate functions. More often than not the easy techniques on HibernateTemplate are sufficient, but sometimes you have to go lower towards the Session.

There are 2 parts towards the puzzle.

The very first is the transaction scope that is defined either by utilizing PlatformTransactionManager/TransactionTemplate OR @Transactional annotations. Begin to see the spring paperwork/google for more information.

The second reason is that when you're inside a transaction HibernateTemplate will communicate with the present transaction using a little of miracle.

So an easy operation like hTemplate.save() will have fun playing the transaction. A far more complex much like your example will even have fun playing the transaction. Actually virtually any method on hTemplate will participate.

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

  • If you work with transactions clearly, see first point above, when the transaction scope shuts the transaction is going to be committed and also the session is going to be closed.
  • Without transactions spring produces a session for you personally every time you call a HibernateTemplate method and shuts it immediately later on. This isn't the most well-liked approach as unless of course you do something quite simple the outcomes is going to be detached in the session and you'll get LazyInit exceptions.

An essential indicate note within the second situation above there's NO explicit transaction. You're subject to the car-commit mode from the connection so you may do, inside a callback, save, save, throw exception. The very first save Might have been committed, with no txn there is no guarantee.

My advice, when you are performing any updates, make use of a transaction.

If all of the transaction stuff is totally new for you look into the spring paperwork for that transaction chapter.

If you are using Spring anyway, you need to only use declarative transaction management around your Repository or Service layer to dela with this particular transparently. The PlatformTransactionManager implementation is going to do what's right for the given persistence provider.

It's considered bad practice to depend on lazy collections being initialised after you are from data access code - it always means you have some business logic within the controller / view layers of the application that ought to be moved in to the service layer.