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

  • Home
  • SEARCH
  • 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 8597891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:01:22+00:00 2026-06-12T01:01:22+00:00

I am currently working with transactions and getting confused. These transaction are created at

  • 0

I am currently working with transactions and getting confused. These transaction are created at the data access layer and not in the stored procedures of the database (SQL Server 2008).
I understand the normal working of Isolation Levels set for a transaction.
I am unable to comprehend what should happen in the following scenario.

  1. Initiate a transaction
  2. Select Employee with ID=1.
  3. Update Employee with ID=1.
  4. Commit

There are multiple threads doing the same thing but different ID. But there might be a case where in two threads look up the same ID. Lets call them Thread A and B. The above steps progress in the following manner with respect to the two threads. The Isolation Level is set to Repeatable Read.

A1. Initiate a transaction
A2. Select Employee with ID=1.
B1. Initiate a transaction
B2. Select Employee with ID=1.
A3. Update Employee with ID=1.
A4. Commit
B3. Update Employee with ID=1.
B4. Commit

What I really want to achieve from the transaction is that when Thread A selects a particular record, Thread B should not even be able to select that record. I don’t know if I am thinking on the right track by using transactions and locks for this scenario.

Awaiting replies 🙂

  • 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-06-12T01:01:24+00:00Added an answer on June 12, 2026 at 1:01 am

    You should use an UPDLOCK table hint to prevent deadlocks, e.g.,

    select * from employee with (updlock) where id = @id
    update employee set name = @name where id = @id
    

    Without this you could get deadlocks, because the select by default takes a shared read lock:

    1. Transaction A does the select (shared read lock).
    2. Transaction B does the select (shared read lock, could be on some of
      the same records as transaction A, e.g., if a page lock is taken).
    3. Transaction A now does the update, which requires an exclusive write
      lock (lock escalation), but has to wait for transaction B to release
      its shared read lock.
    4. Transaction B now also wants to do its update, but has to wait for
      transaction A to release its shared read lock.

    So transaction A and B are now waiting for each other – classic lock escalation deadlock. The UPDLOCK table hint avoids this because it forces the select to take an exclusive lock:

    1. Transaction A does the select (exclusive update lock).
    2. Transaction B wants to do its select but has to wait for transaction A to release its lock first.
    3. Transaction A now does the update, and commits, releasing the update lock taken by the select.
    4. Transaction B can now do its select.

    Edit: You can combine the UPDLOCK with a ROWLOCK to ask for a row level lock, e.g., “with (updlock, rowlock)”. You can ask, but you may not always get it – see http://msdn.microsoft.com/en-us/library/ms187373(v=sql.100).aspx. Also row locks can be more expensive than page locks, because SQL Server will probably have a lot more locks to track if you use row locks. So I would let SQL Server choose for itself the extent of the lock, it normally does an OK job; in this case it shouldn’t take a table lock. Only explicitly use a rowlock if you have a problem without it.

    Also note that a rowlock on its own won’t prevent a deadlock where two transactions select the same record (row) and then try to update it – so you always need an updlock for this.

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

Sidebar

Related Questions

I am currently working on a project that involves generating sequences using stored procedures
Currently working with the following package structure: /package __init__.py final.py /write __init__.py write.py /data
Currently working on a site built in Django and i'm getting an issue when
I'm currently working on a database-heavy, line-of-business Rails application for insurance brokers that is
My application currently needs to upload a large amount of data to a database
Currently I'm working on in-App Purchase functionality, and I am getting below of the
The project I'm working on uses straight JDBC data access in all its boilerplate
Currently working with converting SQLException error messages into messages that are more useful for
Currently working for the first time with JSON and with little experience of jQuery.
Currently working in the deployment of an OFBiz based ERP, we've come to the

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.