-->
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.  [ 5 posts ] 
Author Message
 Post subject: does StaleObjectStateException necessarily mean two ops are
PostPosted: Thu Jun 01, 2006 3:06 am 
Beginner
Beginner

Joined: Thu Apr 13, 2006 12:56 pm
Posts: 23
I have a webapp that uses session-per-request strategy and executes two operations that update the same db table. At higher loads one of two requests infrequently fails with StaleObjectStateException. This behavior seems correct, but I want to make sure the exception does NOT mean that two requests are inadvertently sharing the same Session, thereby breaking session-per-request strategy. So, is the following possible?:

Given:
Long Operation A:
-load from DB
-mod persistent object
-commit

Short Operation B:
-load from DB
-mod same persistent entity
-commit:


Can the following happen given that MySQL DB is in Repeatable Read mode:

1.Session(A) starts
2.Op A reads entity as Obj1

3.Session(B) starts
4.Op B reads same entity as Obj1
5.Op B mods entity
6.Session (B) commits Obj1(b)

7.Op A mods entity Obj1 as Obj1(a)
8.Session (A) tries to commit Obj1(a) and is denied because up-to-date entity is now Obj1(b)

Is the above scenario possible? I thought once Session(A) reads entity as Obj1 the table's locked until Session(A) terminates. If not, is there any way to make Session(B) block on select until Session(A) is done?


thanks,
-nikita

Hibernate version:
3.1.3
Name and version of the database you are using:
MySQL 5.0, InnoDB, repeatable read tx isolation


Top
 Profile  
 
 Post subject: Re: does StaleObjectStateException necessarily mean two ops
PostPosted: Fri Jun 02, 2006 1:03 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
dukehoops02 wrote:
Is the above scenario possible? I thought once Session(A) reads entity as Obj1 the table's locked until Session(A) terminates. If not, is there any way to make Session(B) block on select until Session(A) is done?

The table is not locked if you've only done a read. If you did the read with LockMode.UPGRADE, which turns "select xxx from ..." into "select xxx for update from ...", then you would lock the table, and that will cause session B to block, like you want.

Unfortunately, not every DBMS suppoorts select ... for update. I don't believe that MySQL does.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject: Re: does StaleObjectStateException necessarily mean two ops
PostPosted: Fri Jun 02, 2006 12:35 pm 
Beginner
Beginner

Joined: Thu Apr 13, 2006 12:56 pm
Posts: 23
Thanks, tenwit!

So does that mean that if my app can have multiple threads touching the sam table row, I *will* see StaleObjectStateExceptions and therefore have to have logic to rollback/retry on those? in other words, there is no way to *guarantee* that that exception will never appear?

thanks
-nikita


Top
 Profile  
 
 Post subject: select ... for update in mysql
PostPosted: Fri Jun 02, 2006 12:47 pm 
Beginner
Beginner

Joined: Thu Apr 13, 2006 12:56 pm
Posts: 23
seems that mysql 5.0 does support "select for update":
http://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.html


Interestingly, the first user comment on that page seems to imply that even "select for update" will not guarantee thread safety. So seems like my app will still have to handle StaleObjectStateExceptions - except they should be appearing less frequently when "select for update" is used.

-nikita


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 05, 2006 5:54 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Well, if you have write-often objects, it's best to refresh those before writing to them. Or do the opposite: write what your current thread sees, and ignore updates that happened current thread read whatever object it's about to save. Though the former option would usually be preferable to the latter.

Judicious use of transactions will have a greater impact on avoiding stale objects than "select for update". In the ref docs, the chapters on "Transactions and Concurrency" and "Improving Performance" (11 and 19, in the 3.1 ref docs) both have things to say on this matter.

_________________
Code tags are your friend. Know them and use them.


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