File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes JPA 1.0: entity locking Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "JPA 1.0: entity locking" Watch "JPA 1.0: entity locking" New topic

JPA 1.0: entity locking

Vincenzo Turco

Joined: May 15, 2009
Posts: 12
Hi developers, I need some help with JPA entities locking.
As far as I know, locking with JPA lasts as long as the persistence context in which it is invoked.
I.e. when the persistence context ends, all entities become "detached" and all locks are released.

I am accessing entities with Stateless EJBs and without using native SQL (Oracle db is used)
I would like to lock entities even while data is displayed to users in UI components (such as JSP, servlets, ...) i.e. after peristence context end.

Right now we use Optimistic locking, but this way users are warned of concurrence errors only when they try to save documents, which is unacceptable.

Should I use stateful EJBs and extended persistence contexts?
Can anyone please shed some light?

Thanks, regards

SCJP6 (90%), SCDJWS (74%), SCBCD (83%), SCEA next? ;)
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
Optimistic locking is normally recommended for web applications.
Leaving locks on your database while waiting for a web application that may disappear or go to lunch is not normally a good idea.
Since pessimistic locking uses databases locks, you need to keep the database transaction open. This does not really fit with the EJB model very well, as a SessionBean normally starts and ends a JTA transaction around the method call. You will need to manage your own transactions if you wish to keep them open across requests.

If optimistic locking is really not usable for you, you may also consider application level pessimistic locking. i.e. have a LOCKED column in your table and update this when the user wants a lock, and update it again to release the lock. It will be the application responsibility to check this lock field.


TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
I agree. Here's the link:
subject: JPA 1.0: entity locking
It's not a secret anymore!