Unix Technical Forum

Locking issue

This is a discussion on Locking issue within the DB2 forums, part of the Database Server Software category; --> Hi! Let's say that I have a table TABLE1 in schema A and in SCHEMA B. I have a ...


Go Back   Unix Technical Forum > Database Server Software > DB2

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-27-2008, 07:31 AM
Gregor =?ISO-8859-2?Q?Kova=E8?=
 
Posts: n/a
Default Locking issue

Hi!

Let's say that I have a table TABLE1 in schema A and in SCHEMA B.
I have a stored procedure that does a select from A.TABLE1 and inserts rows
from that select in B.TABLE1.
When the procedure is running and I do a SELECT * FROM A.TABLE1 then this
statement get's locked. Why?

Any pointers to the docs I should read ?

Best regards,
Kovi
--
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
| Gregor Kovac | Gregor.Kovac@mikropis.si |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| In A World Without Fences Who Needs Gates? |
| Experience Linux. |
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-27-2008, 07:31 AM
Mark A
 
Posts: n/a
Default Re: Locking issue

"Gregor Kovac" <gregor.kovac@mikropis.si> wrote in message
news:wPNVf.1210$oj5.476162@news.siol.net...
> Hi!
>
> Let's say that I have a table TABLE1 in schema A and in SCHEMA B.
> I have a stored procedure that does a select from A.TABLE1 and inserts
> rows
> from that select in B.TABLE1.
> When the procedure is running and I do a SELECT * FROM A.TABLE1 then this
> statement get's locked. Why?
>
> Any pointers to the docs I should read ?
>
> Best regards,
> Kovi
> --


Statements do no get locked, rows or tables get locked. The only exception
is that the there is some locking going on for the package, but you should
probably ignore that unless you have some specific problem with that..

The table that is being selected will have a share lock on the rows
selected, and the table inserted will have an exclusive lock on the rows
inserted.

Share locks are released depending on the isolation level. For CS (cursor
stability), share locks will released when the cursor moves off the row to
the next row.

Exclusive locks will be released when a commit or rollback is taken.

Locking is probably discussed in the Administration:Planning and/or
Administration:Implementation manuals.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-27-2008, 07:31 AM
Serge Rielau
 
Posts: n/a
Default Re: Locking issue

Gregor KovaÄŤ wrote:
> Hi!
>
> Let's say that I have a table TABLE1 in schema A and in SCHEMA B.
> I have a stored procedure that does a select from A.TABLE1 and inserts rows
> from that select in B.TABLE1.
> When the procedure is running and I do a SELECT * FROM A.TABLE1 then this
> statement get's locked. Why?
>
> Any pointers to the docs I should read ?

I take a wild guess here and assume you use a cursor to select from
A.table1? Append FOR READ ONLY to the cursor.
The cursor is "ambiguous" in the sense that it could be updated.
Depending on your settings DB2 may either default to FOR UPDATE or FOR
READ ONLY. If it defaults to FOR UPDATE DB2 will acquire update locks.

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-27-2008, 07:31 AM
Gregor =?ISO-8859-2?Q?Kova=E8?=
 
Posts: n/a
Default Re: Locking issue

Serge Rielau wrote:

> Gregor Kovač wrote:
>> Hi!
>>
>> Let's say that I have a table TABLE1 in schema A and in SCHEMA B.
>> I have a stored procedure that does a select from A.TABLE1 and inserts
>> rows from that select in B.TABLE1.
>> When the procedure is running and I do a SELECT * FROM A.TABLE1 then this
>> statement get's locked. Why?
>>
>> Any pointers to the docs I should read ?

> I take a wild guess here and assume you use a cursor to select from
> A.table1? Append FOR READ ONLY to the cursor.
> The cursor is "ambiguous" in the sense that it could be updated.
> Depending on your settings DB2 may either default to FOR UPDATE or FOR
> READ ONLY. If it defaults to FOR UPDATE DB2 will acquire update locks.
>
> Cheers
> Serge


Well, not exactly. The procedure has couple of FOR loops, not CURSORs. The
truth is that the procedure itself is building SQL statements that are then
executed via
PREPARE S1 FROM SQLSTMT;
EXECUTE S1;

Maybe this is the problem?

Best regards,
Kovi
--
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
| Gregor Kovac | Gregor.Kovac@mikropis.si |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| In A World Without Fences Who Needs Gates? |
| Experience Linux. |
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-27-2008, 07:31 AM
Serge Rielau
 
Posts: n/a
Default Re: Locking issue

Gregor Kovač wrote:
> Serge Rielau wrote:
>
>> Gregor Kovač wrote:
>>> Hi!
>>>
>>> Let's say that I have a table TABLE1 in schema A and in SCHEMA B.
>>> I have a stored procedure that does a select from A.TABLE1 and inserts
>>> rows from that select in B.TABLE1.
>>> When the procedure is running and I do a SELECT * FROM A.TABLE1 then this
>>> statement get's locked. Why?
>>>
>>> Any pointers to the docs I should read ?

