October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

MySQL Overselling: How SELECT … FOR UPDATE Protects Inventory

A plain InnoDB SELECT does not lock inventory against concurrent purchases. See how SELECT ... FOR UPDATE protects a check-and-decrement transaction, plus the index and isolation details that affect locking.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A plain InnoDB SELECT reads data; it does not reserve the row against another transaction. If two purchases both read that the last unit is available, both can proceed unless the availability check and stock change are coordinated. Use SELECT ... FOR UPDATE inside a transaction when the application must inspect the current row before deciding whether to decrement stock.

Why a plain SELECT can oversell

Imagine an inventory row with stock = 1. Two purchase transactions run an ordinary SELECT and each sees one unit. Because these are nonlocking consistent reads, neither transaction prevents the other from changing the row after the read. Both application processes may therefore conclude that they can sell the item.

As an Amazon Associate I earn from qualifying purchases.

The weakness is the gap between observing a value and acting on it: the read did not give either transaction exclusive control of that inventory row. MySQL’s 8.4 Reference Manual warns that when a transaction queries data and then inserts or updates related data, a regular SELECT does not provide enough protection.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How SELECT … FOR UPDATE changes the flow

A locking read requests an exclusive lock on the rows it selects. Keep the read, availability check, and stock update in one transaction so a competing transaction that tries to lock or modify the same row must wait until the first transaction commits or rolls back.

#1 Best Overall
Sale
Tera Barcode Scanner Wireless with Screen: Pro Version 1D 2D QR with Setting Keypad Charging Cradle Works with Bluetooth 2.4G Wireless USB Wired Handheld Bar Code Reader HW0009
  • 【Unique Designed Screen Setting】It allows you to customize the screen display according to your preferences. With this innovative feature, you can easily set the language, adjust volume settings, select connection options, and view stored and total barcodes. Experience unparalleled convenience and flexibility as you personalize the settings of your Tera HW0009 to suit your specific needs. 【Package Includes: Barcode Scanner x1, Charging Cradle x1, Charging Cable x1, User Manual x1】
  • 【Superior Global CMOS Imaging Scanning】This advanced scanner excels in fast and accurate reading of both ordinary and high-density barcodes, including challenging formats like PDF417 found on driver's licenses. Its exceptional performance effortlessly handles various scanning scenarios, including underwater scanning, reading barcodes on silver paper, reflective materials, and more.
  • 【Charging Cradle & 2500mAh Large Battery】Designed with a convenient charging cradle, the HW0009 barcode scanner allows you to easily charge it whenever it's not in use. In addition, the scanner itself is equipped with a powerful 2500mAh battery, ensuring seamless all-day operation without the need for frequent charging.
  • 【3-in-1 Connections & Widely Compatible】 Tera HW0009 wireless barcode scanner can work with bluetooth & 2.4G wireless & usb wired. The transmission distance can be 328ft in barrier free environment and 114ft in obstacles environment using 2.4G USB dongle. It can be connected with a variety of devices, such as smartphones, computers, POS, tablets. In addition, it is also compatible with various operating systems, such as windows 11/10/8/7/xp, Mac OS, iOS, android, linux.
  • 【1D 2D QR code Programmable】2D: QR code, Data Matrix, PDF417(including PDF417 on driver’s license), Aztec, Maxicode, Micro QR, Micro PDF417; 1D: UPC/EAN, Code 128/EAN128, GS1-128, ISBT-128, Standard 2 of 5, Matrix 2 of 5, Code 39, Code 32, Code 93, Code 11, Codabar, PLESSEY, MSI, GSI Databar, ITF-14, GS1.
START TRANSACTION;

SELECT stock
FROM inventory
WHERE product_id = ?
FOR UPDATE;

-- In application code, verify stock >= requested_quantity.
-- If insufficient, ROLLBACK and report unavailable.

UPDATE inventory
SET stock = stock - ?
WHERE product_id = ?;

COMMIT;

Run the availability check only after the locking read returns. If the product is missing, stock is insufficient, or the operation fails, roll back rather than committing a partial reservation. The example shows the transaction shape; production code should also reject nonpositive requested quantities, check affected rows, and use the database driver’s transaction API correctly.

Choose a query that locks the intended rows

FOR UPDATE does not guarantee that only the one row you have in mind will be locked, regardless of the predicate. The access path and index matter. With a unique-index equality lookup, InnoDB can generally lock the matching record without locking the preceding gap. Range scans, nonunique indexes, or a scan without a suitable index can create a wider footprint, including range or next-key locking where applicable.

Rank #2
Sale
Tera Barcode Scanner Wireless 1D Laser Cordless Barcode Reader with Battery Level Indicator, Versatile 2 in 1 2.4Ghz Wireless and USB 2.0 Wired
  • Larger battery enables longer continuous usage and twice the stand-by time. With the unique battery indicator light showing the remaining battery level, no more Low Battery Anxiety.
  • The curved handle is extended and widened. With specially designed smooth and flat trigger for a better grip.
  • The orange anti shock silicone protective cover can prevent scratches and friction even when dropped from up to 6.56 feet. IP54 technology protects the wireless barcode scanner from dust.
  • Plug and play with the USB receiver or the USB cable, no driver installation needed. Easy and quick to set up. Wireless transmission distance reaches up to 328 ft. in barrier free environment.
  • Supports almost all 1D Barcodes: Febraban Bank Code, Codabar, Code 11, Code93, MSI, Code 128, EAN-128, Code 39, EAN-8, EAN-13, UPC-A, ISBN, Industrial 25, Interleaved 25, Standard 25, Matrix. Reads damaged, fuzzy, reflective and smudged barcodes.
  • For one inventory row, filter by its unique key where possible.
  • Check the query plan against the actual schema to confirm that the intended index is used.
  • Account for the deployed MySQL version and isolation level: exact lock behavior depends on these as well as the index and query plan.

