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: Optimistic Locking in a Webapp
PostPosted: Mon May 31, 2004 8:48 am 
Beginner
Beginner

Joined: Fri Jan 23, 2004 4:29 pm
Posts: 39
How should I implement optimistic locking in a webapp?
Any feedback is welcome.

I'm using the following frameworks:
    - Hibernate 2.1.2
    - Spring 1.0.1
    - Webwork 2.0
    - Velocity 1.3


Here's how I planned on doing it.

1. Set optimistic-lock to version and dynamic-update to true

Code:
<class
    optimistic-lock="version"
    dynamic-update="true"
/>


The documentation states
Quote:
If you enable dynamic-update, you will have a choice of optimistic locking strategies


2. Add the version tag to the class and create the column in the database

The class has a field called version which is used for versioning.
Code:
java.lang.Integer version


Code:
<version
        column="version"
        name="version"
        type="integer"
        unsaved-value="null"
/>


If the field is null it means we have a transient object which needs to be saved.

3. Add version and id to the HTML form

Code:
<input type="text" name="name" value="$!object.name"/>
<input type="hidden" name="id" value="$!object.id"/>
<input type="hidden" name="version" value="$!object.version"/>


I'm using Velocity so if the object is null the id and version fields would be null and
a new object would be inserted by hibernate.

4. Read version and id in WebWork2 action

When the user submits the data to the action I get the id and version
and create the object as follows.

Code:
Object object = new Object();
object.setVersion(version);
object.setId(version);
object.setName(name);


I then call:
Code:
session.saveOrUpdate(object);


saveOrUpdate would insert a new object into the database if id and version are null.
Or update the existing object if id and version are set to non-null values.

5. Hibernate takes care of the rest

Hibernate increments the version number and checks that a non stale object is being updated.

If the database contains version 15 of object and user submits data with version 0 hibernate will
detect this and throw a StaleObjectException.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 01, 2004 3:14 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Correct except for the first one:
No need for dynamic-update="true" when using optimistic-locking="version"

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 01, 2004 6:05 am 
Beginner
Beginner

Joined: Fri Jan 23, 2004 4:29 pm
Posts: 39
emmanuel wrote:
Correct except for the first one:
No need for dynamic-update="true" when using optimistic-locking="version"


Could you explain why dynamic-update is not needed when optimistic-locking="version".

In the documentation it says:

Quote:
If you enable dynamic-update, you will have a choice of optimistic locking strategies:

*

version check the version/timestamp columns
*

all check all columns
*

dirty check the changed columns
*

none do not use optimistic locking


The documentation makes it sound like you are required to have dynamic-update enabled to have optimistic locking.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 01, 2004 7:05 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
It is only required to use dynamic-update to have a choice, not to have optimistic locking at all :) You can use version/timestamp without dyanmic-update, too.


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.