Announcement Announcement Module
No announcement yet.
enabling second level cache issue Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • enabling second level cache issue

    I was using second level cache of hibernate, and the following code, the content in my dao method, works. By that I mean if i repeatly run this dao method, only the first SQL was sent to the DB.

    try {
    Session session = sessionFactory.openSession();
    String queryString = "from User u where";
    Query q = session.createQuery(queryString);
    q.setParameter("username", username);
    q.setCacheable(true); // <---- enable query cache
    return (User)q.uniqueResult();
    catch (HibernateException e) {
    throw SessionFactoryUtils.convertHibernateAccessExceptio n(e);

    But, the follow code does not work. I tried to enable the query cache:

    HibernateTemplate template = new HibernateTemplate(sessionFactory);
    template.setCacheQueries(true); // <--- enable query cache
    return (User)template.execute(
    new HibernateCallback() {
    public Object doInHibernate(Session session) throws HibernateException {
    return session.createQuery("from User u where")
    .setParameter("userName", username)
    .uniqueResult(); }

    Did i do anything wrong?

    Thanks for help in advance...

  • #2
    By &quot;didn't work&quot; I mean

    it sent out SQL to database each time this dao method is called (thus not using seond level cache).


    • #3
      By looking at the Spring source

      By looking at the spring source, I was able to do it now. Case closed.

      public User getUser(final String username) throws DataAccessException {
      HibernateTemplate template = new HibernateTemplate();
      try {
      return (User)template.createQuery(sessionFactory.openSess ion(), "FROM User u where")
      .setParameter("userName", username).uniqueResult();
      } catch (HibernateException e) {
      throw SessionFactoryUtils.convertHibernateAccessExceptio n(e);