473,386 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Using a cursor for Update

Hi All,

I've been tasked with writing a z/OS C program to read and update a table
(DB/2 v8). This Table contains a single row with a single column of data.
This program (which will run as a Stored Proc) needs to implement a strategy
for concurrency as the Stored Proc can be called concurrently by several
different users.

The code essentially does the following:

1) Read the current value (eg. "ABCD")
2) Increment the value to the next value (eg. "ABCE")
3) Write the incremented value back to the table

In my code, I'm doing the following:

EXEC SQL
DECLARE C2 SENSITIVE STATIC SCROLL CURSOR FOR
SELECT NEXT_MAILBOX_ID FROM BTMFE_MB_NEXT_AVL
FOR UPDATE OF NEXT_MAILBOX_ID;

EXEC SQL
OPEN C2;

EXEC SQL
FETCH FROM C2
INTO :OLD_MAILBOXID;

// the next value is calculated here and stored in :NEW_MAILBOXID
// the new value is then written back to the table

EXEC SQL
UPDATE BTMFE_MB_NEXT_AVL
SET NEXT_MAILBOX_ID = :NEW_MAILBOXID
WHERE CURRENT OF C2;

EXEC SQL
CLOSE C2;

It seems a little overkill to use a Cursor for something so simple, but our
DBA recommended we use one.

Everything works as expected, but I'm not sure if concurrency has been
addressesed.

- Do I need to use a SCROLL cursor when the table only has a single record
with a single column?
- How do I ensure that the row is locked during the above activity?

Sorry for the basic questions, but I'm not a database guy.

Thanks in advance.

Apr 24 '06 #1
5 34188
Paul M wrote:
Hi All,

I've been tasked with writing a z/OS C program to read and update a table
(DB/2 v8). This Table contains a single row with a single column of data.
This program (which will run as a Stored Proc) needs to implement a strategy
for concurrency as the Stored Proc can be called concurrently by several
different users.

The code essentially does the following:

1) Read the current value (eg. "ABCD")
2) Increment the value to the next value (eg. "ABCE")
3) Write the incremented value back to the table

In my code, I'm doing the following:

EXEC SQL
DECLARE C2 SENSITIVE STATIC SCROLL CURSOR FOR
SELECT NEXT_MAILBOX_ID FROM BTMFE_MB_NEXT_AVL
FOR UPDATE OF NEXT_MAILBOX_ID;

EXEC SQL
OPEN C2;

EXEC SQL
FETCH FROM C2
INTO :OLD_MAILBOXID;

// the next value is calculated here and stored in :NEW_MAILBOXID
// the new value is then written back to the table

EXEC SQL
UPDATE BTMFE_MB_NEXT_AVL
SET NEXT_MAILBOX_ID = :NEW_MAILBOXID
WHERE CURRENT OF C2;

EXEC SQL
CLOSE C2;

It seems a little overkill to use a Cursor for something so simple, but our
DBA recommended we use one.

Everything works as expected, but I'm not sure if concurrency has been
addressesed.

- Do I need to use a SCROLL cursor when the table only has a single record
with a single column?
- How do I ensure that the row is locked during the above activity?

The row is locked by virtue of the cursor being declared FOR UPDATE.
But you don't need a scrollable cursor for that. Any cursor will do.

In a future version DB2 for zOS may support:
SELECT mailboxid INTO :newmailboxid
FROM FINAL TABLE(UPDATE BTMFE_MB_NEXT_AVL
SET mailboxid = <blurp>(mailboxid))

That's denser
Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Apr 24 '06 #2

"Paul M" <no****@nospam.com> wrote in message
news:HT*****************@news20.bellglobal.com...
Hi All,

I've been tasked with writing a z/OS C program to read and update a table
(DB/2 v8). This Table contains a single row with a single column of data.
This program (which will run as a Stored Proc) needs to implement a
strategy
for concurrency as the Stored Proc can be called concurrently by several
different users.

The code essentially does the following:

1) Read the current value (eg. "ABCD")
2) Increment the value to the next value (eg. "ABCE")
3) Write the incremented value back to the table

In my code, I'm doing the following:

EXEC SQL
DECLARE C2 SENSITIVE STATIC SCROLL CURSOR FOR
SELECT NEXT_MAILBOX_ID FROM BTMFE_MB_NEXT_AVL
FOR UPDATE OF NEXT_MAILBOX_ID;

EXEC SQL
OPEN C2;

EXEC SQL
FETCH FROM C2
INTO :OLD_MAILBOXID;

// the next value is calculated here and stored in :NEW_MAILBOXID
// the new value is then written back to the table

EXEC SQL
UPDATE BTMFE_MB_NEXT_AVL
SET NEXT_MAILBOX_ID = :NEW_MAILBOXID
WHERE CURRENT OF C2;

EXEC SQL
CLOSE C2;

It seems a little overkill to use a Cursor for something so simple, but
our
DBA recommended we use one.

Everything works as expected, but I'm not sure if concurrency has been
addressesed.

- Do I need to use a SCROLL cursor when the table only has a single record
with a single column?
- How do I ensure that the row is locked during the above activity?

Sorry for the basic questions, but I'm not a database guy.

Thanks in advance.


The rows remain locked after an update until you do a commit. Whether you
need a cursor depends partly on how many rows you are updating, and whether
you should do intermediate commits every few hundred rows or so. If there
are a lot of rows, you can use the "with hold option" and do intermediate
commits without automatically closing the cursor.
Apr 25 '06 #3
Thanks, Mark.