See the MySQL manual’s InnoDB locks set by statements for details on how statements and access paths affect locking.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Understand the isolation-level interaction

InnoDB’s default isolation level is REPEATABLE READ. Ordinary consistent reads in a transaction use a snapshot established by the first consistent read, while locking reads use locking semantics. Mixing those read types casually can leave application logic reasoning from different views of the data. MySQL discusses this distinction in its transaction isolation-level documentation.

Rank #3
JRHC Wireless Barcode Scanner, Portable Inventory Scanner 1D 2D&PDF417 Data Collector Data Terminal Inventory Device with 2.4GHz Wireless & USB Wired Connection Bar QR Code Scanners
  • 【3-in-1 Multifunction Inventory Barcode Scanner】- The wireless barcode scanner is a multi-functional mode inventory scanner, Including scan gun mode, collection function, and inventory mode. You can create 180 storage libraries and store 400,000 data. Our inventory barcode scanners are mainly used in warehouses, medical, cosmetics stores, supermarkets, banks, logistics, libraries, shops, etc
  • 【Powerful Recognition】This bar code scanner can read one-dimensional barcodes and two-dimensional codes in all directions. Identify 1D: Codabar, Code 11, Code93, MSI, Code 128, EAN,UPC,Code 39, UPC-A, ISBN, Industrial 25, Standard25, Matrix;Recognize 2D: QR, DataMatrix, PDF417, Aztec, Micro PDF417. It can also read the QR code on the screen of other smart devices.
  • 【2.4G Wireless Long-distance Transmission】- Our wireless barcode scanner is connected to the computer through a 2.4G wireless USB receiver, supports WINDOWS XP/7/8/10 system, and is compatible with office software such as WORD/EXCEL/Text; the inventory barcode scanner transmits distance when there is no obstacle outdoors It can reach 200M/696 feet, and it can reach 50M/164 feet when there are obstacles or indoors
  • 【Data Storage】 With Internal 4MB flash, the device can store barcode data when away from the receiver and update the data when back to wireless transmission range. Support up to 100,000 barcodes storage when offline. And if you don’t need to transfer data to the pc, you can have it stored in the device and export it when you need to
  • 【Plug and play for Easy Portability】- Insert the USB wireless receiver into the computer, turn on the inventory scanner and connect to the computer immediately, plug and play, no need to install drivers or software, Compatible with most POS systems except those requiring proprietary hardware integrations or direct app-level integration.

For an inventory decision, use the locking read as the basis for the check and keep the dependent update in the same transaction. Confirm version-specific behavior in the manual for the MySQL release you deploy; the relevant documentation spans multiple manual versions.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Account for waits and deadlocks

Locking protects the decision by limiting concurrent access, but it can also make transactions wait. Concurrent transactions may deadlock, so application code should handle transaction failures and retry where appropriate. Keep the locked section short: avoid unrelated slow work before commit, and when a transaction needs several inventory rows, access them in a consistent order where practical. MySQL describes these concurrency behaviors in its InnoDB deadlock documentation.

Rank #4
ScanAvenger Wireless Portable 1D&2D with Stand Bluetooth Barcode Scanner: 3-in-1 Handheld Scanner, Rechargeable Battery for Inventory - USB Bar Code/QR Reader (1D&2D with Next Gen Stand)
  • Compatible with most POS systems except those requiring proprietary hardware integrations or direct app-level integration
  • No Software Needed: No need to download or install any software or apps with this sleek handheld 3-in-1 wireless, Bluetooth, and USB scanner with vibration capabilities to help in noisy environments.
  • Next Gen Smart Charging Stand: One base that can do it all. Wireless Transmission from stand to scanner. Holds scanner. Charges scanner's built-in rechargeable Li-Ion battery via lighting connectors
  • Scan Modes: Connect to Mac or Windows computers, Android or Apple mobile devices, and POS systems to start scanning barcodes with one of the 3 available modes - manual, continuous, and auto sense
  • Code Compatibility: Scan 1D barcodes including UPC, EAN, Code128, Code39, Code11, Codabar, and many others; Scan 2D barcodes including PDF417, Aztec code, Data Matrix, QR Code, Micro PDF, Interleaved, and others. Doesn't work with Maxicode

When an atomic conditional update may fit better

If the application does not need to inspect row state before writing, a conditional update can express the reservation in one statement: decrement only when stock >= requested_quantity, then inspect the affected-row count to determine whether it succeeded. This avoids a separate application-level check, but it may not suit workflows that need to inspect additional state or coordinate several records.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose between this approach and SELECT ... FOR UPDATE based on what the transaction must decide, which rows it must coordinate, and how the application detects insufficient stock and handles transaction failures. The MySQL locking documentation establishes the locking behavior, but does not benchmark these application-level designs.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.