Skip to content

chore(spanner): enforce read-only mode on DML statements with RETURNING - #15073

Open
sakthivelmanii wants to merge 2 commits into
mainfrom
chore/spanner-readonly-dml-returning
Open

sakthivelmanii wants to merge 2 commits into
mainfrom
chore/spanner-readonly-dml-returning

Conversation

@sakthivelmanii

Copy link
Copy Markdown
Contributor

Reject DML statements (INSERT, UPDATE, DELETE) containing a GoogleSQL THEN RETURN or PostgreSQL RETURNING clause when a Cloud Spanner Connection is in read-only mode or inside a READ_ONLY_TRANSACTION, as well as on read-only single-use transactions.

Previously, executing a DML statement with a returning clause through Connection.execute, Connection.executeAsync, or a read-only single-use transaction bypassed the read-only check enforced by Connection.executeQuery:

  • In autocommit read-only mode, the statement opened a read-write transaction and committed the mutation.
  • In transactional read-only mode, Connection.execute and Connection.executeAsync threw IllegalArgumentException instead of SpannerException with ErrorCode.FAILED_PRECONDITION.

All query-returning execution paths now consistently reject DML returning statements in read-only contexts with SpannerException (ErrorCode.FAILED_PRECONDITION) before starting a transaction or consuming any statement tag.

Reject DML statements (`INSERT`, `UPDATE`, `DELETE`) containing a GoogleSQL `THEN RETURN` or PostgreSQL `RETURNING` clause when a Cloud Spanner `Connection` is in read-only mode or inside a `READ_ONLY_TRANSACTION`, as well as on read-only single-use transactions.

Previously, executing a DML statement with a returning clause through `Connection.execute`, `Connection.executeAsync`, or a read-only single-use transaction bypassed the read-only check enforced by `Connection.executeQuery`:
- In autocommit read-only mode, the statement opened a read-write transaction and committed the mutation.
- In transactional read-only mode, `Connection.execute` and `Connection.executeAsync` threw `IllegalArgumentException` instead of `SpannerException` with `ErrorCode.FAILED_PRECONDITION`.

All query-returning execution paths now consistently reject DML returning statements in read-only contexts with `SpannerException` (`ErrorCode.FAILED_PRECONDITION`) before starting a transaction or consuming any statement tag.
@sakthivelmanii
sakthivelmanii requested review from a team as code owners November 18, 2026 21:52
@sakthivelmanii sakthivelmanii self-assigned this Nov 18, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors and centralizes the validation of DML statements with returning clauses in read-only connections and single-use transactions, ensuring checks are performed before allocating a unit of work or marking a transaction as used. Comprehensive tests are added to verify these behaviors. Feedback is provided to customize the error message in checkDmlReturningAllowed for plain DML statements without a returning clause to avoid misleading errors.

This branch has not been deployed

No deployments
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.

2 participants