wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes Hibernate second level cache questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate second level cache questions" Watch "Hibernate second level cache questions" New topic
Author

Hibernate second level cache questions

Theodore David Williams
Ranch Hand

Joined: Dec 21, 2009
Posts: 102
I am trying to find out if enabling the second level cache provided by hibernate will help my performance and after reading through the documentation I am still a little confused and i have a couple of questions.

1. Do entities get stored in the second level cache even when you are asking for an entity by something other than its primary key?

i.e.


I search for Field's by name or name and category based on different criteria. So I cannot make name and category a composite primary key because category can sometimes be null. Lets say I do the following multiple times


The name and category are the same for a lot of the queries, so I really do not want to go out to the db every time I perform this query. If I turn on the second level cache will I avoid going to the db every time I make this query?
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

You will only hit the cache if you ask for an entity by its id .

The second level cache is an excellent resource to take advantage of. You'd be amazed at how much work has gone into optimization techniques, both in the primary and secondary cache. Use it!

-Cameron McKenzie
Theodore David Williams
Ranch Hand

Joined: Dec 21, 2009
Posts: 102
Thanks Cameron

One more question.

Will this work with native queries? Or does it only work when you query using EJBQL or HQL?

In my example below I need to make sure that each field is unique based on name and category (ie there is only one row in the db per name and category). To do this I issue a native query with a "SELECT FOR UPDATE" (I want a pessimistic lock, i.e. I want to block other threads until the lock is released). Will I still benefit from the second level cache?

Thanks again!!
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

It won't work with a native query unless the results of the native query is used to create JPA entities, that is, create and initialize JPA POJOs, AND then those created POJOs are associated with the Hibernate Session at some point. I highly doubt that is happening, so a highly probable answer would be 'no.'
Theodore David Williams
Ranch Hand

Joined: Dec 21, 2009
Posts: 102


This is returning a JPA managed object right? A Field entity right?
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Indeed it is. This is much less 'native' than I was supposing.

After this query, are the field objects bound to the Hibernate Session? I'm actually not positive about that, but I would imagine they are. If they are, then they'll make it into the cache.

So, I now have two opinions about this scenario.

-Cameron McKenzie
Theodore David Williams
Ranch Hand

Joined: Dec 21, 2009
Posts: 102
I will try it out and let you know what I find.

As a quick aside I am trying to turn on the cache in jboss 4.2.3 and have some problems.

In my persistence.xml:


But when I deploy I get:


Looking at jboss it seems like the sever already has a cache configuration file in the deploy directory named "ejb3-entity-cache-service.xml". Should I use that one? Do i need to provide a treecache.xml file with my EAR?
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

I'm not sure myself. I think Hibernate requires that the file be named treecache.xml.

http://qaix.com/java-programming/410-215-jbosscache-re-error-in-using-treecache-from-hibernate-read.shtml

This might be worth starting a new topic. When a thread is a few days old, with multiple replies, fewer people are likely to view it. A new topic with a descriptive subject heading might catch the attention of someone who's dealt with that specific issue.

Just an idea.

Good luck!

-Cameron McKenzie
Theodore David Williams
Ranch Hand

Joined: Dec 21, 2009
Posts: 102
Ok this will not work and the answer to my first question

1. Do entities get stored in the second level cache even when you are asking for an entity by something other than its primary key?


is no you will only hit the cache if you ask for an entity by its id

I found this Hibernate FAQ


I have enabled the second-level cache, but queries still hit the database!

The second-level cache is used for lookup by identifier. If you want to cache query result sets, you must use the query cache. See the documentation for more information.


I will see what it takes to turn on/use caching for query result sets.

Thanks for all the help
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

I edited and corrected my first response to make myself look like I knew what I was talking about.


Theodore David Williams
Ranch Hand

Joined: Dec 21, 2009
Posts: 102
Hehe no worries Cameron. I just like to reply with findings if they differ from previous conversation just so that someone is not lead astray if they are reading the post.

I do appreciate the help!!! I got a lot more accomplished by conversing about the subject.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Hibernate second level cache questions