-->
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.  [ 4 posts ] 
Author Message
 Post subject: JUnit testing Optimistic Lock
PostPosted: Mon Mar 05, 2012 7:49 pm 
Beginner
Beginner

Joined: Thu Jun 14, 2007 4:33 am
Posts: 39
Hi, i've red quite a bit now about how to test hibernates optimistic locking in a junit test.

what i need the optimistic locking for is for generating sequence-numbers for different configurations of an entity (configuration would be propertyA=x, and propertyB=y where x and y can have infinite different values). all entries for each configuration need to be consecutively numbered. i hope you kind of get the point, dont know how to explain this.. what i did, is creating a table, where for each configuration i store the last sequence-number. for retrieving a new one, i load the old one, increment and save it back. hibernate-should assure i will be notified if between loading and saving any updates to my data-row will be made.
now i saw some solutions where multiple sessions are opened and closed, but no parallel sessions where used. i wonder why my approach doesnt seam to work. what i do is:

[*] start a thread where i open a new session: FactoryManager.getSessionFactory().openSession();
[*] load a record from my table
[*] pause the thread
[*] outside the thread i again start a new transaction FactoryManager.getSessionFactory().openSession();
[*] load a record from my table
[*] change something
[*] update, commit, flush, close session
[*] resume thread
[*] in the thread i change my record
[*] again update, commit, flush, close

i expected an exception since i updated the object outside my current session. however hibernate just executes the 2 updates, so practically the second update gets lost since it just overwrites the first update which results in the same version-number ans sequence-number.
does hibernate not check for optimistic locking if i dont close the current session?
if so, i really cant close the current session between loading and updating the entity. so am i wrong this could be a problem in a multi-user environment? any suggestions?


Top
 Profile  
 
 Post subject: Re: JUnit testing Optimistic Lock
PostPosted: Tue Mar 06, 2012 5:51 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Hiberante does check for optimistic locking when you declare an appropriate version column:

Quote:
@Entity
public class YourClass {
...
@Version
private int version;
...
}


Did you remember to do that?


Top
 Profile  
 
 Post subject: Re: JUnit testing Optimistic Lock
PostPosted: Tue Mar 06, 2012 6:35 am 
Beginner
Beginner

Joined: Thu Jun 14, 2007 4:33 am
Posts: 39
hi. yes, sure i did. only difference is the @column annotation:
Code:
@Version
@Column(name="OPTLOCK")
public Integer getVersion() {
   return this.version;
}
   
public void setVersion(Integer version) {
   this.version = version;
}

hibernate does increment the value on every update, it just doesnt throw an exception, but seams to ignore the update. probably there is a misunderstanding of the optimistic locking mechanism on my side. maybe someone could reply to my earlier question:
does hibernate check for optimistic locking if i dont close the current session between loading and updating the datarow?


Top
 Profile  
 
 Post subject: Re: JUnit testing Optimistic Lock
PostPosted: Tue Mar 06, 2012 7:34 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
does hibernate check for optimistic locking if i dont close the current session between loading and updating the datarow?


Hibernate always does check for optimistic locking, regardleess if you close session or not.
Can you please log all the jdbc-activity with p6spy.
Then we will se how the updates do the optimistic locking check ...


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