-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 
Author Message
 Post subject: Problem with caching
PostPosted: Wed Nov 04, 2009 12:09 pm 
Newbie

Joined: Wed Nov 04, 2009 11:53 am
Posts: 5
Hi all,

I have a caching problem with the code below:

Code:
MultiFieldQueryParser parser = new MultiFieldQueryParser(fields, new StandardAnalyzer());
org.apache.lucene.search.Query searchQuery = parser.parse(query);
org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery(searchQuery, Test.class);
List results=hibQuery.list();


It seems that what ever I do with the cache settings I always get old data for the result items returned from the above code. This is only a problem when I update content for an existing object. New objects and deleting objects work fine.

While I am not sure if the above query gets the data from the database or Lucene index, I have checked them both and they have been updated with the new content. However, the only way to get the new content from the above query is to restart the servlet container, so previous query must be cached in memory somehow.

Anyone have any ideas what I could try?

I have switched off both first and second level cache in the hibernate configuration, but no luck:(

I execute the following code after every object update, but again no luck flushing the cache :(

Code:
FullTextSession fullTextSession = Search.getFullTextSession(dp);
fullTextSession.flush();
fullTextSession.flushToIndexes();
fullTextSession.clear();


Hope someone is able to give me pointers on this.

Big thanks in advance :)

Olli


Top
 Profile  
 
 Post subject: Re: Problem with caching
PostPosted: Thu Nov 05, 2009 3:33 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

first things first, the data gets in per default loaded from the database (unless you are using projections). Of course if you have a second level cache configured it will be used it well. However, if everything is configured correctly and you have proper session and transaction management the cache will of course be in sync.

Next, you don't have to call flushToIndexes() all the time. I recommend you rely on the automatic indexing in conjunction with the transaction scope, meaning the index gets automatically updated on a transaction commit.

Some pointers to get to the bottom of this. Check the log file. Have you turned on debug? Post more of your code, especially your configuration and the code between opening and closing a session. Are you running in a standalone app or in a web app?

Have you read through the online documentation of Hibernate Search? This will give you a better understanding of hoe Search works.

--hardy


Top
 Profile  
 
 Post subject: Re: Problem with caching
PostPosted: Thu Nov 05, 2009 4:08 am 
Newbie

Joined: Wed Nov 04, 2009 11:53 am
Posts: 5
Ok, done now a test outside the web app and it seems to be working ok there, so it is something in my web app container that caches...

Regards,

Olli


Top
 Profile  
 
 Post subject: Re: Problem with caching
PostPosted: Thu Nov 05, 2009 5:17 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
My guess would be session/transaction management problems. Are you using the Open Session in View pattern?

--Hardy


Top
 Profile  
 
 Post subject: Re: Problem with caching
PostPosted: Thu Nov 05, 2009 5:32 am 
Newbie

Joined: Wed Nov 04, 2009 11:53 am
Posts: 5
The interesting thing is that if I add some debug immediate after my hibernate code like this:

Code:
MultiFieldQueryParser parser = new MultiFieldQueryParser(fields, new StandardAnalyzer());
org.apache.lucene.search.Query searchQuery = parser.parse(query);
org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery(searchQuery, Test.class);
List results=hibQuery.list();
System.out.println(((Test)results.get(0)).getName());


Outside the container I get the updated name, but if running as wep app then I get the cached name from the previous query.

I am using my custom Hibernate Util like below:

Code:
public class HibernateUtil {

   /** ThreadLocal Session Map */
   public static final ThreadLocal MAP = new ThreadLocal();
   
   private static final Logger LOG = Logger.getLogger(HibernateUtil.class);

   private static final SessionFactory SESSION_FACTORY;

    /** Make default construct private */
    private HibernateUtil() { }

    /** Loads Hibernate config. */
    static {
        try {
            LOG.debug("HibernateUtil.static - loading config");
            SESSION_FACTORY = new Configuration().configure().buildSessionFactory();
            LOG.debug("HibernateUtil.static - end");
        } catch (HibernateException ex) {
            throw new RuntimeException("Exception building SessionFactory: " + ex.getMessage(), ex);
        }
    }

    /**
     * Gets Hibernate Session for current thread.  When finished, users
     * must return session using {@link #closeSession() closeSession()} method.
     * @return Hibernate Session for current thread.
     * @throws HibernateException if there is an error opening a new session.
     */
    public static Session currentSession() throws HibernateException {
        Session s = (Session)MAP.get();
        // Open a new Session, if this Thread has none yet
        if (s == null) {
            s = SESSION_FACTORY.openSession();
            MAP.set(s);
        }
        return s;
    }

    /**
     * Closes the Hibernate Session.  Users must call this method after calling
     * {@link #currentSession() currentSession()}.
     * @throws HibernateException if session has problem closing.
     */
    public static void closeSession() throws HibernateException {
        Session s = (Session)MAP.get();
        MAP.set(null);
        if (s != null) {
            s.close();
        }
    }
}


and then do:

Code:
Session dp=HibernateUtil.currentSession();


for every database call.

Regards,

Olli

P.S. The application is a simple XML-RPC server is that makes any difference.


Top
 Profile  
 
 Post subject: Re: Problem with caching
PostPosted: Mon Nov 09, 2009 3:16 pm 
Newbie

Joined: Wed Nov 04, 2009 11:53 am
Posts: 5
This is one weird problem... Just to test it out I decided to move the application from Tomcat to Resin. And while with Tomcat it will cache the search result every time, with Resin it will allow couple of updates to the same object and then start caching.

No idea what is going on and how to debug it...

Olli


Top
 Profile  
 
 Post subject: Re: Problem with caching
PostPosted: Tue Nov 10, 2009 5:45 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
How do you integrate your HibernateUtil into the web application? Are you sure that the session is closed properly? I recommend you implement the open session in view pattern.
How does your annotated entities look like and even more important how does your entity update code look like? Do you handle transactions manually?

--Hardy


Top
 Profile  
 
 Post subject: Re: Problem with caching
PostPosted: Tue Nov 10, 2009 10:23 am 
Newbie

Joined: Wed Nov 04, 2009 11:53 am
Posts: 5
Yes I am an idiot. It was sessions not being closed properly.

Thanks for your help on this one :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.