diff --git a/spanner/src/isolation_level.php b/spanner/src/isolation_level.php new file mode 100644 index 000000000..778d101b8 --- /dev/null +++ b/spanner/src/isolation_level.php @@ -0,0 +1,74 @@ + IsolationLevel::SERIALIZABLE + ]); + $instance = $spanner->instance($instanceId); + $database = $instance->database($databaseId); + + // The isolation level specified at the request level takes precedence over + // the isolation level configured at the client level. + $database->runTransaction(function (Transaction $t) { + // Read an AlbumTitle. + $results = $t->execute('SELECT AlbumTitle from Albums WHERE SingerId = 1 and AlbumId = 1'); + foreach ($results as $row) { + printf('Current Album Title: %s' . PHP_EOL, $row['AlbumTitle']); + } + + // Update the AlbumTitle. + $rowCount = $t->executeUpdate('UPDATE Albums SET AlbumTitle = \'A New Title\' WHERE SingerId = 1 and AlbumId = 1'); + printf('%d record(s) updated.' . PHP_EOL, $rowCount); + }, [ + 'transactionOptions' => [ + 'isolationLevel' => IsolationLevel::REPEATABLE_READ + ] + ]); +} +// [END spanner_isolation_level] + +// The following 2 lines are only needed to run the samples +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/spanner/src/read_lock_mode.php b/spanner/src/read_lock_mode.php new file mode 100644 index 000000000..1e3bf115a --- /dev/null +++ b/spanner/src/read_lock_mode.php @@ -0,0 +1,74 @@ + ReadLockMode::OPTIMISTIC + ]); + $instance = $spanner->instance($instanceId); + $database = $instance->database($databaseId); + + // The read lock mode specified at the request level takes precedence over + // the read lock mode configured at the client level. + $database->runTransaction(function (Transaction $t) { + // Read an AlbumTitle. + $results = $t->execute('SELECT AlbumTitle from Albums WHERE SingerId = 2 and AlbumId = 1'); + foreach ($results as $row) { + printf('Current Album Title: %s' . PHP_EOL, $row['AlbumTitle']); + } + + // Update the AlbumTitle. + $rowCount = $t->executeUpdate('UPDATE Albums SET AlbumTitle = \'A New Title\' WHERE SingerId = 2 and AlbumId = 1'); + printf('%d record(s) updated.' . PHP_EOL, $rowCount); + }, [ + 'transactionOptions' => [ + 'readLockMode' => ReadLockMode::PESSIMISTIC + ] + ]); +} +// [END spanner_read_lock_mode] + +// The following 2 lines are only needed to run the samples +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/spanner/test/spannerTest.php b/spanner/test/spannerTest.php index 548042e6c..0b86844ce 100644 --- a/spanner/test/spannerTest.php +++ b/spanner/test/spannerTest.php @@ -492,6 +492,26 @@ public function testReadWriteTransaction() $this->assertStringContainsString('Transaction complete.', $output); } + /** + * @depends testUpdateData + */ + public function testReadLockMode() + { + $output = $this->runFunctionSnippet('read_lock_mode'); + $this->assertStringContainsString('Current Album Title:', $output); + $this->assertStringContainsString('record(s) updated.', $output); + } + + /** + * @depends testUpdateData + */ + public function testIsolationLevel() + { + $output = $this->runFunctionSnippet('isolation_level'); + $this->assertStringContainsString('Current Album Title:', $output); + $this->assertStringContainsString('record(s) updated.', $output); + } + /** * @depends testAddColumn */