>> I take a wild guess here and assume you use a cursor to select from
>> A.table1? Append FOR READ ONLY to the cursor.
>> The cursor is "ambiguous" in the sense that it could be updated.
>> Depending on your settings DB2 may either default to FOR UPDATE or FOR
>> READ ONLY. If it defaults to FOR UPDATE DB2 will acquire update locks.
>>
>> Cheers
>> Serge

>
> Well, not exactly. The procedure has couple of FOR loops, not CURSORs. The
> truth is that the procedure itself is building SQL statements that are then
> executed via
> PREPARE S1 FROM SQLSTMT;
> EXECUTE S1;

FOR loops are cursors
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-27-2008, 07:32 AM
Gregor =?ISO-8859-2?Q?Kova=E8?=
 
Posts: n/a
Default Re: Locking issue

Serge Rielau wrote:

> Gregor Kovač wrote:
>> Serge Rielau wrote:
>>
>>> Gregor Kovač wrote:
>>>> Hi!
>>>>
>>>> Let's say that I have a table TABLE1 in schema A and in SCHEMA B.
>>>> I have a stored procedure that does a select from A.TABLE1 and inserts
>>>> rows from that select in B.TABLE1.
>>>> When the procedure is running and I do a SELECT * FROM A.TABLE1 then
>>>> this statement get's locked. Why?
>>>>
>>>> Any pointers to the docs I should read ?
>>> I take a wild guess here and assume you use a cursor to select from
>>> A.table1? Append FOR READ ONLY to the cursor.
>>> The cursor is "ambiguous" in the sense that it could be updated.
>>> Depending on your settings DB2 may either default to FOR UPDATE or FOR
>>> READ ONLY. If it defaults to FOR UPDATE DB2 will acquire update locks.
>>>
>>> Cheers
>>> Serge

>>
>> Well, not exactly. The procedure has couple of FOR loops, not CURSORs.
>> The truth is that the procedure itself is building SQL statements that
>> are then executed via
>> PREPARE S1 FROM SQLSTMT;
>> EXECUTE S1;

> FOR loops are cursors

hmm.. that explains a lot of things ))

Thanks a million times
--
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
| Gregor Kovac | Gregor.Kovac@mikropis.si |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| In A World Without Fences Who Needs Gates? |
| Experience Linux. |
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 02-27-2008, 07:34 AM
Gregor =?ISO-8859-2?Q?Kova=E8?=
 
Posts: n/a
Default Re: Locking issue

Gregor Kovač wrote:

> Serge Rielau wrote:
>
>> Gregor Kovač wrote:
>>> Serge Rielau wrote:
>>>
>>>> Gregor Kovač wrote:
>>>>> Hi!
>>>>>
>>>>> Let's say that I have a table TABLE1 in schema A and in SCHEMA B.
>>>>> I have a stored procedure that does a select from A.TABLE1 and inserts
>>>>> rows from that select in B.TABLE1.
>>>>> When the procedure is running and I do a SELECT * FROM A.TABLE1 then
>>>>> this statement get's locked. Why?
>>>>>
>>>>> Any pointers to the docs I should read ?
>>>> I take a wild guess here and assume you use a cursor to select from
>>>> A.table1? Append FOR READ ONLY to the cursor.
>>>> The cursor is "ambiguous" in the sense that it could be updated.
>>>> Depending on your settings DB2 may either default to FOR UPDATE or FOR
>>>> READ ONLY. If it defaults to FOR UPDATE DB2 will acquire update locks.
>>>>
>>>> Cheers
>>>> Serge
>>>
>>> Well, not exactly. The procedure has couple of FOR loops, not CURSORs.
>>> The truth is that the procedure itself is building SQL statements that
>>> are then executed via
>>> PREPARE S1 FROM SQLSTMT;
>>> EXECUTE S1;

>> FOR loops are cursors

> hmm.. that explains a lot of things ))
>
> Thanks a million times

Hmm...

So if I have a FOR loop defined like this:
FOR FOR1 AS SELECT F1, F2 FROM TABLE1 DO
UDPATE TABLE2 SET B = FOR1.F1 WHERE ID = F2;
END FOR

how do I apply FOR READ ONLY to it?
Maybe:
FOR FOR1 AS SELECT F1, F2 FROM TABLE1 FOR READ ONLY DO
UDPATE TABLE2 SET B = FOR1.F1 WHERE ID = F2;
END FOR
but these does not work.

Best regards,
Kovi
--
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
| Gregor Kovac | Gregor.Kovac@mikropis.si |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| In A World Without Fences Who Needs Gates? |
| Experience Linux. |
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 09:06 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com