-->
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.  [ 7 posts ] 
Author Message
 Post subject: Clearing the 2nd-level cache
PostPosted: Mon Apr 19, 2004 9:08 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 8:07 pm
Posts: 229
Location: Brisbane, Australia
Is there any way to clear the 2nd-level cache (we intend to use EHCache)?

Basically, we want to use 2nd-level caching, but we'd also like the production support team to be able to make certain changes to the DB at runtime.
They'd be going around the middle layer of our application, so there's really no way for the cache to know that the DB was updated.

We'd like the prod support team to be able to make these changes without bouncing the application.

I was thinking if we could give the support team a "reset cache" operation that cleared all the cached data, then we'd have the best of both worlds.


Should we use the Cache.clear() method, or the SessionFactory.evict(Class) method?

For query-caching, just doing a call to SessionFactory.evictQueries() should do the job, right?

I guess these things are thread-safe, or do we have to do some kind of application-level locking?

_________________
Cheers,
Shorn.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 19, 2004 10:08 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
I'm not too familiar with the query caching, so I can't speak to that one.

For the other, though, use the Cache.clear() on the appropriate cache reference. Session.evict() only clears the entry from the session-level cache.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 19, 2004 10:10 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 8:07 pm
Posts: 229
Location: Brisbane, Australia
Thanks for the prompt reply Steve.

What about SessionFactory.evict(Class)?

_________________
Cheers,
Shorn.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 19, 2004 10:21 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
My bad, read evict() and just thought of Session.evict(). SessionFactory.evict() is actually short-hand for <obtain correct cache>.clear(), so yes that would also work.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 19, 2004 11:07 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 8:07 pm
Posts: 229
Location: Brisbane, Australia
Sooooo, I'm stuck :)

This is the code I use to clear the cache:
Code:
                log.debug("clearAllCaches(), evicting queries....");
                sessionFactory.evictQueries();
                log.debug("clearAllCaches(), done evicting queries");

                log.debug("clearAllCaches(), evicting classes....");
                for( Iterator i=hibernateConfig.getClassMappings(); i.hasNext(); ){
                    PersistentClass iClass = (PersistentClass) i.next();
                    sessionFactory.evict( iClass.getMappedClass() );
                }
                log.debug("clearAllCaches(), done evicting classes");

I've declared the class I'm interested in caching in hibernate.cfg.xml with:
Code:
<class-cache class="nrm.clas.domain.reference.InteractionMethod" usage="read-write"/>

The list code that does the actual query looks roughly like:
Code:
        Criteria criteria = createCriteria(entityClass);
       
        LogicallyDeletable example  (LogicallyDeletable) entityClass.newInstance();
       
        example.setActive(Boolean.TRUE);
        criteria.add( Example.create(example) );
       
        criteria.addOrder( Order.asc("id") );
        criteria.setCacheable(true);
       
        return criteria.list();

But when I do the cache clear operation, it doesn't seem to work.
The log statements get output, but subsequent calls to the list method don't result in SQL queries being issued. I can only see any newly inserted rows in the DB if I restart the JVM.

Any thoughts on what I'm doing wrong?
I'm currently using the non-production hibernate Hashtable-based CacheProvider, would that make a difference to the query cache?

We're running in a standalone Tomcat instance, so there aren't too many classloaders flying around, so I don't think that's the problem.

I didn't declare any regions when I was setting up the caching, so I figured I wouldn't need to worry about regions when writing the cache-clear operation, is that assumption incorrect?

_________________
Cheers,
Shorn.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 19, 2004 11:21 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Just for the sake of argument :) what happens if you switch back to one of the "supported" cache providers?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 20, 2004 1:00 am 
Pro
Pro

Joined: Tue Aug 26, 2003 8:07 pm
Posts: 229
Location: Brisbane, Australia
Thanks Steve.

I *think* that's it. I stuck EHCache in there and it seems to go okay.
But there's some issues with our GUI that are now confusing the test case, so I'm not exactly sure what's going on anymore, but it looks like using ehcache solves the problem.

How long do things stay in the cache for (by default) with ehcache?

It seems like things leave the cache much faster with EHCache than with the Hashtable Provider.

Anyway, that seems to have solved my problems for now.
Thanks again fou your quick responses.

_________________
Cheers,
Shorn.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.