-->
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.  [ 14 posts ] 
Author Message
 Post subject: Newbie: Hibernate-1.2.5 Cache Problem
PostPosted: Sat Jul 17, 2004 6:18 am 
Beginner
Beginner

Joined: Wed Feb 04, 2004 5:41 am
Posts: 20
Hi! I've a class Product with one-to-one mapping with class Inventory with eager-loading. The inital stockqty of Inventory is 10. One program will add class Inventory.stockqty by 10 and the other program will subtract Inventory.stockqty by 3.

The first and second programs Product object, embedded inside a SSLB, could be loaded right before the operation below:

Assuming the first program run slower than the second program and the second program. The result is Hibernate performed is 20 instead of 17! I tried logging the operations and it seems that the first program's (ran the operation after the second program) Inventory's stockqty value is 10 instead of 7. It seems that the first program's stockqty had been cached? Any suggestions?

Any help is greatly appreciated.

len.


Top
 Profile  
 
 Post subject: Re: Newbie: Hibernate-1.2.5 Cache Problem
PostPosted: Sun Jul 18, 2004 12:55 am 
Senior
Senior

Joined: Sat Jul 17, 2004 5:16 pm
Posts: 143
Hibernate caches things. This is good in some circumstances, and perhaps bad in circumstances like yours. I think there are two ways to solve this:

1. You need to either circumvent the cache for this transaction, and use pessimistic locking (assuming the transaction itself (the read/update) doesnt take long... this locks the DB resources)

-or-

2. You can version your objects in the DB (e.g. with a timestamp) so when the update occurs at least an exception occurs so that you know someone else edited the data in the meantime... This is good for GUI's, but perhaps bad for daemons unless you can automatically retry the logic.

Chris


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 18, 2004 10:48 pm 
Beginner
Beginner

Joined: Wed Feb 04, 2004 5:41 am
Posts: 20
How do I circumvent the cache? It seems that I can't find any APIs to disable the cache upon performing query.

-len


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 18, 2004 11:16 pm 
Senior
Senior

Joined: Sat Jul 17, 2004 5:16 pm
Posts: 143
len wrote:
How do I circumvent the cache?


Get the object (using cache), then call Session.refresh(object); There are other ways too, though this seems easy and appropriate.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 19, 2004 3:26 am 
Beginner
Beginner

Joined: Wed Feb 04, 2004 5:41 am
Posts: 20
mchyzer wrote:
len wrote:
How do I circumvent the cache?


Get the object (using cache), then call Session.refresh(object); There are other ways too, though this seems easy and appropriate.


No refresh method found here. I'm using 1.2.5 with eager-loading. Is eager-loading the problem? My database server is using repeatble read.

-len


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 19, 2004 3:52 am 
Beginner
Beginner

Joined: Wed Feb 04, 2004 5:41 am
Posts: 20
I'ved modified my stockqty addition and subtraction, moved them to a dao method with LockMode.UPRADE to perform the before the operation. It still uses the old value.

-len


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 19, 2004 5:50 am 
Expert
Expert

Joined: Fri Feb 06, 2004 7:49 am
Posts: 255
Location: Moscow, Russia
len wrote:
How do I circumvent the cache? It seems that I can't find any APIs to disable the cache upon performing query.


SessionFactory
Code:
void    evict(Class persistentClass)
          Evict all entries from the second-level cache.
void    evict(Class persistentClass, Serializable id)
          Evict an entry from the second-level cache.
void    evictCollection(String roleName)
          Evict all entries from the second-level cache.
void    evictCollection(String roleName, Serializable id)
          Evict an entry from the second-level cache.
void    evictQueries()
          Evict any query result sets cached in the default query cache region.
void    evictQueries(String cacheRegion)
          Evict any query result sets cached in the named query cache region.


But in your place I would disable second-level cache or would use optimististic version locking.

--
Leonid


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 19, 2004 8:21 am 
Beginner
Beginner

Joined: Wed Feb 04, 2004 5:41 am
Posts: 20
I tried to turn my database server to serialization isolation and configured my hib-1.2.5 isolation level to 8. Seems to work but I'm not sure if it will have performance impact.

-len


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 19, 2004 8:36 am 
Expert
Expert

Joined: Fri Feb 06, 2004 7:49 am
Posts: 255
Location: Moscow, Russia
len wrote:
I tried to turn my database server to serialization isolation and configured my hib-1.2.5 isolation level to 8. Seems to work but I'm not sure if it will have performance impact.


It should have perfomance impact.
In chapter 5 of Hibernate In Action, authours, for the best perfomance, recommend to use version optimistic locking (<version> is mapped) with read commited or repeatable read isolation levels:
int TRANSACTION_READ_COMMITTED = 2; // most suitable
int TRANSACTION_REPEATABLE_READ = 4;
In that chapter they describe this choice.

--
Leonid


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 19, 2004 9:18 am 
Beginner
Beginner

Joined: Wed Feb 04, 2004 5:41 am
Posts: 20
How do I disable 2nd level cache?
How do I use version optimistic locking?

I need to build a financial application with heavy updates. The inventory table is very critical in my application especially with multi-user and its running inside an ejb container with SLSB. I need to get an accurate inventory before performing any additions or substractions. I tried turning my db-server to read-committed but I'ved tried running multi-apps and my inventory didnt have accurate values.

-len


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 19, 2004 10:52 am 
Expert
Expert

Joined: Fri Feb 06, 2004 7:49 am
Posts: 255
Location: Moscow, Russia
len wrote:
How do I disable 2nd level cache?

2nd level cache is not enabled by default, do not turn on it :)
http://www.hibernate.org/hib_docs/reference/en/html/performance.html#performance-cache
If you need to clear enabled 2nd level cache, use SessionFactory.evict.

len wrote:
How do I use version optimistic locking?

http://www.hibernate.org/hib_docs/reference/en/html/mapping.html#mapping-declaration-version
http://www.hibernate.org/hib_docs/reference/en/html/transactions.html#transactions-optimistic

There are a lot of information and suggesions about 2nd level chache, versioning and suitable transaction isolation levels in chapter 5 of "Hibernate In Action".

--
Leonid


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 20, 2004 4:07 am 
Beginner
Beginner

Joined: Wed Feb 04, 2004 5:41 am
Posts: 20
1.2.5 API doesnt have evict methods.

If I use session.connection and execute update stockqty=stockqty (+/-) to prevent cached data before writing to db?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 20, 2004 4:36 am 
Expert
Expert

Joined: Fri Feb 06, 2004 7:49 am
Posts: 255
Location: Moscow, Russia
If there is a way to write some entities into database bypassing hibernate (bypassing 2nd level cache) in your application, it is strongly recommend to turn off 2nd level cache for those entities. Use 2nd level cache for immutable referenced data if you need to improve your performance. Map version property, and ensure that application that write into database bypassing hibernate increments version and checks it before update.

--
Leonid


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 20, 2004 6:13 am 
Beginner
Beginner

Joined: Wed Feb 04, 2004 5:41 am
Posts: 20
My 2nd level cache is turned off by default.


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