• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JPA Optimistic lock on Web environmnet - does not work?

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I used to work for a company who had own ORM solution, and used Optimistic Lock to handle concurrent updates on Java EE applications, and it worked fine.

JPA has out of the box solution with @Version. Which I can not get to work. I have googled a bit, couldn't find detailed explanation or proper sample to confirm so. I don't see people complaining about it, so I guess it works.

Environment JPA, EJB3, Jboss 5.1, Mysql 5.0.22, <transaction-isolation>TRANSACTION_READ_COMMITTED</transaction-isolation>, Struts 2

I have object with @Version property long, which gets auto incresed when record is saved.
I do simple thing:
1)Open the same edit page in two browsers. Version field is persisted on web layer
2)Save changed data. Version gets updated.
3)On another browser I save the record again. Data is staled, version number is less the current. I track in debugger and I see stale data with version number less then current get merged with no error.


Shouldn't OptimisticLockException raise?

Is it expected behavior? Or this will work only with attached objects? If so, how to accomplish optimistic lock on Web app?

I have spent more time than intended on google, and find number of sites with same ~two sentences about, "just put @Version and all will work fine" but couldn't find site o book describing it with more details or real life like sample.

I would welcome any link or book title which covers the the topic I have issues with.





 
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The issue is probably with how you are merging/updating the objects, perhaps include this code. Ensure that the object you are committing has the old version from the webpage.

Also include the SQL the occurs and your JPA provider.

See also,
http://en.wikibooks.org/wiki/Java_Persistence/Locking
 
Dejan Mratinkovic
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the link James, it is a good link, I have been using Java Persistence from wikibooks as reference myself.

My code comes down to this (object is filled with data from Struts 2 forms, so it is plain pojo populated with data from screen), and is very simple:



Bottom line, I never get OptimisticLockException.

This code is witin transaction of stateless Ejb

The log I get is:



Code is to simple to be wrong, but, yet, I am missing something.
 
Dejan Mratinkovic
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Issue resolved.

Code is to simple to be wrong, but, yet, I am missing something.



Of course it is. Issue resolved.


Just in case someone else runs to it:
At one point, before the merge method, object fetched from db got version field updated with version from client (stale). Then, JPA on merge compared "live" object version which matched the "client" version and no optimistic lock exception was raised.

These were some leftovers from previous tests, as original code updated some, but not all fields. So, live object was fetched from db and fields were updated (version among them). But, if that is so, exception will not fire.

I would expect lock exception to fire here also, but I guess I was wrong.



 
reply
    Bookmark Topic Watch Topic
  • New Topic