Repository navigation
Conversation
Acquiring exclusive row locks during repeatable read transactions in Cloud Spanner requires combining IsolationLevel.REPEATABLE_READ with SELECT FOR UPDATE inside run_in_transaction.
|
Here is the summary of changes. You are about to add 1 region tag.
This comment is generated by snippet-bot.
|
There was a problem hiding this comment.
Code Review
This pull request introduces a new Python sample demonstrating pessimistic row locking (SELECT ... FOR UPDATE) under REPEATABLE_READ isolation in Cloud Spanner, along with its associated tests and configuration files. A review comment identifies a potential issue in select_for_update.py where querying non-existent albums or encountering NULL budget values could lead to IndexError or TypeError exceptions, and suggests adding appropriate validation and fallback logic.
| source_budget = int(source_rows[0][0]) - transfer_amount | ||
| target_budget = int(target_rows[0][0]) + transfer_amount |
There was a problem hiding this comment.
If the requested albums do not exist in the database, source_rows or target_rows will be empty, causing an IndexError when attempting to access source_rows[0] or target_rows[0]. Additionally, if MarketingBudget is NULL, calling int() on None will raise a TypeError. It is safer to check if the rows exist and handle potential None values gracefully.
if not source_rows or not target_rows:
raise ValueError("Source or target album not found.")
source_budget = (source_rows[0][0] or 0) - transfer_amount
target_budget = (target_rows[0][0] or 0) + transfer_amountThere was a problem hiding this comment.
Thanks! This comment refers to an earlier revision of this file; the code here has since been reworked in later commits, so this thread no longer applies to the current diff.
… UPDATE Running the budget transfer against an unseeded Albums table or a row with a NULL MarketingBudget previously raised an unhandled IndexError or TypeError.
Prints short resource identifiers instead of full project-prefixed resource paths and omits project_id from diagnostic messages.
Prints only runtime resource and query results instead of hardcoded string literals.
Description
Under
IsolationLevel.REPEATABLE_READ, plainSELECTqueries read at the transaction's snapshot timestamp without verifying whether concurrent transactions modify those cells before commit, so transactions that compute updates from read values must useSELECT ... FOR UPDATEto validate scanned cells at commit time and prevent lost updates.Establishes the
spanner/transactionsPython 3.13+ sample package and addsspanner_select_for_updatedemonstratingSELECT ... FOR UPDATEwithTransactionOptions.IsolationLevel.REPEATABLE_READ.Checklist
Testing
Compliance & Style
spanner/, which the default repository owners cover.Post-Approval Actions