IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Select for update


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
suresh.easy

New User


Joined: 14 May 2008
Posts: 1
Location: bangalore

PostPosted: Thu May 15, 2008 4:04 pm
Reply with quote

We have a requirement where we do the following:

1) Select current application number value from table.
2) Calculate new application number value. (add plus 1)
3) Store new application number value back to database with UPDATE
statement.

NB: Appl. number is char field.

When several users try to get an application number at the same time, for some users during step 3 (update), it gives "Data

has been changed by another user. Start again" as the timestamp is changed. This is bit irritating to the users and

confusing. To avoid, one solution is to use SELECT FOR UPDATE putting a rowlevel lock but read in one of forums that this

approach also causes problems:

bytes.com/forum/thread482255.html

Once a row is locked by a user, for all others who try to access that row gets a deadlock (what are the possible sqlcodes?)

One approach we thought was to allow deadlock sqlcodes as OK codes and try for 5 times to see if the lock is released before

giving up. Please suggest any other solution that you can think of.

Any way to use both select and update in the same sql.

Regards,
Suresh
Back to top
View user's profile Send private message
ashimer

Active Member


Joined: 13 Feb 2004
Posts: 551
Location: Bangalore

PostPosted: Thu May 15, 2008 5:01 pm
Reply with quote

deadlock sqlcode is -911.

a few questions ?

1. will there be only one ie the latest appl number in the table or you want the latest to be modified and updated ?

2. "Data has been changed by another user. Start again" as the timestamp is changed - how is this error thrown ?

Select for Update is a intent-to-change type of transaction and during this the locks acquired can be one of Share, Update or Exclusive.

So here a better option is to check a few times for getting an exclusive lock on the table and then update accordingly ...
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Thu May 15, 2008 5:49 pm
Reply with quote

Quote:

2. "Data has been changed by another user. Start again" as the timestamp is changed - how is this error thrown


seems as if an application program, attempting to go thru the 3 steps, timed out on step 1 or step 3 and issued the message - cics will not do this.

good logic would to put a controlled loop for x number of times to attempt to achieve an acceptable new char number.

Quote:

NB: Appl. number is char field.


why oh why do people insist on not using the correct data type when defining a column????

even so, why is it taking so long for an application program to:
do a select for update,
go thru the gyrations of adding 1 to a char field,
and issuing an update.

this process should not take so long. only the last of many, many similar tasks should be timed-out.

and a timed-out task which knows that it is often in contention should suspend for a few seconds, then retry.

keep in mind there is a retry limit defined to db2. often set at 10, and programmatically trying 10 times means 100 attempts.

I also believe this is poor programming to react to contention by telling the user to try again.

1. insure that the processing done between the select for update and the update is as little as possible.

2. programmatically loop thru and retry 10 or so times before telling the user he has to try again.
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Thu May 15, 2008 9:21 pm
Reply with quote

Hello,

Quote:
Once a row is locked by a user, for all others who try to access that row gets a deadlock
I believe this is not accurate. Once a row is locked, other "users" are forced to wait. . . The -911 should not be raised in this situation.

A deadlock occurs when there is an overlap of lock attempts by two different processes. Each process attempts to lock the same 2 rows in not the same sequence of locking. FWIW.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts Dynamically pass table name to a sele... DB2 2
No new posts SELECT from data change table DB2 5
No new posts Select two different counts from SQL... DB2 6
No new posts Select a DB2 value in a specific deci... DB2 4
No new posts How can I select certain file dependi... JCL & VSAM 12
Search our Forums:

Back to Top