(Newbie)Optimistic Locking for Web Apps

View: New views
4 Messages — Rating Filter:   Alert me  

(Newbie)Optimistic Locking for Web Apps

by RogerV :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi

Could someone explain or point to a tutorial on how to implement optimistic locking correctly in a web-application. I've added the version column and annotation to my entity and I can see the version number incrementing in the database every time I perform an update. I've read the article at http://java.dzone.com/articles/dont-break-optimistic-locking but I can still overwrite changes.

The logic goes basically - Client A wants to update an entity. The web-app opens the entity manager, gets the entity, closes the entity manager so the entities are now detached and displays the entity to Client A in an HTML form. When client A submits the form, the web-app creates a new instance of the entity, opens the entity manager, merges the new entity and closes the entity manager again. At this point I see the version number increment in the database. However, if while Client A is thinking about changing the entity, client B selects the same entity, displays and submits his changes, they're being overwritten when client A submits their changes - I'm not getting an OptimisticLockingException being thrown and I see the version number increment by 2.

Since the entity manager is closed between getting and updating the entity, is it even possible to expect EclipseLink to deal with this?

Re: (Newbie)Optimistic Locking for Web Apps

by RogerV :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


RogerV wrote:
Hi

Could someone explain or point to a tutorial on how to implement optimistic locking correctly in a web-application. I've added the version column and annotation to my entity and I can see the version number incrementing in the database every time I perform an update. I've read the article at http://java.dzone.com/articles/dont-break-optimistic-locking but I can still overwrite changes.
Please accept my apologies - I missed a crucial step from the example!

Regards

Re: (Newbie)Optimistic Locking for Web Apps

by tch :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Just for future people, why don't you let us know what you missed?

./tch



On Thu, Sep 10, 2009 at 8:16 AM, RogerV <roger.varley@...> wrote:

>
>
>
> RogerV wrote:
>>
>> Hi
>>
>> Could someone explain or point to a tutorial on how to implement
>> optimistic locking correctly in a web-application. I've added the version
>> column and annotation to my entity and I can see the version number
>> incrementing in the database every time I perform an update. I've read the
>> article at http://java.dzone.com/articles/dont-break-optimistic-locking
>> but I can still overwrite changes.
>>
>
> Please accept my apologies - I missed a crucial step from the example!
>
> Regards
> --
> View this message in context: http://www.nabble.com/%28Newbie%29Optimistic-Locking-for-Web-Apps-tp25381983p25382223.html
> Sent from the EclipseLink - Users mailing list archive at Nabble.com.
>
> _______________________________________________
> eclipselink-users mailing list
> eclipselink-users@...
> https://dev.eclipse.org/mailman/listinfo/eclipselink-users
>
_______________________________________________
eclipselink-users mailing list
eclipselink-users@...
https://dev.eclipse.org/mailman/listinfo/eclipselink-users

Re: (Newbie)Optimistic Locking for Web Apps

by RogerV :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


tch wrote:
Just for future people, why don't you let us know what you missed?
I'm using the Struts 2 framework which will "automagically" create a new instance of the entity for me. I ommitted to include the version number as a hidden field on the form to return to the framework, so Struts 2 just gave it the default value which is insufficient to trigger the optimistic locking. Once I added the version attribute to the html form, optimistic locking works like a charm - it's actually embarassingly easy to implement!

So in summary;

1) Add the version number field to your database table.
2) Add the version annotation to your entity.java
3) Include the version number as a hidden field in your html data entry form and return it with the update request.
4) Create a new Entity instance with the returned data (this is the bit that Struts does for you)
5) EntityManager.merge(entity)

Regards