Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 346257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:09:36+00:00 2026-05-12T11:09:36+00:00

Given a table of logical resource identifiers (one per row), what is the best

  • 0

Given a table of logical resource identifiers (one per row), what is the best way for an arbitrary number of database clients to perform the following operations:

  • Claim access to a specific resource, if it is not already claimed
  • SELECT the next available resource and claim it (similar to above)
  • Release a previously-claimed resource

(The table would have a “claimant” column, which would be NULL in unclaimed rows.)

I’m stuck on the atomicity of these operations: would I need a whole-table lock before I SELECT for the requested / next available resource (and afterwards UPDATE, assuming the claim was successful), or is there some more granular way to do this? I don’t have a huge volume of data now, but I’d prefer to keep the table as accessible as possible.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T11:09:37+00:00Added an answer on May 12, 2026 at 11:09 am

    You are basically describing a classical queue based workflow, and you should consider using a real queue.

    For the sake of discussion, here is how you achieve what you wish:

    • claim specific resource: SELECT ... FROM resources WITH (UPDLOCK, ROWLOCK) WHERE key = @key. Will block if resource is already claimed. Use lock timeouts to return exception if resource already claimed. key must be indexed and unique.
    • next available resource: SELECT ... FROM resources WITH (UPDLOCK, ROWLOCK, READPAST) ORDER BY <accessorder>. You must define a order by to express the preference of resources (oldest, highest priority etc)
    • release a claimed resource: COMMIT your transaction.

    The gist of the problem is using the right lock hints, and this kind of problem does require explicit lock hints to solve. UPDLOCK will act as a ‘claim’ lock. ROWLOCK creates the right granularity preventing the server from ‘optimizing’ to a page lock. READPAST allows you to skip claimed resources. Placing UPDLOCK on the rows will lock the row and allow you to update it later, but will prevent other operations like ordinary read-committed SELECTs that will block on the locked row. The idea is though that your are going to UPDATE the row anyway, which will place an unavoidable X lock. If you want to keep the table more available you can use app locks instead, but is significantly harder to pull off correctly. You will need to request an app lock on a string descriptor o the resource, like the key value, or a CHECKSUM of the key or it’s %%LOCKRES%% value. App locks allow you to separate the scope of the ‘claim’ from a transaction by requesting the app lock at the ‘session’ scope, but then you have to release the claim manually (‘transaction’ scoped app locks are released at commit time). Heads up though, there are a thousand ways to shoot yourself in the foot with app locks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given table: ID ONE TWO X1 15 15 X2 10 - X3 - 20
Given a table row, I want to get the HTML out of the span
Given a table action(start:DATE, length:NUMBER, type:NUMBER), with all records unique, I need to select
My application database has a Groups table that separates users into logical roles and
im programming a app to perform CRUD to any given table in any given
Notice that for the given table below, the first row or the first many
Problem: When I click the delete button for a given table/section row, i get
Is there a way to determine if a trigger on any given table affects
Given a table where the first column is seconds past a certain reference point
Given a table containing dotted quad IPv4 addresses stored as a VARCHAR(15) , for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.