-->
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: OptimisticLockException after a ConstraintViolationException
PostPosted: Fri Apr 04, 2008 11:06 am 
Newbie

Joined: Thu May 05, 2005 5:54 am
Posts: 17
First of all excuse me for the cryptic subject but i cannot find nothing better. The scenario is this:

create new entity A with field X=0
begin transaction
merge(A)
commit transaction
a RollbackException is thrown beacause a unique on field X is violated.
I handle the exception and increase the value of X then i retry to merge
begin transaction
merge(A)
commit transaction

at the second merge i get the OptimisticLockException. This code works very well without the @Version annotation. Now that i added it it does not work anymore.
Can someone give me advice on how to modify the code in order to avoid the OptimisticLockException?

The try and error behaviour is used to handle the concurrency in a safe way, maybe it's not the most efficient way but it works and collisions are very infrequent so there is no need to optimize it. So, possibly, i want to mantain that logic.

Thank you


Hibernate version:
core 3.2.6
annotations 3.3.3
entity manager 3.3.1

Name and version of the database you are using:
postgres 8.2

java 6


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 05, 2008 8:38 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

Quote:
I handle the exception and increase the value of X then i retry to merge


How exactly do you handle the exception. On which object do you increase the value of X? Or do you create a new instance of A with the new value X? The latter should work.

In case you try to modify the originally created instance I could imagine that the version property causes some grief. I image that the version gets incremented on the first commit attempt. When you then set a new value of X it might get incremented again and the following commit will fail due to a OptimisticLockException. Well, that's just a guess, but maybe you can debug your application and check the values of the version property. Also turn on sql output and you can see which sql triggers the exception.

--Hardy


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 07, 2008 6:32 am 
Newbie

Joined: Thu May 05, 2005 5:54 am
Posts: 17
Hello,

i create a new entity, i assign the id (java uuid), version is null, unique field is 0

begin transaction
merge (translates to an insert)
fail bencause 0 already exist

on the *same* entity i increase the unique field to 1
id is the same, version is still null
begin transaction
merge (again translates to an insert)
fail because OptimisticLockException

the last sql operation logged is the insert in the incriminated table
the OptimistickLockEcxeption is caused by:
org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [mypackage.MyEntity#e84af2a9-ca23-40f1-9694-0ebb72894b53]

so i think that the cause of the Lock failure is on that entity.
That entity also contains references to other versioned entities that already exist in the database (so version number is != 0) but i do not modify them and i am the only one that is working on the db now. I only change the unique field and retry to merge (and this translate to an insert)

Thank you


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 10, 2008 5:12 am 
Newbie

Joined: Thu May 05, 2005 5:54 am
Posts: 17
BUMP.

should i assume that a new entity that faild to merge in a transaction cannot be modified and merged again in another transaction when optimistic locking is enabled?

Since there is no record in the database for that entity, it makes no sense to me the OptimistcLockException.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 10, 2008 6:17 am 
Newbie

Joined: Thu May 05, 2005 5:54 am
Posts: 17
ok, i made a spimpler test case and i've found where the problem exactly is. semi pseudo code follows:
Code:
class A {
   @id //generated in the contructor
   UUID id;
   @version
   Long version;
  @column(unique=true)
   Long uniqueField;
  @ManyToOne
   B connectedEntity;
}

class B {
@Id //generated in the contructor
UUID id;
@Version
Long version;
}

a = new A();
//B with id 2 is in the database, version=0
a.setConnectedEntity(find(B.class,2));
//a record with unique 0 already exists in the database
a.setUniqueField(0);

try {
   transaction.begin();
   merge(a);
   transaction.commit();
} catch(exception e) {
  //Exception thrown because of the unique violation
  transaction.begin();
  a.setUniqueField(a.getUiniqueField() + 1));
  merge(a);
  transaction.commit();
}


the second merge causes the OptimisticLockException only if i set the connected entity (that is versioned and already in the db but not modified in current transactions...)

I hope this clarify the whole thing to whom can explain me how to avoid the Exception.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 10, 2008 7:08 am 
Newbie

Joined: Thu May 05, 2005 5:54 am
Posts: 17
solved by using different EntityManagers.

so instead of:
create entitymanager
begin trastaction
do stuf with entity X
commit
begin trasaction
do again stuff with entity X loaded in previous transaction
commit
close entitymanager

that was working without versioning.

do like this:
create entitymanager
begin transaction
do stuff with entity X
commit
close entitymanager
create entity manager
begin transaction
do stuff with entity X loaded in previous transaction
commit
close entitymanager


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.