Skip to content

docs(spanner): snippets to set transaction options#2194

Open
skuruppu wants to merge 1 commit intoGoogleCloudPlatform:mainfrom
skuruppu:spanner-read-lock-mode-samples
Open

docs(spanner): snippets to set transaction options#2194
skuruppu wants to merge 1 commit intoGoogleCloudPlatform:mainfrom
skuruppu:spanner-read-lock-mode-samples

Conversation

@skuruppu
Copy link
Copy Markdown
Contributor

@skuruppu skuruppu commented Apr 8, 2026

Adds a snippet for setting the isolation level and another for setting the read lock mode.

Both samples show how to set the isolation level and read lock mode at the client level, and how to set it at a transaction level.

@skuruppu skuruppu requested review from a team as code owners April 8, 2026 03:43
@snippet-bot
Copy link
Copy Markdown

snippet-bot bot commented Apr 8, 2026

Here is the summary of changes.

You are about to add 2 region tags.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@product-auto-label product-auto-label bot added api: spanner Issues related to the Spanner API. samples Issues that are directly related to samples. labels Apr 8, 2026
Copy link
Copy Markdown

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

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 introduces new PHP code samples and integration tests for Google Cloud Spanner, demonstrating transaction isolation levels and read lock modes. Several technical issues were identified in the implementation: the defaultTransactionOptions configuration key is not supported by the SpannerClient constructor, the readLockMode option is incorrectly nested and will be ignored by the library, and the use of REPEATABLE_READ isolation is invalid for the read-write transaction context provided in the sample.

printf('%d record(s) updated.' . PHP_EOL, $rowCount);
}, [
'transactionOptions' => [
'isolationLevel' => IsolationLevel::REPEATABLE_READ
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

Cloud Spanner only supports SERIALIZABLE isolation for read-write transactions in the GoogleSQL dialect. REPEATABLE_READ is only supported for read-only transactions. Since runTransaction creates a read-write transaction and this snippet performs an UPDATE, using REPEATABLE_READ here will likely result in an error from the Spanner API. Consider using SERIALIZABLE or demonstrating this option in a read-only transaction context.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is not true. Cloud Spanner supports setting the isolation level to SERIALIZABLE or REPEATABLE_READ in both GSQL and PG dialects. It also supports setting the isolation level in both ReadWrite and ReadOnly transactions. It's a no-op to set it in ReadOnly transactions.

Comment on lines +67 to +69
'transactionOptions' => [
'readLockMode' => ReadLockMode::PESSIMISTIC
]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

The readLockMode option must be nested under the readWrite key within transactionOptions to correctly map to the underlying Protobuf structure (transaction_options.read_write.read_lock_mode). Without this nesting, the option will be ignored by the library.

        'transactionOptions' => [
            'readWrite' => [
                'readLockMode' => ReadLockMode::PESSIMISTIC
            ]
        ]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is necessary when setting it in the grpc API but not at the client-library level. See the unit tests for an example of setting readLockMode in this way.

Adds a snippet for setting the isolation level and another for setting
the read lock mode.

Both samples shows how to set the isolation level and read lock mode at
the client level, and how to set it at a transaction level.
@skuruppu skuruppu force-pushed the spanner-read-lock-mode-samples branch from 02f8a23 to a2d07ca Compare April 8, 2026 03:49
@coughytable
Copy link
Copy Markdown

coughytable commented Apr 11, 2026 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: spanner Issues related to the Spanner API. samples Issues that are directly related to samples.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants