wood burning stoves 2.0*
The moose likes JDBC and the fly likes Optimistic Or Pessimistic locking . Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Optimistic Or Pessimistic locking ." Watch "Optimistic Or Pessimistic locking ." New topic
Author

Optimistic Or Pessimistic locking .

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234


What changes and where to do the changes to be made to have my database support Optimistic Locking .

And also what is the default locking mechanism or Oracle DataBase ?(The context of locking means whether it it Optimistic Or Pessimistic)??

Please help


Save India From Corruption - Anna Hazare.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


What changes and where to do the changes to be made to have my database support Optimistic Locking .

You'll need to incloude a versioning field in your tables.


And also what is the default locking mechanism or Oracle DataBase ?(The context of locking means whether it it Optimistic Or Pessimistic)?? [

Neither.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

You'll need to include a versioning field in your tables.


Thanks paul ,

Do you mean i should add a Timestamp column in database ?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

You could do, though timestamps have the problem that they can clash. Better using a different data type.
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Paul , as you indicated i will add a different data type , but how the answer is related to my question .

How can i achieve this ?

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Is my question so foolish ??
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2497
    
    8

Ravi Pavan wrote:
What changes and where to do the changes to be made to have my database support Optimistic Locking .


We can only suggest oracle to lock a record, we can't enforce it.

What steps are needed?

In one transaction,
You have to reserve the record by using select .... from <yourtable> where <condition on a field with a unique index - preferably on the primary key> FOR UPDATE ;
The FOR UPDATE clause will reserve the record for you. However, Oracle may decide to apply a page or table lock.
But when you use the primary key as where clause, it will tipically apply a row lock.


You have now pessimistically locked, and the mlock will disappear when you end your transaction by committing or rollbacking.

More can be found by googling for Oracle locking strategy.

Is my question so foolish ??
Come on, you have had several replies on the same day as you posted the question. That is better than the response time of some Platinum service level agreements.





OCUP UML fundamental and ITIL foundation
youtube channel
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Jan Thank you

We can only suggest oracle to lock a record, we can't enforce it.

You have now pessimistically locked, and the mlock will disappear when you end your transaction by committing or rollbacking.




From these two statements , it indicates that Oracle follows a default of pessimistic locking mechanism ?

Am i correct ?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

No. Re-read Jan's post.
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2497
    
    8

From these two statements , it indicates that Oracle follows a default of pessimistic locking mechanism ?
Am i correct ?
No. Oracle uses optimistic locking.
Only when explicitely asking for a pessimistic lock (see the ...FOR UPDATE... clause in my example), it will switch to pessimistic.

How did you derive your conclusion from these two statements? In their context, they suggest the opposite.
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234


Yes paul thanks for making a point .

But please tell me how page or table lock and a row lock are related to opitimistic or a pessimistic lock ?
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Oracle uses optimistic locking.
Only when explicitely asking for a pessimistic lock (see the ...FOR UPDATE... clause in my example), it will switch to pessimistic.


Thanks Jan and Paul . I got my answer. Have a great time .
 
Don't get me started about those stupid light bulbs.
 
subject: Optimistic Or Pessimistic locking .