-->
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.  [ 6 posts ] 
Author Message
 Post subject: unexpected update at tx.commit() w/ optimistic locking
PostPosted: Tue Jun 28, 2005 3:23 pm 
Beginner
Beginner

Joined: Mon Jan 24, 2005 11:33 am
Posts: 24
Location: Stuttgart, Germany
Hibernate version: 3.0.5

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

Transaction tx = session.beginTransaction();
List fixes = session.createCriteria(Fix.class).list();
tx.commit();


Name and version of the database you are using:
Apache Derby, 10.0.2.0

The generated SQL (show_sql=true):

Hibernate: select this_.FIX_ID as FIX1_0_, ... from FIX this_
Hibernate: update FIX set VERSION=?, ... where FIX_ID=? and VERSION=?
Hibernate: update FIX set VERSION=?, ... where FIX_ID=? and VERSION=?
Hibernate: update FIX set VERSION=?, ... where FIX_ID=? and VERSION=?
Hibernate: update FIX set VERSION=?, ... where FIX_ID=? and VERSION=?
Hibernate: update FIX set VERSION=?, ... where FIX_ID=? and VERSION=?



In my application, there is class Fix, that has field:
private int version; // for optimistic locking

When executing the code shown above, Hibernate will issue an "update" when the transaction is committed, which increases the version counter (see the generated SQL, as shown above - there are 5 objects returned by the select).

Now my question is: why does Hibernate issue the update, even when there was no session.update()?
I found this behaviour at all isolation levels (1, 2, 4).

This might result in one of two concurrent readers (both executing the same above code) receiving a StaleObjectStateException at Transaction.commit().

I've read the book and browsed the forum, but couldn't find an aswer to that question. Mabe I'm overlooking s.th.?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 16, 2005 5:03 pm 
Newbie

Joined: Thu Mar 31, 2005 5:01 pm
Posts: 16
I'm seeing the same behaviour. Anyone know what's going on here?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 19, 2005 7:38 am 
Newbie

Joined: Sun Sep 18, 2005 11:38 am
Posts: 1
I had experienced the same problem
hibernate was updating the version of a certain table when i was doing session.find() , don't know what exactly was causing it, but i have managed to solve it with session.clear() before find(). Hope it'll help and if somebody knows what is causing such behavior please do tell.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 19, 2005 9:30 am 
Newbie

Joined: Thu Mar 31, 2005 5:01 pm
Posts: 16
BTW, to be more specific, it's the flush that triggers the update. (The original poster reported that it was the commit, which is true because it calls flush.)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 19, 2005 9:53 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
hibernate issues SQL UPDATES at flush time because it thinks that domain entity is dirty.

find out why it thinks the thing is dirty. typically, if unexpected, this is due to bad equals() implementations.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 19, 2005 11:27 am 
Newbie

Joined: Thu Mar 31, 2005 5:01 pm
Posts: 16
I found the cause of my problem.

When I added optimistic locking, I copied and pasted my @Id methods because they are pretty similar to how I wanted my @Version methods to look.

Unfortunately I left too much of the @Id accessor in the @Version accessor. My getVersion was returning the object's key! This is what caused the update to trigger.

I'm sure Steve's explanation is right for most people, but I just thought I'd add another avenue for anyone searching the forum archives. (And, of course, confession is good for the soul.)


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