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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:56:49+00:00 2026-06-15T02:56:49+00:00

I have a table tbl_orders . It has a quantity field quantity . In

  • 0

I have a table tbl_orders. It has a quantity field quantity.
In some part of my application I need to decrement the quantity by 1.

I already know the id of the record (available from the client side), so I issue an update statement:

UPDATE tbl_orders
SET quantity=quantity-1
WHERE id= 6 

The problem is that this query can accidentally be run multiple times concurrently.
For example, 2 customer service operators may update the same record simultaneously.
That means that the quantity will be decremented by 2 when it is supposed to be decremented once only.

I tried putting the update in a transaction, but that resulted in only delaying the second transaction until the first one was committed. Once it was committed the second update ran and decremented the record again.

How can I make sure that other queries fail if one is modifying a record?

UPDATE:

for an update to be valid the quantity on the client side was the same in the database. For example if a user sees a quantity 5 on his browser and wants to decrement it, the value in database must be the same.

UPDATE 2

I found a good explanation here for optimistic locking using Doctrine 2:

  • 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-15T02:56:51+00:00Added an answer on June 15, 2026 at 2:56 am

    One approach I’ve used/seen in the past was having a timestamp column. When querying, ensure you have both the ID and the original timestamp at the start of editing the record. Then, when you are trying to update, send via

    update YourTable
       set counter = counter -1,
           TheTimestampColumn = new timestamp value
       where ID = yourID
         and TheTimeStampColumn = timeStampEditStartedWith
    

    This way, whoever gets to it first with the original starting timestamp would win. For the one following, you would have to track how many records were updated, and if that count equals zero, then you would notify the user that another person made a change to the record while you were viewing it. Do you want to reload the latest data? (or something like that).

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

Sidebar

Related Questions

I have a table called tbl_loans which has the field loan_number . Loan numbers
i have a table 'tbl_orders', that table contains the folllowing order_id | customer_id |
I need to copy some data from one table to another but I need
I need some help optimizing a MySQL query or table When I run this
I have a database in MYSQL and it has chat table which looks like
I have a table that has a fixed layout of rows. Each row has
I have several tables in a database. One table (tbl_transactions) has thousands of orphaned
I need to make complicated query. I have table: CREATE TABLE IF NOT EXISTS
i have a table (tbl_world) which look like this id | first_name | last_name
I have a table that contains intervals: CREATE TABLE tbl ( user_id: INTEGER, start:

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.