Update / Select and readpast locks

Hi everybody,
We are running into concurrency issue with one of the applications that uses multi threading (200 threads / sec running that query (in a stored procedure)). The query with the concurrency issue is the following one:
--retrieve the oldest record that needs to be processed
select top 1 @conversation_id = conversation_id from
conversation with (rowlock) , network_carrier
with (rowlock)
where
conversation.[to] = network_carrier.full_carrier_oid and
network_code = @network_code and
process = 'R' and
process_count < 3
order by conversation_id asc
--update that record to 'processing' if it has not been grabbed by someone else since we selected it above
update conversation with (rowlock)
set process = 'S',
process_time = getdate(),
process_count = process_count +1
where
conversation_id = @conversation_id and
process = 'R'
--if the rowcount is > 0, that means we got it, so return it
if @@ROWCOUNT > 0
select * from conversation with (rowlock) where conversation_id = @conversation_id
So, in order to fix this, since we cannot change the application at this time, I was thinking of changing completely the way the query works to the following:
declare @conversation_id int
--update that record to 'processing' if it has not been grabbed by someone else since we selected it above
update conversation with (rowlock)
set process = 'S',
process_time = getdate(),
process_count = process_count +1,
@conversation_id = conversation_id
where
conversation_id = (
select top 1 conversation_id from
conversation with (readpast) , network_carrier
with (rowlock)
where
conversation.[to] = network_carrier.full_carrier_oid and
network_code = @network_code and
process = 'R' and
process_count < 3
order by conversation_id asc
select * from conversation with (rowlock) where conversation_id = @conversation_id
But even though the documentation says:
* A queue reader that uses READPAST skips past queue entries locked by other transactions to the next available queue entry, without having to wait until the other transactions release their locks.
* When specified in an UPDATE statement, READPAST is applied only when reading data to identify which records to update, regardless of where in the statement it is specified
Does this skipping include SHARED LOCKS made by selects ?
Thanks

Assuming that this is inside a transaction, I would change the SELECT to:
SELECT TOP (1) @conversation_id = c.conversation_id
FROM   conversation c WITH (UPDLOCK, READPAST)
WHERE  EXISTS (SELECT *
               FROM   network_carrier nc
               WHERE  c.[to] = nc.full_carrier_oid
                 AND  nc.network_code = @network_code)
 AND     c.process = 'R'
 AND   c.process_count < 3
ORDER  BY c.conversation_id ASC
I had to make a complete guess in which tables the unprefixed columns are, so you may need to rearranged.
I rewrote it with EXISTS, since that is apparently the purpose of the query.
I took out the ROWLOCK, because I don't see the point with them - if SQL Server feel compelled to escalate to table locks, you have bigger problems.
As for the locking, you need UPDLOCK to reserve a row, so that multiple process cannot grab the same guy. UPDLOCK is a shared lock that only can be held by one process. That is, regaular SELECT is still possible, but no one else can take an UPDLOCK, so they
will be blocked. Or would have, if it had not been for the READPAST hint.
I prefer to have the queries separate, as it makes clear what is happening. If you use an UPDATE with a subquery, the result may not be the expected.
Erland Sommarskog, SQL Server MVP, [email protected]

Similar Messages

  • My iphone 4s is stuck on the update screen and my lock screen button is broken. How do I reset it?

    I was trying to do the 7.1.2 update and my iphone 4s got stuck on the update screen, and my lock screen button is broken. How do I reset it?

    iPod: How to use the Screen Lock

  • UPDATE statement and database locks

    Hello everybody,
    I have a problem related to an UPDATE statement. There are two applications, let say A and B.
    Application A executes:
    update some_table
    set some_field = 'value_A',
    where pk_field=1
    no commit!
    Application B executes:
    update some_table
    set some_field = 'value_B',
    where pk_field=1
    Now application B is locked and wait until application A executes a commit.
    THIS IS A PROBLEM!
    I know one way to solve this problem:
    Both applications should execute "select for update nowait" before
    updating a row.
    Is there any other solutions? Something like "update nowait"?
    thanks in advance
    Dmitri Geller
    DGeller (at) lhsgroup dot com

    The major difference between my approach and standard "select for update nowait + update"
    approach, is more information for user : who blocked his object, and when. Else , i don't see the
    differences. Yep. And a Boieng 747 and bicycle is not much a difference as both are used for transporting people.
    Are you against select + nowait ?No, I am for understanding transaction isolation, serialisation, ACID principles, the differences between optimistic and pessimitic locking and so on
    You wrote "THE LOCK is good". OK, and what about time ?The two has NOTHING to do with one another. Not a single thing.
    In real industrial systems the users cannot wait more then few seconds. But the transactions can
    run much more. It isn't acceptable.If by implication you mean that I'm in the .edu environment that does not deal with "real corporate systems", I'm not.
    I recently wrote a special replicator in PL/SQL. It makes extensive use of customised parallel processing (does not use PQ as it also support Oracle SE). It hits very busy tables (with far over a 1000 rows/sec transaction rate). I did not have to hack my own form of locking. Performance is excellent.
    Why? Not because I am a brilliant Linus-like programming genius. Simply because I used Oracle as it has been designed to use.
    Can you claim the same? Can you show me the Oracle reference material that states that you need to write your own Lock Manager? Can you show me expert opinion from recognised inviduals such as Tom Kyte, Jonathan Lewis, Gary Milsap and others that state that Oracle's concurrency controls need to be hacked like you did in order to make it work?
    Can you provide me with any sound technical evidence to backup your claims that your method is better than what Oracle has built into the core of the database?

  • My iPhone 4 is stuck in the update screen and my lock button is not working. How can I fix this?

    I just tried to update my iPhone 4 to 7.1 and for some strange reason the phone wouldn't update then wanted me  to restore it... (Figures!) But my lock button no longer works so holding the home button and the lock button is a option I have to rule out. I have no way to repair my phone myself. What can I do?

    Hello Edward
    Follow the prompts on your iPhone and you will get either at your home screen like normal or get to a point of restoring from back up. The article below will give step by step for restoring from back up.
    iOS: How to back up and restore your content
    http://support.apple.com/kb/HT1766
    Thanks for using Apple Support Communities.
    Regards,
    -Norm G.

  • HT1212 if you cannot remember passcode you selected and get locked out of iphone 4 and it says to connect to itunes and it doesnt work what can you do to have use of your phone again?????

    Is there anyway you can enable iphone 4 afer being locked out by entering wrong passcode more than 5 times and it says to connect to itunes and still doesnt work???? so frustrating... apple cannot

    iOS: Forgotten passcode or device disabled after entering wrong passcode

  • I can't access the App Store icon. It opens but a large delta airlines app is superimposed over the top of the App Store updates icons and is locking the entire app. How do I delete it?

    I can't access the App Store app. When I tap the Icon, a large Delta airlines app is superimposed over the top of everything and freezes up the entire App Store. how can delete this large app and where did it come from.

    Quit the App Store app and reset your iPad.
    Double click the Home button to show the screen with running and recently used apps. Each app icon will have a sample page above it. Flick up on the page (not the app icon) and the page will fly away and the app icon will disappear. This quits that app. Then reset your device. Press and hold the Home and Sleep buttons simultaneously until the Apple logo appears. Let go of the buttons and let the device restart. See if that fixes your problem.

  • My computer restarts itself to update then when it comes the password has been changed and im locked

    my computer restarts itself to update then when it comes the password has been changed and im locked out how do i stop it and fix it

    Hi
    try to boot in safe mode, and check there if you have any alternate account (administrator ect) where you can gain access to your window and then change password of it.
    Hope it helps!
    **Clicking on the Kudos! white star is a nice way to say thank you on any post that helped you or resolved the problem.**
    **Selecting "Accept as Solution" for a reply that solves your issue helps others who are searching the web for an answer**
    **Clicking on the Kudos! white star is a nice way to say thank you on any post that helped you or resolved the problem.**
    **Selecting "Accept as Solution" for a reply that solves your issue helps others who are searching the web for an answer**

  • HT201412 did a software update and now the only thing I get is the itunes logo and arrow pointing up and a lock symbol with a line going down toward the screen button.  Any suggestions to get this to open?

    Trying to get the ipad to open after doing a software update.  I only get the itunes logo with an arrow pointing toward it and a lock symbol with a line going down towards the screen button.  Any suggestions to get this to open?

    You are in Recovery Mode. Follow the instructions below to recover your iPad.
    http://support.apple.com/kb/ht4097

  • I selected update option and it erased my ipod with all downloaded and paid materials...any ideas for how to restore?

    I selected the update option and it erased my ipod with all downloads and other materials lost...any ideas on how to restore?

    Everything should be on your computer.  Just sync it back.
    Did you fail to make sure that everything was on your computer before updating?

  • Was updating my touch this morning when an error message occured, itunes became non responsive and was shut down.  My touch is locked in the itunes update mode and my computer no longer recognizes the touch.  How do I correct?

    During update, error occured and Itunes became non-responsive and shut down.  My Itouch is now locked in the itunes update screen and is no longer identified by my computer.  How do I fix this problem?

    Try this:
    DFU Mode / Unable to Update or Restore
    http://support.apple.com/kb/HT1808
    DFU Mode
    Open iTunes and Connect the iPhone to your Mac
    Press and Hold the Sleep/Wake Button and the Home Button at the Same Time
    After 10 Seconds release the Sleep/Wake Button until iTunes tells you it has Detected an iPhone in Recovery Mode...
    The Phone Screen will remain Black... Now Restore.
    Backing up, Updating and Restoring
    http://support.apple.com/kb/HT1414

  • I updated my IOS on my iPhone 4S and it locked up.  I had to restore my phone and now iTunes will not go past the setup your phone screen even though the phone as been restored.  How do I get iTunes to go past this screen so I can sync my phone again?

    I updated my IOS on my iPhone 4S and it locked up.  I had to restore my phone and now iTunes will not go past the setup your phone screen even though the phone as been restored.  How do I get iTunes to go past this screen so I can sync my phone again?

    Depending on what iPhone you have when you get to the connect to wifi you should be able to activate your device over cellular data. Try doing a hard reset by holding the home circle button an power button until an apple pops up and the  go through the setup again and when you get to the wifi part a option should be setup with cellular data iPhone 4 and above should give this option sometimes doesn't like showing up that's why u have to do a hard reset first

  • Combine select and update into single statement,without bind-variable

    I have a problem, that I think is not possible to solve the way I want to, but I just wanted to check before leaving the idea...
    I am looking for a way to combine the select and the update statement into one single statement. This is what I wan't to achive: select some data, and update the data selected before returning them.
    On this site http://www.psoug.org/reference/update.html I see that the following are possible:
    var bnd1 NUMBER
    var bnd2 VARCHAR2(30)
    var bnd3 NUMBER
    UPDATE employees
    SET job_id ='SA_MAN', salary = salary + 1000,
    department_id = 140
    WHERE last_name = 'Jones'
    RETURNING salary*0.25, last_name, department_id
    INTO :bnd1, :bnd2, :bnd3;
    I need to have this as a single statement, and cannot use bind-variables. So I was hoping that something like this could be possible:
    UPDATE customer c
    SET c.HAS_CREDIT ='1'
    WHERE c.HAS_CREDIT = '0'
    RETURNING c.CUSTOMER_NO, c.FIRSTNAME, c.LASTNAME
    where c.HAS_CREDIT = '1'
    But this doesn't compile, complaining of missing into (ORA-00925: missing INTO keyword). And even though I would like this to be possible because this would solve my current problem, I think it would be very confusing. For instance; would the where clause of the returning part be operating after the update or before?
    Any comments or suggestions on how to get it work in a single statement, or should I just leave this path straight away?

    Hi,
    RETURNING only works with bind variables, see
    http://download-uk.oracle.com/docs/cd/B19306_01/appdev.102/b14261/returninginto_clause.htm#sthref3006
    The real problem is that the form of RETURNING clause with bind variables is only valid for single row update or insert statements.
    To update (or insert) multiple rows and return the data, you will need to use
    RETURNING BULK COLLECT INTO clause. See
    http://download-uk.oracle.com/docs/cd/B19306_01/appdev.102/b14261/tuning.htm#sthref2236
    Cheers,
    Colin

  • Can I use a select and update statement in a single jsp file?

    I want to update the BUY table everytime I would add a SELL transaction.....I want to minus the stocks that I sold to those that Ive bought before.....
    note: I used a seperate table in BUY and SELL transaction
    After I Have added a transaction, I want to update the buy table. This is my problem, can I used both SELECT and UPDATE statement at the same time in a single jsp file for example like this:
    select * from test, test1;
    update test
    set total_shares=total_shares-Stotal;
    where stock_code=Scode AND name_broker=Sbroker;
    Can i have both of these statements in the same jsp file in oder to update the buy table?
    Or can anyone suggest how can process that update?THANKS!
    --------------------

    Can i have both of these statements in the same jsp file in oder to update the buy table?Yes. But wouldn't it have been easier just to try it?

  • So i was updating my ipod touch (4g) to ios 6 and i needed to stop it in the middle of updating. so i held the home button and the lock button. it turned white and i tried charging it when it died but it went back to usual please help!

    so i was updating my ipod touch (4g) to ios 6 and i needed to stop it in the middle of updating. so i held the home button and the lock button at the same time for it to restart. It started turning on with the apple symbol and then the screen went white. I kept trying to do the same thing but it wouldnt work. Eace time i did that the screen was a different color. I was yellow/white at one point, blue at another time and sometimes the color behind the apple symbol. It got really HOT and i just let it die. When it was dead i put it on a charger. once it got charged up it started turning on with the apple symbol. then it turned white again and then restarted without me touching anything. When it restarted it turned the blue again. i took it off the charger immediantly because i didnt want it getting as hot as it was before. ive tried every way i know how to fix it but not putting it in the computer because i dont want it to ruin the computer. Please help because i really dont want to get a new ipod. Ive read a lot about how to fix it here on apple but there all the same answer and it still doesnt work. Please respond ASAP! thank you! i could really use your help right now!

    First see if placing the iPod in Recovery Mode will allow a restore.
    Next try DFU mode and restore.
    How to put iPod touch / iPhone into DFU mode « Karthik's scribblings
    If not successful then time for an appointment at the Genius Bar of an Apple store. You are not alone with this problem.

  • HT4623 I have never had a passcode to lock  my iPad. Now that I have updated it with IOS7 it is asking for one and is locked. How do I unlock this without having to reset the whole thing through iTunes????

    I have never had a passcode to lock  my iPad. Now that I have updated it with IOS7 it is asking for one and is locked. How do I unlock this without having to reset the whole thing through iTunes????

    Unfortunately, if the iPad is insisting that you use a passcode, there is no way around it other than to restore the device using iTunes on a computer, or by erasing it in iCloud.com via Find My iPhone.
    You could try resetting the iPad and cross your fingers and hope this is just a blip on the radar....hold down on the sleep and home buttons at the same time for about 10 seconds until the Apple logo appears on the screen.

Maybe you are looking for

  • How do I use the same pictures for all desktops

    I am trying to use the same folder of picture across all my desktops.  It seems that every desktop I have to go in to preferences and click the same thing over and over.  Is there a way to do it once and apply it across all desktops?

  • G570 really bad graphics problem

    Hi all, my 1st post so hope someone can help me as i need to get this sorted before i throw the laptop through a window! Basically i use my G570 for work (graphic designer) and i have had problems with the display when i have a couple of programs wor

  • Cannot change song order in Manual Sync mode

    Hello, A few years ago my parents bought me a laptop, I have my iPod on Manual Sync mode so none of my music from my old computer is lost. However, I've noticed that if you are in Manual Sync mode, you cannot control the order songs are listed in wit

  • How to export entire iPhoto library to external hard drive

    i am trying to move my library to an external drive and have followed these & similar instructions: http://discussions.apple.com/thread.jspa?threadID=1256384 when it comes to the step of opening iphoto & choosing a library i select the one i dragged

  • Will a voice dialing car kit feature work with iphone 3g?

    I am a new iphone user, and I know it doesn't have voice dialing, but I can't figure out if I can get around this by buying a car kit that offers voice dialing (e.g. parrot or motorola). Does this feature on the car kit require that the phone do voic