This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Beginner needs help with second-level cache Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Beginner needs help with second-level cache" Watch "Beginner needs help with second-level cache" New topic
Author

Beginner needs help with second-level cache

Julien Martin
Ranch Hand

Joined: Apr 24, 2004
Posts: 384
Hello,
I am trying to get the second-level cache to work. I log the summary of stats and I noticed that I always get ZERO second-level cache puts and hits whereas the query-cache gets puts and hits. Is that normal. Can anyone help me understand what's going on??
Thanks in advance,
Julien Martin.

Here is the relevant snippet from my persitence.xml:

Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9924
    
158

Have you added



entry in the hbm.xml file of your hibernate mappings?


[My Blog] [JavaRanch Journal]
Julien Martin
Ranch Hand

Joined: Apr 24, 2004
Posts: 384
Japirai,
I use JPA... Can you advise me on how to proceed please?
Julien.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9924
    
158

I havent used JPA extensively. So i wont be able to help you much. I would believe there would be some class level annotation which will allow you to enable caching the entity - Just a guess.
Julien Martin
Ranch Hand

Joined: Apr 24, 2004
Posts: 384
Thanks anyhow for taking the time to reply!! I'll have a look at the jpa annotations.
Best regards,
Julien.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9924
    
158

Here it is Hibernate Annotations. Section 2.4.8 "Cache" on that page.
Julien Martin
Ranch Hand

Joined: Apr 24, 2004
Posts: 384
Hello again,

I followed the instructions to the letter as follows:



Here is what I get:
**********************************
INFO-org.hibernate.stat.StatisticsImpl-StatisticsImpl.java:463 - Logging statistics....
INFO-org.hibernate.stat.StatisticsImpl-StatisticsImpl.java:464 - start time: 1177342200468
INFO-org.hibernate.stat.StatisticsImpl-StatisticsImpl.java:465 - sessions opened: 8
INFO-org.hibernate.stat.StatisticsImpl-StatisticsImpl.java:466 - sessions closed: 0
INFO-org.hibernate.stat.StatisticsImpl-StatisticsImpl.java:467 - transactions: 0
INFO-org.hibernate.stat.StatisticsImpl-StatisticsImpl.java:468 - successful transactions: 0
INFO-org.hibernate.stat.StatisticsImpl-StatisticsImpl.java:469 - optimistic lock failures: 0
INFO-org.hibernate.stat.StatisticsImpl-StatisticsImpl.java:470 - flushes: 0
INFO-org.hibernate.stat.StatisticsImpl-StatisticsImpl.java:471 - connections obtained: 16
INFO-org.hibernate.stat.StatisticsImpl-StatisticsImpl.java:472 - statements prepared: 16
INFO-org.hibernate.stat.StatisticsImpl-StatisticsImpl.java:473 - statements closed: 16
INFO-org.hibernate.stat.StatisticsImpl-StatisticsImpl.java:474 - second level cache puts: 0
INFO-org.hibernate.stat.StatisticsImpl-StatisticsImpl.java:475 - second level cache hits: 0
INFO-org.hibernate.stat.StatisticsImpl-StatisticsImpl.java:476 - second level cache misses: 0
INFO-org.hibernate.stat.StatisticsImpl-StatisticsImpl.java:477 - entities loaded: 30
INFO-org.hibernate.stat.StatisticsImpl-StatisticsImpl.java:478 - entities updated: 0
INFO-org.hibernate.stat.StatisticsImpl-StatisticsImpl.java:479 - entities inserted: 0
INFO-org.hibernate.stat.StatisticsImpl-StatisticsImpl.java:480 - entities deleted: 0
INFO-org.hibernate.stat.StatisticsImpl-StatisticsImpl.java:481 - entities fetched (minimize this): 0
INFO-org.hibernate.stat.StatisticsImpl-StatisticsImpl.java:482 - collections loaded: 0
INFO-org.hibernate.stat.StatisticsImpl-StatisticsImpl.java:483 - collections updated: 0
INFO-org.hibernate.stat.StatisticsImpl-StatisticsImpl.java:484 - collections removed: 0
INFO-org.hibernate.stat.StatisticsImpl-StatisticsImpl.java:485 - collections recreated: 0
INFO-org.hibernate.stat.StatisticsImpl-StatisticsImpl.java:486 - collections fetched (minimize this): 0
INFO-org.hibernate.stat.StatisticsImpl-StatisticsImpl.java:487 - queries executed to database: 16
INFO-org.hibernate.stat.StatisticsImpl-StatisticsImpl.java:488 - query cache puts: 10
INFO-org.hibernate.stat.StatisticsImpl-StatisticsImpl.java:489 - query cache hits: 0
INFO-org.hibernate.stat.StatisticsImpl-StatisticsImpl.java:490 - query cache misses: 10
INFO-org.hibernate.stat.StatisticsImpl-StatisticsImpl.java:491 - max query time: 765ms
**********************************

The persistence.xml has not changed.

Any clue?

J.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

It looks to me like some values were being loaded into the second level cache. Why would you need to change your persistence.xml. The second level cache is not part of the JPA spec, so nothing would be defined in the spec defined persistence.xml.

Second Level caching here is a Hibernate specific thing, hence the Hibernate annotation instead.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Julien Martin
Ranch Hand

Joined: Apr 24, 2004
Posts: 384
Thanks Mark,

It looks to me like some values were being loaded into the second level cache.

Please tell me what makes you say that?

Why would you need to change your persistence.xml. The second level cache is not part of the JPA spec, so nothing would be defined in the spec defined persistence.xml.

I make good note of this. I thought the property would just be ignored if the persistence provider was changed.

Julien.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

"INFO-org.hibernate.stat.StatisticsImpl-StatisticsImpl.java:488 - query cache puts: 10"

But wait, that is the query cache.

So, which cache provider are you using and did you define your cache region in that providers configuration file?

Not only do you put a cache usage in the mapping, but you also have to tell the provider what cache region to create

Ah, you use OSCacheProvider, not sure their configuration file name, but like for ehcache it is ehcache.xml.

I will also ask why you are using the query-cache. It is useful for data that never changes, because if the results change often, what you will have in the query-cache will be stale pretty quickly.

Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Beginner needs help with second-level cache
 
Similar Threads
Relation between Hibernate Query Cache and Second Level Cache
I can't get my query cache to work...
Hibernate Query Caching - Why the misses?
Could not instantiate cache implementation
Session factory Error