Is a Commit done automatically when I close the Cursor or do I have to
explicitly issue a commit? My table has a single row with a single column.
The stored value is essentially an incrementing counter that I don't want
read or altered by concurrent requests.

Thanks again.

"Mark A" <no****@nowhere.com> wrote in message
news:1r******************************@comcast.com. ..

"Paul M" <no****@nospam.com> wrote in message
news:HT*****************@news20.bellglobal.com...
Hi All,

I've been tasked with writing a z/OS C program to read and update a table (DB/2 v8). This Table contains a single row with a single column of data. This program (which will run as a Stored Proc) needs to implement a
strategy
for concurrency as the Stored Proc can be called concurrently by several
different users.

The code essentially does the following:

1) Read the current value (eg. "ABCD")
2) Increment the value to the next value (eg. "ABCE")
3) Write the incremented value back to the table

In my code, I'm doing the following:

EXEC SQL
DECLARE C2 SENSITIVE STATIC SCROLL CURSOR FOR
SELECT NEXT_MAILBOX_ID FROM BTMFE_MB_NEXT_AVL
FOR UPDATE OF NEXT_MAILBOX_ID;

EXEC SQL
OPEN C2;

EXEC SQL
FETCH FROM C2
INTO :OLD_MAILBOXID;

// the next value is calculated here and stored in :NEW_MAILBOXID
// the new value is then written back to the table

EXEC SQL
UPDATE BTMFE_MB_NEXT_AVL
SET NEXT_MAILBOX_ID = :NEW_MAILBOXID
WHERE CURRENT OF C2;

EXEC SQL
CLOSE C2;

It seems a little overkill to use a Cursor for something so simple, but
our
DBA recommended we use one.

Everything works as expected, but I'm not sure if concurrency has been
addressesed.

- Do I need to use a SCROLL cursor when the table only has a single record with a single column?
- How do I ensure that the row is locked during the above activity?

Sorry for the basic questions, but I'm not a database guy.

Thanks in advance.

The rows remain locked after an update until you do a commit. Whether you
need a cursor depends partly on how many rows you are updating, and

whether you should do intermediate commits every few hundred rows or so. If there
are a lot of rows, you can use the "with hold option" and do intermediate
commits without automatically closing the cursor.

Apr 25 '06 #4
Paul M wrote:
Thanks, Mark.

Is a Commit done automatically when I close the Cursor or do I have to
explicitly issue a commit? My table has a single row with a single
column. The stored value is essentially an incrementing counter that I
don't want read or altered by concurrent requests.


The end of a transaction (commit or rollback) is completely independent of
cursors. So you have to use your own explicit COMMIT statement (or the
respective API call). Where a connection does exist is that all cursors
are closed at ROLLBACK and non-holdable cursors are closed at COMMIT
(holdable cursors are kept open after the commit).

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Apr 25 '06 #5
"Paul M" <no****@nospam.com> wrote in message
news:sN******************@news20.bellglobal.com...
Thanks, Mark.

Is a Commit done automatically when I close the Cursor or do I have to
explicitly issue a commit? My table has a single row with a single
column.
The stored value is essentially an incrementing counter that I don't want
read or altered by concurrent requests.

Thanks again.


This is a common situation. The following is the best solution and will
avoid deadlocks:

-- increment the sequence number and hold exclusive lock on the row

UPDATE sequence_table
SET seq_number = seq_number + 1
WHERE seq_key = ?;
-- to retrieve the value you just incremented above

SELECT seq_number
FROM sequence_table
WHERE seq_key = ?;

COMMIT;

Apr 25 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: xxbmichae1 | last post by:
I have a <select> object that i've set up an onchange event that fires in IE fine when I use the cursor up and down in the list, but If I use the cursor up and down in Firefox the event doesn't...
1
by: Bennett Haselton | last post by:
Suppose I add a new row to a table in a dataset, and then I use an OleDbDataAdapter to add that new row to a SQL Server database using OleDbDataAdapter.Update(), as in the following code: ...
0
by: Praveen_db2 | last post by:
Hi All Db2 8.1.3 I have a SP ,Say SP1. SP1 is returning a dynamic result set i.e. a cursor is left opened. This SP is called in another SP ,Say SP2. A result set locator (RS1) is associated...
10
by: Roger Withnell | last post by:
I'm using ASP, VBScript and SQL Server. I'm also using UTF-8 character set and so my codepage is 65001 and SQL Server datatype nvarchar. I can insert unicode characters correctly into the...
0
by: anonieko | last post by:
This approach I found very efficient and FAST when compared to the rowcount, or Subquery Approaches. This is before the advent of a ranking function from DB such as ROW_NUMBER() in SQL Server...
7
by: ssshhh | last post by:
Hi! I'm new here. Here's my problem. How can u reconcile two tables using cursor? I would like to create a new table reflecting the matching records of the two tables I've compared. how will...
1
by: ashokakr | last post by:
hi all, I have a doubt in cursor.Can we use cursor for update query in procedures. I tried all the cursor concepts but i could not found it, So please help me in giving the syntax for the...
5
by: shanks0092001 | last post by:
Hi All, I need a urgent help on the pl/sql written below declare rate integer; cursor c_f is select treccy,treamt from tredtl where treccy not in ('YEN,'EUR'); begin select fxrate into rate...
2
by: gjain12 | last post by:
When I am trying to declare a cursor over a temproay table in my stored procedure I am getting some errors. Below are the piece of code from my stored procedure in which I am getting error. ...
0
by: bala venkata siva ram kum | last post by:
hi I want autocompletetextview in android here we are using sqlite database with query using cursor. public void cal() { SQLiteDatabase db= null; String TableName = "enquiryhead"; db...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.