• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

how to avoid OptimisticLockException

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We are using EJB3 and version locking. I am developing an engine which takes near about 3 sec to complete. In the mean time it fire multiple named queries. Normally it works fine. But when it is processing one request and in the meantime second request starts then it gives OptimisticLockException.

I analyzed and found that I start a new transaction when engine starts and then engine do process and in the end (after 3 sec) I close the transaction. So let I started a transaction and read data from a table. then in the mean time second request comes and it again tries to read the same data then since previous transaction has not ended yet, it is throwing OptimisticLockException.

Is there any solution to avoid that exception, I can't remvoe the version lock from the table since it is being used other places also.

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should not avoid this exception, see below from MZ notes
--------------------------------------------------------------------------

Optimistic locking is a technique that is used to insure that updates to the database data
corresponding to the state of an entity are made only when NO intervening transaction
has updated that data for the entity state since the entity state was read. This insures that
updates or deletes to that data are consistent with the current state of the database and
that intervening updates are NOT LOST. Transactions that would cause this constraint to
be violated result in an OptimisticLockException being thrown and transaction ROLLBACK.
Portable applications that wish to enable optimistic locking for entities must specify @Version
attributes for those entities - i.e., persistent properties or fields annotated with the @Version
annotation or specified in the XML descriptor as version attributes. Applications are strongly
encouraged to enable optimistic locking for all entities that may be concurrently accessed or
merged from a disconnected state. Failure to use optimistic locking may lead to inconsistent
entity state, lost updates and other state irregularities. If optimistic locking is not defined as part
of the entity state, the application must bear the burden of maintaining data consistency.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic