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 > Database Server Software > DB2 > How to lock records

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-12-08, 07:36
iskander iskander is offline
Registered User
 
Join Date: Sep 2007
Posts: 50
How to lock records

How can I achieve a pessimistic lock in a DB2 sql stored procedure?
Can I start a transaction with serializable isolation level?
how?

Thank you,
Dan
Reply With Quote
  #2 (permalink)  
Old 03-12-08, 08:41
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
If you want a pessimistic lock on data that you will eventually update, use the FOR UPDATE clause on the select.
I am not sure what you mean by serializable, but you can use the "WITH RR USE AND KEEP EXCLUSIVE LOCKS". For further info:

http://publib.boulder.ibm.com/infoce...n/r0000879.htm

Andy
Reply With Quote
  #3 (permalink)  
Old 03-12-08, 09:20
iskander iskander is offline
Registered User
 
Join Date: Sep 2007
Posts: 50
Thanks Andy.
Just a question: how can I span the lock over multiple commands (for example in a stored proc that executes a few queries and calls some other stored procs) ?
Reply With Quote
  #4 (permalink)  
Old 03-12-08, 09:25
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Locks will stay until either a COMMIT or ROLLBACK.

Andy
Reply With Quote
  #5 (permalink)  
Old 03-12-08, 09:39
iskander iskander is offline
Registered User
 
Join Date: Sep 2007
Posts: 50
Cheers mate!
Reply With Quote
  #6 (permalink)  
Old 03-12-08, 10:09
iskander iskander is offline
Registered User
 
Join Date: Sep 2007
Posts: 50
can you help me with this one as well please:

what if instead of lock for update I want to lock the whole table for an insert?

Something like:

declare seqNo INT;
select sequence_number into seqNo
from myTable
order by sequence_number desc
fetch first 1 row only;
set seqNo = seqNo + 1;
...do more stuff here ...
insert into myTable (sequence_number) VALUES (seqNo);


but with a lock around it so that I don't get concurrency issues?
Reply With Quote
  #7 (permalink)  
Old 03-12-08, 11:00
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
Try this:

select * from final table (insert into myTable (sequence_number) VALUES ((select max(sequence_number) + 1 from myTable));
commit;
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #8 (permalink)  
Old 03-12-08, 11:30
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Quote:
Originally Posted by iskander
can you help me with this one as well please:

what if instead of lock for update I want to lock the whole table for an insert?

Something like:

declare seqNo INT;
select sequence_number into seqNo
from myTable
order by sequence_number desc
fetch first 1 row only;
set seqNo = seqNo + 1;
...do more stuff here ...
insert into myTable (sequence_number) VALUES (seqNo);


but with a lock around it so that I don't get concurrency issues?
This might work also:

declare cursor1 cursor with hold for select max(sequence_number) from myTable for update;
declare seqNo INT;

open cursor1;
fetch cursor1 into seqNo;

set seqNo = seqNo + 1;
...do more stuff here ...
insert into myTable (sequence_number) VALUES (seqNo);

close cursor1;

Andy
Reply With Quote
  #9 (permalink)  
Old 03-13-08, 05:48
iskander iskander is offline
Registered User
 
Join Date: Sep 2007
Posts: 50
Thanks guys, will try
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