-->
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: Put ALL entries to 2-nd level cache
PostPosted: Thu Nov 30, 2006 10:17 am 
Newbie

Joined: Thu Nov 30, 2006 10:06 am
Posts: 9
I need to use a table as a dictionary, so I want to load all entries to 2-nd level cache. I need NO assosiations, just load all data to a list. How can I define it with hibernate mappings?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 30, 2006 10:47 am 
Beginner
Beginner

Joined: Thu Aug 24, 2006 6:01 am
Posts: 49
Location: sophia-antipolis, France
You just need to specify the cache in your mappings for each class you want to cache. Use either XML:
Code:
<class name="eg.Cat" .... >
    <cache usage="read-write"/>
    ...
</class>


or annotations:
Code:

@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public abstract class Cat {...}



Then in your hibernate properties, specify the cache provider:
Code:

hibernate.cache.provider_class=
org.hibernate.cache.EhCacheProvider



now whenever you use
load()
get()
list()
iterate()
scroll()
your objects will be put in the 2nd-level cache.

I forgot to mention that if you're not working with a mapped class, maybe you just want to cache query results? Read 19.4. The Query Cache in the reference.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 30, 2006 11:46 am 
Newbie

Joined: Thu Nov 30, 2006 10:06 am
Posts: 9
When i use iterate() or list() hinernate does NOT use cache, it just look into DB. Query cache can help but is really slow. What i want is to store all table entries as a collection of objects, not using query cache.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 30, 2006 12:06 pm 
Beginner
Beginner

Joined: Thu Aug 24, 2006 6:01 am
Posts: 49
Location: sophia-antipolis, France
Sorry, I should have put my references:

Hibernate reference, 19.3. Managing the caches
Quote:
Whenever you pass an object to save(), update() or saveOrUpdate() and whenever you retrieve an object using
load(), get(), list(), iterate() or scroll(), that object is added to the internal cache of the Session.


In section 10.4 of the Hibernate Reference:
Quote:
Occasionally, you might be able to achieve better performance by executing the query using the iterate()
method. This will only usually be the case if you expect that the actual entity instances returned by the query
will already be in the session or second-level cache. If they are not already cached, iterate() will be slower
than list() and might require many database hits for a simple query, usually 1 for the initial select which only
returns identifiers, and n additional selects to initialize the actual instances.


This tells me that the first time you load the objects, you should use list(), and then on subsequent calls, you should use iterate().


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 30, 2006 12:11 pm 
Beginner
Beginner

Joined: Thu Aug 24, 2006 6:01 am
Posts: 49
Location: sophia-antipolis, France
If you want to debug your second-level cache, you can do this like this.

Set up your hibernate properties:

Code:
hibernate.generate_statistics=true
hibernate.cache.use_structured_entries=true


Then in the test code, we can get the cache regions. By default a region corresponds to a fully-qualified class name. For example:

Code:
SessionFactory sessionFactory = (SessionFactory)this.applicationContext.getBean("sessionFactory");
String[] regionNames = sessionFactory.getStatistics().getSecondLevelCacheRegionNames();
for (int i = 0; i < regionNames.length; i++) {
    System.out.println("region " + regionNames[i]);
    Map region = sessionFactory.getStatistics()
                .getSecondLevelCacheStatistics(regionNames[i])
                .getEntries();
}


The debug log shows the cache contents for all of the persistent objects such as this HealthValue object:

Code:
region domain.HealthValue
HealthValue#1
HealthValue#1 now: 1164710337035
HealthValue#1 Creation Time: 1164710275467 Next To Last Access Time: 0
HealthValue#1 mostRecentTime: 1164710275467
HealthValue#1 Age to Idle: 120000 Age Idled: 61568
HealthValue#1 expired?: false


Top
 Profile  
 
 Post subject: How do I clear it all
PostPosted: Tue Dec 05, 2006 12:39 pm 
Regular
Regular

Joined: Sat Jan 07, 2006 8:30 pm
Posts: 68
I need to clear all the second level cache at will. How do I do that ?

I use Ehcache for now - my env. is not clustered yet.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 05, 2006 4:07 pm 
Beginner
Beginner

Joined: Thu Aug 24, 2006 6:01 am
Posts: 49
Location: sophia-antipolis, France
It looks like SessionFactory.evictEntity(String entityName) is what you are looking for.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 05, 2006 6:31 pm 
Regular
Regular

Joined: Sat Jan 07, 2006 8:30 pm
Posts: 68
I need to clear all the cache (2nd level) for all entities and all queries.

Any easy way to do that ? session.clear() will do clear only 1st level cache.

Is my understanding wrong ?

I want to get my hand on the internal org.hibernate.cache.Cache implementor. The rest is simple.


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.