Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7173785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:53:49+00:00 2026-05-28T15:53:49+00:00

I am not able to understand advantages of using Hibernate Callback method, are there

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T15:53:50+00:00Added an answer on May 28, 2026 at 3:53 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not able to understand what is the use of it.RegisterClientScriptBlock/RegisterStartupScript. When we can
Hello I am not able to understand how to use jQuery plug-ins in cakePHP.
I am using ASP.NET MVC application. However I am not able to understand the
I am not able to understand few things on the Garbage collection. Firstly, how
I'm not able to understand the difference between Thread vs ThreadPool. Consider i've to
I am not able to use the breakpoint in Studio with Javascript. I'm able
I am not able to flush stdin here, is there a way to flush
I'm not able to understand the following multi-dimensional code. Could someone please clarify me?
I am not able to understand the error with the code below which simply
I'm not able to understand the process of calling web services from my iphone

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.