Skip to content

feat(spanner): support select for update - #4586

Open
arnabnandy7 wants to merge 1 commit into
GoogleCloudPlatform:mainfrom
arnabnandy7:feature/spanner-for-update
Open

feat(spanner): support select for update#4586
arnabnandy7 wants to merge 1 commit into
GoogleCloudPlatform:mainfrom
arnabnandy7:feature/spanner-for-update

Conversation

@arnabnandy7

Copy link
Copy Markdown

Problem

SpannerRepository.findById() uses Spanner's Read API, which cannot express a
FOR UPDATE clause. Custom SQL can lock the parent entity, but eagerly loaded
interleaved children are fetched through expression subqueries and are not
locked by an outer FOR UPDATE clause. Lazy interleaved child queries also did
not inherit locking options.

This can cause excessive transaction aborts when concurrent read-write
transactions read and update the same entity.

Fixes #3984.

Solution

This change adds opt-in FOR UPDATE support for Spring Data Spanner:

  • Adds @Query(forUpdate = true) for derived and custom repository queries.
  • Adds SpannerRepository.findByIdForUpdate(...).
  • Adds SpannerQueryOptions.setForUpdate(true) for programmatic queries.
  • Applies FOR UPDATE to generated parent queries and eager interleaved-child
    expression subqueries.
  • Propagates locking to lazy interleaved-child queries.
  • Rejects locking queries outside read-write transactions.
  • Rejects combining forUpdate with DML queries.
  • Documents the new APIs, transaction requirement, and contention trade-offs.

Testing

Added unit coverage for:

  • Derived-query SQL generation.
  • Eager interleaved-child locking.
  • Query option behavior.
  • findByIdForUpdate validation.
  • Rejection outside read-write transactions.
  • Existing query and repository behavior.

Targeted test result:

  • 96 tests run.
  • 0 failures.
  • 0 errors.

Maven compilation and validation also complete successfully.

Signed-off-by: Arnab Nandy <arnab_nandy7@yahoo.com>
@arnabnandy7
arnabnandy7 requested a review from a team as a code owner July 18, 2026 11:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Spanner] Request to implement the FOR UPDATE clause when querying inside a read-write transaction

1 participant