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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:33:50+00:00 2026-05-13T21:33:50+00:00

How to prevent a race condition in MySQL database when two connections want to

  • 0

How to prevent a race condition in MySQL database when two connections want to update the same record?

For example, connection 1 wants to increase “tries” counter. And the second connection wants to do the same. Both connections SELECT the “tries” count, increase the value and both UPDATE “tries” with the increased value. Suddenly “tries” is only “tries+1” instead of being “tries+2”, because both connections got the same “tries” and incremented it by one.

How to solve this problem?

  • 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-13T21:33:51+00:00Added an answer on May 13, 2026 at 9:33 pm

    Here’s 3 different approaches:

    Atomic update

    update table set tries=tries+1 where condition=value;
    

    and it will be done atomically.

    Use row locking

    If you do need to first select the value and update it in your application, you likely need to use row locking. That means you’ll have to use InnoDB, not MyISAM tables.
    Your query would be something like:

    select tries from table where condition=value for update;
    .. do application logic to add to `tries`
    update table set tries=newvalue where condition=value;
    

    All other queries trying to read the same row will have to wait, until the table gets updated, and they will return the updated value.

    Version scheme

    A common approach is to introduce a version column in your table. Your queries would do something like:

    select tries,version from table where condition=value;
    .. do application logic, and remember the old version value.
    update table set tries=newvalue,version=version + 1 where condition=value and version=oldversion;
    

    If that update fails/returns 0 rows affected, someone else has updated the table in the mean time. You have to start all over – that is, select the new values, do the application logic and try the update again.

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

Sidebar

Related Questions

I want to prevent XSS attacks in my web application. I found that HTML
Does TCP/IP prevent multiple copies of the same packet from reaching the destination? Or
I need to prevent Session Fixation , a particular type of session hijacking, in
How do I prevent vim from replacing spaces with tabs when autoindent is on?
Is it possible to prevent an asp.net Hyperlink control from linking, i.e. so that
How do I prevent my users from accessing directly pages meant for ajax calls
How can I prevent a user from resizing GridViewColumns withing a ListView control?
Is it possible to prevent stack allocation of an object and only allow it
I am trying to prevent data races in a multihreaded server. My problem is
How can I prevent the user from being able to resize an image in

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.