Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to Achive Transaction Concurrency in EJB 3  RSS feed

 
udaya ranasinghe
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 5511
687
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Devaka.
 
Rob Spoor
Sheriff
Posts: 21047
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to EJB.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!