Skip to content

docs: add FOR UPDATE clause to MySQL transaction example #4035

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

kotahorii
Copy link
Contributor

Fixes #3485

Summary

Adds the FOR UPDATE clause to the MySQL transaction example to prevent race conditions when reading and then updating data within the same transaction.

Changes Made

  1. Modified GetRecord query to include FOR UPDATE clause:

    -- Before
    SELECT * FROM records WHERE id = $1;
    
    -- After  
    SELECT * FROM records WHERE id = $1 FOR UPDATE;
  2. Added explanatory section about locking reads in transactions to help users understand when and why to use SELECT ... FOR UPDATE

Problem Addressed

The current transaction example shows a read-then-update pattern without proper row locking, which can lead to race conditions in MySQL where other transactions might modify the same rows between the SELECT and UPDATE operations.

According to the MySQL documentation:

"If you query data and then insert or update related data within the same transaction, the regular SELECT statement does not give enough protection. Other transactions can update or delete the same rows you just queried."

Solution

The FOR UPDATE clause provides exclusive row locking in MySQL/InnoDB, preventing other transactions from modifying the selected rows until the current transaction completes.

References

🤖 Generated with Claude Code

Fixes issue where transaction example doesn't properly lock rows
in MySQL, which could lead to race conditions when reading and
then updating the same data.

The SELECT statement in the transaction example now includes
FOR UPDATE clause to provide exclusive row locking in MySQL/InnoDB,
as recommended in the MySQL documentation for safe read-then-update
operations within transactions.

Also adds explanation about locking reads in transactions to help
users understand when and why to use SELECT ... FOR UPDATE.

Fixes sqlc-dev#3485

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@dosubot dosubot bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Aug 2, 2025
@kyleconroy kyleconroy closed this Aug 2, 2025
@kyleconroy
Copy link
Collaborator

The sqlc documentation isn't the correct place for database-specific transaction documentation. When and where to use FOR UPDATE is a large topic better left to MySQL-specific documentation.

@kotahorii kotahorii deleted the fix-mysql-transaction-docs branch August 3, 2025 01:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size:XS This PR changes 0-9 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Error in the documentation for transactions?
2 participants