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

  • Home
  • SEARCH
  • 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 6339373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:38:14+00:00 2026-05-24T19:38:14+00:00

In some piece of our code we are using HibernateDaoSupport’s setCacheQueries() method. At first

  • 0

In some piece of our code we are using HibernateDaoSupport’s setCacheQueries() method. At first we had a function getByGroupId() that just called setCacheQueries(true), but when doing integration testing this caused Hibernate to throw “duplicate exceptions”.
So I googled around a bit and saw that many used a pattern where they enabled cache before doing a query and disabling it afterwards. I then tried disabling caching after the query, and the duplication bug disappeared. Now I am wondering what this pattern really does? The code below is modeled after this pattern.

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class MyDao extends HibernateDaoSupport{

  public List getByGroupId(Long groupId) {
    getHibernateTemplate().setCacheQueries(true);
    List result = getHibernateTemplate().find(
                    "from Selection where groupId = ? order by sortOrder ASC", groupId);
    getHibernateTemplate().setCacheQueries(false);
    return result;
  }
}

I am unsure on how Spring and Hibernate works together here. If setCacheQueries(false) would empty all cached queries it would make no sense, but if it just disables caching of later queries (until setCacheQueries(true) is called) it would make a little more sense.

  • Is this pattern of turning on/off caching before and after doing queries normal?
  • Does it work (i.e. are the queries cached)?
  • Any idea why it would cause Hibernate to throw exceptions about duplicate entries when not calling setCacheQueries(false) after the query?
  • 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-24T19:38:15+00:00Added an answer on May 24, 2026 at 7:38 pm

    When the HibernateTemplate’s cacheQueries property is true, it automatically makes each Query or Criteria executes cacheable. I.e. it calls Query.setCacheable(true) and Criteria.setCacheable(true) before executing the query/criteria.

    So, your pattern consists in fact in making the query you’re going to execute cacheable, and then reset the flag to false so that the next query is not made cacheable.

    The problem is that if the HibernateTemplate is used by multiple threads, the results of using this pattern are indeterminate. You might have a thread set the flag to true, and then another one immediately reset it to false before the first thread has the time to execute its query. And since the access to the property is not synchronized, you might have visibility problems as well.

    I would use HibernateTemplate.setCacheable(true) only at the creation of the HibernateTemplate, to make sure that all the queries it executes are cacheable. If you don’t want it, then use two different HibernateTemplate instances (one with the cache enabled, and the other without), or use the Hibernate API directly when you want a different caching behavior.

    Regarding your exception, without knowing the use-case, the exact exception and its stack trace, it’s hard to diagnose.

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

Sidebar

Related Questions

Some time ago, I came across a piece of code, that used some piece
I was writing some Unit tests last week for a piece of code that
I have a piece of code here that i really could use some help
When do you know it's time to refactor/review some piece of code ? And
Some time ago I wrote a little piece of code to ask about on
I'm looking to patch a piece of abandonware with some code. The software is
I've inherited a piece of code at work and I've just noticed a fairly
Often in programming, it is a very common requirement that some piece of functionality
This piece of jQuery code posts to one of our php pages. var json
Some web applications, like Google Docs, store data generated by the users. Data that

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.