-->
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: Want help regarding optemistic locking using version
PostPosted: Mon Sep 15, 2008 1:14 am 
Newbie

Joined: Tue Aug 12, 2008 7:08 am
Posts: 8
Hi All,

I am new to hibernate Ineed some help regarding Optimistic locking using version.

I have implemented this in one of my project ,What I want is I need to check this implementaion wether it is working correctly or not in my project.

I want to know the procedure to check this and how to get that stale object exception. i.e. I wan a know the testing procedure.

I am looking forward for the help.

Thanks in advance.......


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 15, 2008 5:09 am 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
Optimistic locking is done using the version or timestamp property.
This creates an additional field in your table to hold the (respectively) current version or timestamp of the entity.

When persisting versioned entities, Hibernate issues a "update... where id=? and version=?" where the version is the one the entity had when first loaded.

If the entity is modified by another thread between the time the first thread loads and saves it, then the where clause shown above will not match it anymore (because the version field is now different) - hence causing the OptimisticLockException.

If you want to test that behavior, the easiest is to put a breakpoint in your application. When the execution is halted, change the data in your database (increase the version number by one), then release the thread...

_________________
If you found my post helpful, please rate it! Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 15, 2008 5:36 am 
Newbie

Joined: Tue Aug 12, 2008 7:08 am
Posts: 8
Hello bertrand,

Thank you for ur sugession on my query . The solution u gave for testing is very good and also easy but I need to write a test case for that is it possilble if so plz tell me what to do

I need to check staleobjectexception


Last edited by kpr284 on Mon Sep 15, 2008 5:42 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 15, 2008 5:42 am 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
kpr284 wrote:
Hello bertrand,

Thank you for ur sugession on my query . The solution u gave for testing is very good and also easy but I need to write a test case for that is it possilble if so plz tell me what to do


What do wanna test? The fact that Hibernates throws the OptimisticLock exception in such case? If so, then you are writing a test case against Hibernate... which is not your job - you should rather test your application...

If you wanna test how your application behaves in such circumstances, then simply think about what will cause it to happen, and find a way to reproduce it in your test case!

Some hint:
- two threads accessing and modifying the data;
- to Hibernate sessions - one per thread of course;
- synchronization between the two so the first commits after the second;

_________________
If you found my post helpful, please rate it! Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 15, 2008 5:58 am 
Newbie

Joined: Tue Aug 12, 2008 7:08 am
Posts: 8
bertrand,

yes thats true I got your point ...Once again thanks for ur help...


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.