Results 1 to 3 of 3

Thread: enabling second level cache issue

Hybrid View

  1. #1
    Join Date
    Oct 2004
    Location
    Ashburn, Virginia
    Posts
    4

    Default 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 u.name=:username";
    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 u.name=:userName")
    .setParameter("userName", username)
    .uniqueResult(); }
    }
    );

    Did i do anything wrong?

    Thanks for help in advance...

  2. #2
    Join Date
    Oct 2004
    Location
    Ashburn, Virginia
    Posts
    4

    Default By "didn't work" I mean

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

  3. #3
    Join Date
    Oct 2004
    Location
    Ashburn, Virginia
    Posts
    4

    Default 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();
    template.setCacheQueries(true);
    try {
    return (User)template.createQuery(sessionFactory.openSess ion(), "FROM User u where u.name=:userName")
    .setParameter("userName", username).uniqueResult();
    } catch (HibernateException e) {
    throw SessionFactoryUtils.convertHibernateAccessExceptio n(e);
    }

    }

Similar Threads

  1. Enabling Lock at row level
    By Fez in forum Data
    Replies: 1
    Last Post: Oct 19th, 2005, 08:49 AM
  2. Replies: 2
    Last Post: Aug 11th, 2005, 09:22 AM
  3. Spring/Hibernate Delete/Update Problem
    By Noname in forum Data
    Replies: 4
    Last Post: Jun 15th, 2005, 11:07 PM
  4. Replies: 0
    Last Post: Apr 9th, 2005, 09:50 AM
  5. Replies: 8
    Last Post: Jan 22nd, 2005, 04:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •