Skip to content

Commit 02f8a23

Browse files
committed
docs(spanner): snippets to set transaction options
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.
1 parent 685ddf2 commit 02f8a23

File tree

3 files changed

+172
-0
lines changed

3 files changed

+172
-0
lines changed

spanner/src/isolation_level.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
/**
3+
* Copyright 2026 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/spanner/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Spanner;
25+
26+
// [START spanner_isolation_level]
27+
use Google\Cloud\Spanner\SpannerClient;
28+
use Google\Cloud\Spanner\Transaction;
29+
use Google\Cloud\Spanner\V1\TransactionOptions\IsolationLevel;
30+
31+
/**
32+
* Shows how to run a Read Write transaction with isolation level options.
33+
*
34+
* Example:
35+
* ```
36+
* isolation_level($instanceId, $databaseId);
37+
* ```
38+
*
39+
* @param string $instanceId The Spanner instance ID.
40+
* @param string $databaseId The Spanner database ID.
41+
*/
42+
function isolation_level(string $instanceId, string $databaseId): void
43+
{
44+
// The isolation level specified at the client-level will be applied to all
45+
// RW transactions.
46+
$spanner = new SpannerClient([
47+
'defaultTransactionOptions' => [
48+
'isolationLevel' => IsolationLevel::SERIALIZABLE
49+
]
50+
]);
51+
$instance = $spanner->instance($instanceId);
52+
$database = $instance->database($databaseId);
53+
54+
// The isolation level specified at the request level takes precedence over
55+
// the isolation level configured at the client level.
56+
$database->runTransaction(function (Transaction $t) {
57+
// Read an AlbumTitle.
58+
$results = $t->execute('SELECT AlbumTitle from Albums WHERE SingerId = 1 and AlbumId = 1');
59+
foreach ($results as $row) {
60+
printf('Current Album Title: %s' . PHP_EOL, $row['AlbumTitle']);
61+
}
62+
63+
// Update the AlbumTitle.
64+
$rowCount = $t->executeUpdate('UPDATE Albums SET AlbumTitle = \'A New Title\' WHERE SingerId = 1 and AlbumId = 1');
65+
printf('%d record(s) updated.' . PHP_EOL, $rowCount);
66+
}, [
67+
'transactionOptions' => [
68+
'isolationLevel' => IsolationLevel::REPEATABLE_READ
69+
]
70+
]);
71+
}
72+
// [END spanner_isolation_level]
73+
74+
// The following 2 lines are only needed to run the samples
75+
require_once __DIR__ . '/../../testing/sample_helpers.php';
76+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

spanner/src/read_lock_mode.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
/**
3+
* Copyright 2026 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/spanner/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Spanner;
25+
26+
// [START spanner_read_lock_mode]
27+
use Google\Cloud\Spanner\SpannerClient;
28+
use Google\Cloud\Spanner\Transaction;
29+
use Google\Cloud\Spanner\V1\TransactionOptions\ReadWrite\ReadLockMode;
30+
31+
/**
32+
* Shows how to run a Read Write transaction with read lock mode options.
33+
*
34+
* Example:
35+
* ```
36+
* read_lock_mode($instanceId, $databaseId);
37+
* ```
38+
*
39+
* @param string $instanceId The Spanner instance ID.
40+
* @param string $databaseId The Spanner database ID.
41+
*/
42+
function read_lock_mode(string $instanceId, string $databaseId): void
43+
{
44+
// The read lock mode specified at the client-level will be applied to all
45+
// RW transactions.
46+
$spanner = new SpannerClient([
47+
'defaultTransactionOptions' => [
48+
'readLockMode' => ReadLockMode::OPTIMISTIC
49+
]
50+
]);
51+
$instance = $spanner->instance($instanceId);
52+
$database = $instance->database($databaseId);
53+
54+
// The read lock mode specified at the request level takes precedence over
55+
// the read lock mode configured at the client level.
56+
$database->runTransaction(function (Transaction $t) {
57+
// Read an AlbumTitle.
58+
$results = $t->execute('SELECT AlbumTitle from Albums WHERE SingerId = 2 and AlbumId = 1');
59+
foreach ($results as $row) {
60+
printf('Current Album Title: %s' . PHP_EOL, $row['AlbumTitle']);
61+
}
62+
63+
// Update the AlbumTitle.
64+
$rowCount = $t->executeUpdate('UPDATE Albums SET AlbumTitle = \'A New Title\' WHERE SingerId = 2 and AlbumId = 1');
65+
printf('%d record(s) updated.' . PHP_EOL, $rowCount);
66+
}, [
67+
'transactionOptions' => [
68+
'readLockMode' => ReadLockMode::PESSIMISTIC
69+
]
70+
]);
71+
}
72+
// [END spanner_read_lock_mode]
73+
74+
// The following 2 lines are only needed to run the samples
75+
require_once __DIR__ . '/../../testing/sample_helpers.php';
76+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

spanner/test/spannerTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,26 @@ public function testReadWriteTransaction()
492492
$this->assertStringContainsString('Transaction complete.', $output);
493493
}
494494

495+
/**
496+
* @depends testUpdateData
497+
*/
498+
public function testReadLockMode()
499+
{
500+
$output = $this->runFunctionSnippet('read_lock_mode');
501+
$this->assertStringContainsString('Current Album Title:', $output);
502+
$this->assertStringContainsString('record(s) updated.', $output);
503+
}
504+
505+
/**
506+
* @depends testUpdateData
507+
*/
508+
public function testIsolationLevel()
509+
{
510+
$output = $this->runFunctionSnippet('isolation_level');
511+
$this->assertStringContainsString('Current Album Title:', $output);
512+
$this->assertStringContainsString('record(s) updated.', $output);
513+
}
514+
495515
/**
496516
* @depends testAddColumn
497517
*/

0 commit comments

Comments
 (0)