• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hibernate Cache

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is is possible to automatically update the dataobjects in memory (when cached), whenever an update occurs in database by another user?

Appreciate time & help
Regards
Uma
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by uma prasath:
Is is possible to automatically update the dataobjects in memory (when cached), whenever an update occurs in database by another user?


You need to define your terms.

Recall that Hibernate is typically configured with two-level caching. The first level is session-caching, and it would be too expensive and too much of a scaling bottleneck (not to mention a synchronization nightmare) if Hibernate tried to do anything like that to session caches. Instead, typically, optimistic locking is used to catch conflicting updates. Would that work for you?

As far as the second-level cache goes, it comes in different flavors depending
on your system achitecture and which implementation you choose -- it's a plug-in. I can't comment on clustered architectures, but with the simpler application-wide caches, you can configure them to know which tables are read-only, which are read-write, and which and mainly read-only but occasionally updated. Again, to avoid scaling and synchronization problems, data may be retieved from the second-level by *value*
(in other words, a copy is returned to sessions) so once an entity is in a first-level cache, it can go out of synch if the data is updating in another session. Again, optimistic locking is the usual solution.

All this assumes that all access to the database is through your Hibernate application. If, say, there is some old smelly legacy app still grinding away, then a second-level cache may not be a good idea at all. But if you are willing for its data to be a bit out of data, if your business model can live with that, then you can configure the timeouts in your second-level cache to drop objects when they reach a certain age, so that they are constantly refreshed, over time.

As you can see, there's a lot to this. I suggest you read up on it. There are several good books on Hibernate, for example Hibernate In Action (just a plug from a satisfied customer) or the reference manual at Hibernate's web site.
[ February 03, 2006: Message edited by: Jeff Albertson ]
 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by uma prasath:
Is is possible to automatically update the dataobjects in memory (when cached), whenever an update occurs in database by another user?



Ok, I'm reading into Hibernate in Action and there is a topic of pessimistic Locking

From Hibernate in Action

■ LockMode.UPDGRADE�Bypass both levels of the cache, do a version check
(if applicable), and obtain a database-level pessimistic upgrade lock, if
that is supported.
■ LockMode.UPDGRADE_NOWAIT�The same as UPGRADE, but use a SELECT...FOR
UPDATE NOWAIT on Oracle. This disables waiting for concurrent lock releases,
thus throwing a locking exception immediately if the lock can�t be obtained.



Hope it helps. I'll read more into versioning and cache. Your question is interesting and I hadn't fully considered it.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd comment that if you are taking a first swipe at it (this is general relational database advise and not just Hibernate) avoid pessimistic locking unless you know or strongly suspect that access is very contentious. I believe the authors of Hibernate In Action took pains to bring this across, too. (Darn, wish I had my copy with me!)
 
Gerardo Tasistro
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jeff Albertson:
I believe the authors of Hibernate In Action took pains to bring this across, too.



Oh yes they do. Just about every other sentence.
 
uma prasath
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Thanks you very much for your response.
Appreciate your time and effort !!!
Regards
Uma
 
uma prasath
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi jeff,
As you said, i am in need of data synchronisation between the cached object and the database. Say a users U1,U2 searches at time T1. If user U2 updates the details in the table at time T2. Please confirm whether those changed data's will be updated to the cached object ???. If yes, when the user U1 performs a search again at time T3, did the system will fetch it from the cached objects (with updated details) ???

Thanks
Uma
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Again, you need to be careful when you refer to the "cache", since Hibernate has two cache levels, but let me try to step through your example:

T1: users U1 and U2 both load the same row. If there is a second-level cache, and it have seen configured to cache rows of this table, it will attempt to cache a copy of this row. At this moment then, there are *three* copies of this row in memory:
-- a copy in the application-wide, second-level cache
-- a copy in U1's first-level cache
-- a copy in U2's first level cache

T2: U2 updates its copy og the object and commits the changes. At this moment:
-- the copy in the application-wide, second-level cache will have been updated
-- the copy in U1's first-level cache has not been changed by this update
-- the copy in U2's first level cache is of course, as U2 updated it.

T3: U1 loads the row again. This will return the same object reference as was returned to U1 at T1 -- the same unchanged data. U1 will have to do some more work to get the latest values:
-- close its current session and open a new one.
-- clear the session to evict all data from the first-level cache
-- evict just that object from the first-level cache
-- refresh that object (usually not a good idea,)

So what should U2 do? The typical advice in a web application, or whereever sessions are short is to try optimistic locking first and see if that's good enough.
 
uma prasath
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the information jeff
Regards
Uma
 
uma prasath
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi jeff
will hibernate automatically detects data modified by other process (update by user U2)?. If i use second level cache (sessionFactory level cache), is it possible to automatically update the cached objects in user U1 session.

Thanks
Uma
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by uma prasath:

will hibernate automatically detects data modified by other process (update by user U2)?.



No. Hibernate is not notified when other processes, for example a legacy application, update the database. If the database access isn't solely through a single running process, rethink second-level caching, bcause its usefulness may be compromised.

Originally posted by uma prasath:

If i use second level cache (sessionFactory level cache), is it possible to automatically update the cached objects in user U1 session.



No, there is no automatic update. Such a thing would be impossible for reasons of thread safety and data integrity.
 
uma prasath
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi jeff
Is there any other mechanism/option to achieve this using hibernate ??? and if updates occurs through another session in hibernate, is it possible ?

Regards
Uma
[ February 06, 2006: Message edited by: uma prasath ]
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you summarize exactly what you want? Because I think it is either inpracticable or computationally infeasible. And why do you want to do it? What is the problem you are trying to solve and what is your goal?
 
When you have exhausted all possibilities, remember this: you haven't - Edison. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic