If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > pessimistic vs optimistic concurrency control

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-31-04, 06:37
mrmonkeyboy mrmonkeyboy is offline
Registered User
 
Join Date: Mar 2004
Location: Sweden
Posts: 3
Smile pessimistic vs optimistic concurrency control

Could someone explain to me what the difference between pessimistic (for examle 2PL) and optimistic concurrency control is?
Reply With Quote
  #2 (permalink)  
Old 03-31-04, 14:34
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Re: pessimistic vs optimistic concurrency control

I have never heard of "2PL" before, but anyway:

Pessimistic means you actually lock the data when you select it to make sure nobody else can update it before you do. For example in Oracle you would SELECT ... FROM ... WHERE ... FOR UPDATE;

Optimistic means that you do not lock the data when you select it, but when you subsequently update it you check that it has not been updated by someone else in the meanwhile, otherwise your update fails. There are various ways to do this - record version numbers, last_update timestamps, or just check all the data values like this:

UPDATE ...
SET val1 = :new_val1, val2 = :new_val2, ...
WHERE key = :key
and val1 = :old_val1, val2 = :old_val2, ...;

(:old_val1, :new_val1 are variables holding the selected and modified values for column val1).

Pessimistic locking requires that a database session is maintained between the select and the update. In web-based applications, no database connection is maintained and so optimistic locking must be used.
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #3 (permalink)  
Old 03-31-04, 19:26
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
I suspect that mrmonkeyboy meant 2PC, a common abbreviation for two phase commit. I can see a slight resemblance between pessimistic locking and two phase commit... Both of them are intensely statefull.

-PatP
Reply With Quote
  #4 (permalink)  
Old 04-01-04, 05:27
mrmonkeyboy mrmonkeyboy is offline
Registered User
 
Join Date: Mar 2004
Location: Sweden
Posts: 3
Ok! Thanks a lot for the info!

-Mr Monkeyboy
Reply With Quote
  #5 (permalink)  
Old 05-31-04, 10:56
jpt6366 jpt6366 is offline
Registered User
 
Join Date: May 2004
Location: Northern Ontario
Posts: 1
Smile

FYI: Two Phase Locking Protocol ( 2PL )

"In a given transaction, all locks precede all unlocks, i.e. once a transaction has released a lock it cannot acquire any more locks.

2PL guarantees serializability for all transactions that go to conclusion"
Reply With Quote
  #6 (permalink)  
Old 06-01-04, 08:08
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Thanks, I hadn't come across that term before.
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On