-->
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.  [ 6 posts ] 
Author Message
 Post subject: Help with 1st level cache
PostPosted: Tue Dec 05, 2006 3:25 am 
Newbie

Joined: Sat Dec 02, 2006 1:09 am
Posts: 12
I have read about Hibernate's first level and second level cache. I would like to see details about everything stored in both but after much reading (including manning books) have not figured out how to access the cache, poke around and view what is in there.

I would like to have access and see all hibernate objects (database rows in the form of an object) etc.

thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 05, 2006 3:00 pm 
Regular
Regular

Joined: Sat Jan 07, 2006 8:30 pm
Posts: 68
I think that 1st level cache (session level) can be handled thru the Session object. That probably will solve your problem. Please keep in mind that that will only evict from current session all the entities loaded and will have no effect over the 2nd level cache.

However the second level cache is the one I try to control. I need to clear it when I want.

In the past I've seen different approach using OSCache where the there is a cache object that then is fed to the hibernate factory but now I don't use that approach. I need just to be able to get my hand on the cache manager, the one that's registered in the factory and say clear(). How do I do that. This should be independent of the cache manager user(EHCache,OSCache) etc.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 05, 2006 7:09 pm 
Regular
Regular

Joined: Sat Jan 07, 2006 8:30 pm
Posts: 68
This should do it...

SessionFactoryImpl sessionFactoryImpl = (SessionFactoryImpl)session.getSessionFactory();
Map cacheRegionsMap = sessionFactoryImpl.getAllSecondLevelCacheRegions() ;
Collection<Cache> cacheRegions = cacheRegionsMap.values();
for (Cache cache : cacheRegions) {
cache.clear();
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 08, 2006 4:25 pm 
Newbie

Joined: Sat Dec 02, 2006 1:09 am
Posts: 12
Thanks - I do not necessarily want to clear it or evict anything. I just want to poke around and see all objects there and all data in the objects. Any idea how to get a reference to the cach and dump it out to a log file (debug to log4j) or the screen?

thanks for the help.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 08, 2006 5:56 pm 
Beginner
Beginner

Joined: Wed Jul 13, 2005 2:18 pm
Posts: 44
For the 2nd-level cache, you can use the Statistics API. See SessionFactory.getStatistics(), Statistics.getSecondLevelCacheStatistics(), and the SecondLevelCacheStatistics class in the Javadocs for specific details. Also note that you'll have to turn on generation of statistics via a property setting on the session factory. See section 3.4.6 of the hibernate manual.

A word of warning - the Statistics API has bugs of the NPE/CCE variety. I wouldn't put any code that accesses the API into production without being very careful. For example, there's a ClassCastException that occurs when you've got a cached query result in the 2nd-level cache and you try to access it.

_________________
- Matt


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 08, 2006 6:23 pm 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
You should use the session.getPersistenceContext() method to see the things that Hibernate is tracking.

Hope this helps.

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


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