-->
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.  [ 11 posts ] 
Author Message
 Post subject: hibernate cache whole table ?
PostPosted: Tue Aug 23, 2005 4:24 am 
Newbie

Joined: Tue Aug 23, 2005 4:18 am
Posts: 2
Hey

I have quite a number of "Types" tables in my DB, for example i have an User table and UserTypes table where User has an id or code of UserTypes, those "Types" tables are immutable through whole deployment time, and i would like hibernate to cache them, i mean cache whole table -> all access to those entities should be in-memory, wherether it's by some query, or by load or by whatever else, (<cache> in table mapping seems not to work, coz hibernate is still doing selects, and collection cache seems to be inappropriate here), some suggestions ?

thanks :)
Hin


Top
 Profile  
 
 Post subject: Re: hibernate cache whole table ?
PostPosted: Tue Aug 23, 2005 6:21 am 
Senior
Senior

Joined: Tue Aug 03, 2004 2:11 pm
Posts: 142
Location: Somerset
mn7 wrote:
Hey

I have quite a number of "Types" tables in my DB, for example i have an User table and UserTypes table where User has an id or code of UserTypes, those "Types" tables are immutable through whole deployment time, and i would like hibernate to cache them, i mean cache whole table -> all access to those entities should be in-memory, wherether it's by some query, or by load or by whatever else, (<cache> in table mapping seems not to work, coz hibernate is still doing selects, and collection cache seems to be inappropriate here), some suggestions ?

thanks :)
Hin



Turn on hibernate logging (log4j.category.org.hibernate=DEBUG)

Look at cache hit entries for ehcache assuming you are using this.

Hibernate will cache both the queries (so you don't have to prepare them again) and the results.

On the ehcache.xml file you will need to add an entry for the class you want to cache, and make the cacheing "eternal".

The ehcache documentation explains how to do this.

Bear in mind if you change the data outside of the hibernate application the cache will not get refreshed.

_________________
On the information super B road


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 23, 2005 10:15 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
You can create "cluster" if DB supports this optimization (join will be as fast as no join) you will save memory on client and your time.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 24, 2005 5:54 am 
Newbie

Joined: Tue Aug 23, 2005 4:18 am
Posts: 2
Paul: i did as you advised, but hibernate is still doing selects, well, by doing query.iterate and then iterator.next it hits the cache but in core query.iterate it's still doing "select id from table", and caching query by name doesn't work at all. I don't wan't ANY access to the database on those tables except for the first time.

baliukas: as I said -> no access to database whatsoever. :)

thanks, mn


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 24, 2005 6:51 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
Do you need performance or do not want to access database ? :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 24, 2005 6:53 am 
Senior
Senior

Joined: Tue Aug 03, 2004 2:11 pm
Posts: 142
Location: Somerset
mn7 wrote:
Paul: i did as you advised, but hibernate is still doing selects, well, by doing query.iterate and then iterator.next it hits the cache but in core query.iterate it's still doing "select id from table", and caching query by name doesn't work at all. I don't wan't ANY access to the database on those tables except for the first time.

baliukas: as I said -> no access to database whatsoever. :)

thanks, mn


I don't know enough about how hiberante interacts with caches to be able to help you. There is a whole section in "Hibernate in Action" about this. YOu have bought the book haven't you ;-) ?

If they are totally immutable why not cache them in your code (i.e. make instances static and final) ?

_________________
On the information super B road


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 24, 2005 8:58 am 
Newbie

Joined: Thu Nov 25, 2004 11:44 am
Posts: 15
Location: Woodbridge UK
Have you set up the Cache provider? For example:

Code:
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider

Have you added the cache element with nonstrict-read-write (for very infrequent updating) or read-only (for never updating) in your Types class mapping file? For example

Code:
<class name="ProjectEventType" table="project_event_type" lazy = "false">
   
   <cache usage = "nonstrict-read-write"/>
   
   <id ...
   </id>

       ... properties

</class>


Have you set up your ehcache.xml with the cacheing rules you want to apply for the class?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 24, 2005 9:16 am 
Senior
Senior

Joined: Tue Aug 03, 2004 2:11 pm
Posts: 142
Location: Somerset
mn7 wrote:
Paul: i did as you advised, but hibernate is still doing selects, well, by doing query.iterate and then iterator.next it hits the cache but in core query.iterate it's still doing "select id from table", and caching query by name doesn't work at all. I don't wan't ANY access to the database on those tables except for the first time.

baliukas: as I said -> no access to database whatsoever. :)

thanks, mn


I suggest you post all the information asked in the template whenever you do a new post, particulalrly hibernate.cfg.xml, class mapping xmls, and ehcache.xml, along with whatever I've missed off below. As it stands we haven't enough information to work on (PS I hope you are generous with the credits at the end of this!)


Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

_________________
On the information super B road


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 24, 2005 4:37 pm 
Beginner
Beginner

Joined: Tue Aug 23, 2005 3:52 pm
Posts: 26
baliukas wrote:
Do you need performance or do not want to access database ? :)



If I need performance and I want cache a subset of of the table, how do I handle this?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 25, 2005 3:29 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
Leonid wrote:
baliukas wrote:
Do you need performance or do not want to access database ? :)



If I need performance and I want cache a subset of of the table, how do I handle this?

If data is stuff like labels for comboboxes, then you can export tables to property files for performance too.
There are many optimizations, but try to optimize database before to do something in application. It can take more time to tune database than to code optimization, but I think it must save time for maintainence ("readonly" requirement can become "read/write" at any time).
"Materialized" or "Indexed" Views can help to cache data too, it is possible to add this cache without any modifications in applications (you do not need to map class to this view, DB is able to rewrite query itself) it works in similar way as index. It can be used for query cache ( select from mview directly ) too.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 25, 2005 9:23 am 
Beginner
Beginner

Joined: Tue Aug 23, 2005 3:52 pm
Posts: 26
I can see how the combination of the view and the query cache can solve the problem. However if any of the data that is cached will change, query cache will be dropped. This means that next request for the data will have to go to the db to reinitialize query cache.


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