Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Locking and Version
Locking and Version [message #759244] Sun, 27 November 2011 21:37 Go to next message
pop prem is currently offline pop prem
Messages: 20
Registered: June 2010
Junior Member
Hi,

I need to clarify some of my doubts related to eclipselink locking.

How can i configure eclipselink that a row level lock should be held rather than a table level? I'm using spring transactions and havn't touched any configurations related to locking in eclipselink, so it uses default locking (optimistic). Please correct me if i'm wrong here.

I have added @Version column in my entity classes to support locking mechanism. Is this a good way or do i have to use the Locking API in code too like entityManager.lock or @Lock etc? Should i use these locks in code even when i have configured database transactions through spring? I'm confused with the usage of code level locking and declarative transaction management.

Thanks.
Re: Locking and Version [message #759263 is a reply to message #759244] Mon, 28 November 2011 02:11 Go to previous messageGo to next message
Tom Eugelink is currently offline Tom Eugelink
Messages: 807
Registered: July 2009
Senior Member
Actually it is quite simple; Eclipselink per default uses optimistic locking, this means that it actually does not use any explicit locking. All that happens is that if a record is updated, the version on which this applies is specified.
For example, suppose you have a record with version 5 and Eclipse link updates it:

UPDATE record SET somefield = somevalue, version = 6 WHERE somekey = anothervalue AND version = 5;

If one record is changed (Eclipselink check for that), all is fine. But if no record is updated, someone has already done an update (or delete) and in that case Eclipselink will throw an exception.

So Eclipselink does not do any explicit locking. Naturally this all happens in a transaction and the update will place a write lock during the transaction. But wheter that is a row, page or table level locking is configured in the database.

This behavior is pretty unrelated to transaction management. Transaction management configures how long a transaction is kept open (usually only during the request).

Tom



On 2011-11-28 03:37, popprem wrote:
> Hi,
>
> I need to clarify some of my doubts related to eclipselink locking.
>
> How can i configure eclipselink that a row level lock should be held rather than a table level? I'm using spring transactions and havn't touched any configurations related to locking in eclipselink, so it uses default locking (optimistic). Please correct me if i'm wrong here.
>
> I have added @Version column in my entity classes to support locking mechanism. Is this a good way or do i have to use the Locking API in code too like entityManager.lock or @Lock etc? Should i use these locks in code even when i have configured database transactions through spring? I'm confused with the usage of code level locking and declarative transaction management.
>
> Thanks.
Re: Locking and Version [message #759280 is a reply to message #759263] Mon, 28 November 2011 04:08 Go to previous messageGo to next message
pop prem is currently offline pop prem
Messages: 20
Registered: June 2010
Junior Member

Thanks a lot for your reply Tom.

So what i understood is, Locking machanism is handled by eclipselink and it is not related to spring transactions and the table/row level locking is based on the database configurations.

Is it enough only to have a version attribute in each entities and just enable transactions through Spring and go with persist/update/delete operations? If so, in what kind of situations should we use the locking API provided by Eclipselink (code level locking like entityManager.lock() etc)??

Thanks.
Re: Locking and Version [message #759281 is a reply to message #759263] Mon, 28 November 2011 04:08 Go to previous messageGo to next message
pop prem is currently offline pop prem
Messages: 20
Registered: June 2010
Junior Member
Thanks a lot for your reply Tom.

So what i understood is, Locking machanism is handled by eclipselink and it is not related to spring transactions and the table/row level locking is based on the database configurations.

Is it enough only to have a version attribute in each entities and just enable transactions through Spring and go with persist/update/delete operations? If so, in what kind of situations should we use the locking API provided by Eclipselink (code level locking like entityManager.lock() etc)??

Thanks.
Re: Locking and Version [message #759375 is a reply to message #759244] Mon, 28 November 2011 08:48 Go to previous messageGo to next message
James is currently offline James
Messages: 272
Registered: July 2009
Senior Member
Row versus table locks are up to your database, and can only be configured on your database. You can execute a native SQL query if you really want to lock an entire table, although not normally a good idea.

See,
http://en.wikibooks.org/wiki/Java_Persistence/Locking
--
James : http://wiki.eclipse.org/EclipseLink : http://en.wikibooks.org/wiki/Java_Persistence : http://java-persistence-performance.blogspot.com/
Re: Locking and Version [message #759418 is a reply to message #759280] Mon, 28 November 2011 10:46 Go to previous messageGo to next message
Tom Eugelink is currently offline Tom Eugelink
Messages: 807
Registered: July 2009
Senior Member
> So what i understood is, Locking machanism is handled by eclipselink and it is not related to spring transactions and the table/row level locking is based on the database configurations.

Correct.

> Is it enough only to have a version attribute in each entities and just enable transactions through Spring and go with persist/update/delete operations? If so, in what kind of situations should we use the locking API provided by Eclipselink (code level locking like entityManager.lock() etc)??

Yes. And I don't know any real world scenario where the EM.lock would be used. Maybe the Eclipselink guys can give an example.
I have one scenario in which I do some outside-of-Eclipse locking, but I fall back to regular SQL statements for that.

Tom
icon14.gif  Re: Locking and Version [message #759444 is a reply to message #759418] Mon, 28 November 2011 12:17 Go to previous messageGo to next message
pop prem is currently offline pop prem
Messages: 20
Registered: June 2010
Junior Member
Tom and James, Thanks a lot for your answers, they really helped me to overcome my
confusions. Smile

Re: Locking and Version [message #759448 is a reply to message #759418] Mon, 28 November 2011 12:17 Go to previous message
pop prem is currently offline pop prem
Messages: 20
Registered: June 2010
Junior Member
Tom and James, Thanks a lot for your answers, they really helped me to overcome my
confusions. :)
Previous Topic:Locking and Version
Next Topic:has no primary key specified. It should define either an @Id, @EmbeddedId or an @IdClass
Goto Forum:
  


Current Time: Wed Aug 27 21:21:46 EDT 2014

Powered by FUDForum. Page generated in 0.01977 seconds