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 699865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:26:01+00:00 2026-05-14T03:26:01+00:00

I have a strange issue (at least for me :)) with the MySQL’s locking

  • 0

I have a strange issue (at least for me :)) with the MySQL’s locking facility.

I have a table:

create table `test` (  
  `id` int(11) NOT NULL AUTO_INCREMENT,  
  PRIMARY KEY (`id`)  
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=latin1

With this data:

+—-+
| id |
+—-+
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 10 |
| 11 |
| 12 |
+—-+

Now I have 2 clients with these commands executed at the beginning:

set autocommit=0;
set session transaction isolation level serializable;
begin;

Now the most interesting part. The first client executes this query: (makes an intent to insert a row with id equal to 9)

SELECT * from test where id = 9 FOR UPDATE;
Empty set (0.00 sec)

Then the second client does the same:

SELECT * from test where id = 9 FOR UPDATE;
Empty set (0.00 sec)

My question is: Why the second client does not block ? An exclusive gap lock should have been set by the first query because FOR UPDATE have been used and the second client should block.

If I am wrong, could somebody tell me how to do it correctly ?

The MySql version I use is: 5.1.37-1ubuntu5.1

  • 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-14T03:26:01+00:00Added an answer on May 14, 2026 at 3:26 am

    Because at that time, it’s safe to return the (empty) result – there’s no lock to set for the record with id=9 as that doesn’t exist and thus it cannot be updated – I don’t think you can rely on innodb setting a read locks in such a case. It should set a write lock on id=9 though.

    If at a later time, one of the transactions do update the table, and touch the same data as another transaction – the update would likely block in one of the transactions and later on fail if the other transaction commited that data. It’s perfectly normal for transactions to fail in scenarios like this – leaving you to handle it – which usually is a matter of retrying the transaction.

    If there were a record with id=9 you’d probably see the 2. select block until the first transaction is finished, as now there is a record that have to be read locked in case the first transaction decides to update that row.

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

Sidebar

Related Questions

I have a strange issue: I am using SPContext.Current.Web in a .aspx page, but
I have this strange issue with my web app. You see, I'm using jQuery
I have a strange issue that has arisen recently: Whenever I enter text, even
I have a strange issue with jQuery's hover, addClass $(document).ready(function(){ $('#selectable li').hover( function(){ $(this).addClass('selecting',
I have a strange issue with eclipse. When I put a .xls file in
I have a really strange issue. I am working on a Java SWING application
I have a very strange issue on my hands. I have two IIS websites
I, in fact two end users have seen a strange issue in Excel. When
Strange issue. I have SQL Server running on an EC2 box that I can
I have a strange one (or at least it's beyond me as a novice

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.