File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB and other Java EE Technologies and the fly likes How to Achive Transaction Concurrency in EJB 3 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "How to Achive Transaction Concurrency in EJB 3 " Watch "How to Achive Transaction Concurrency in EJB 3 " New topic

How to Achive Transaction Concurrency in EJB 3

udaya ranasinghe

Joined: Dec 10, 2009
Posts: 1
I want to handle transaction concurrency in my project.
I read about the @Version but i couldn't understand the concept.
Can anybody give example code and explanation for that?

Devaka Cooray
ExamLab Creator

Joined: Jul 29, 2008
Posts: 3911

Hello Udaya Welcome to JavaRanch.

That's the optimistic locking feature which can be used to prevent inappropriate usage of concurrent updates over an entity in the database. Simply you can use this @Version annotation to annotate a property of a primary entity.

Now, the version property manages a version value for the given entity in the database. It gets assigned a new value at each commit (to the database). In a concurrent scenario, let's say that the attributes in the same entity gets concurrently modified. Then, the first commit will be successful, because the version value in the entity instance, and which was in the database are identical to each other. This value will be changed once it is committed. After that, the second concurrent modification will not be allowed to commit - because the version value which IS in the database is different than the value in the entity instance itself. On that case OptimisticLockException will be thrown when you attempt to commit (or flush) the entity instance which has an incorrect version value. If there is an active transaction, that will be (marked) rolled-back so that it prevents the concurrent problem.


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Rob Spoor

Joined: Oct 27, 2005
Posts: 20269

Moving to EJB.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: How to Achive Transaction Concurrency in EJB 3
It's not a secret anymore!