• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Hibernate second level cache questions

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
author and cow tipper
Posts: 5006
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5006
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This is returning a JPA managed object right? A Field entity right?
 
Cameron Wallace McKenzie
author and cow tipper
Posts: 5006
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5006
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5006
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I edited and corrected my first response to make myself look like I knew what I was talking about.


 
Theodore David Williams
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Look! It's Leonardo da Vinci! And he brought a tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic