|
| 1 | +
|
| 2 | +/** |
| 3 | + * Copyright 2022 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/master/spanner/README.md |
| 22 | + */ |
| 23 | + |
| 24 | +namespace Google\Cloud\Samples\Spanner; |
| 25 | + |
| 26 | +// [START spanner_read_write_retry] |
| 27 | +use Google\Cloud\Spanner\SpannerClient; |
| 28 | +use Google\Cloud\Spanner\Transaction; |
| 29 | + |
| 30 | +/** |
| 31 | + * Performs a read-write transaction to update two sample records in the |
| 32 | + * database. if the transaction fails, it will retry for exactly $maxRetries times. |
| 33 | + * |
| 34 | + * Before running this sample, you will need to run the `update_data` sample |
| 35 | + * to populate the fields. |
| 36 | + * Example: |
| 37 | + * ``` |
| 38 | + * read_write_retry($instanceId, $databaseId); |
| 39 | + * ``` |
| 40 | + * |
| 41 | + * @param string $instanceId The Spanner instance ID. |
| 42 | + * @param string $databaseId The Spanner database ID. |
| 43 | + */ |
| 44 | +function read_write_retry(string $instanceId, string $databaseId): void |
| 45 | +{ |
| 46 | + $spanner = new SpannerClient(); |
| 47 | + $instance = $spanner->instance($instanceId); |
| 48 | + $database = $instance->database($databaseId); |
| 49 | + $maxRetries = 2; |
| 50 | + |
| 51 | + $database->runTransaction(function (Transaction $t) use ($spanner) { |
| 52 | + // Read the second album's budget. |
| 53 | + $secondAlbumKey = [2, 2]; |
| 54 | + $secondAlbumKeySet = $spanner->keySet(['keys' => [$secondAlbumKey]]); |
| 55 | + $secondAlbumResult = $t->read( |
| 56 | + 'Albums', |
| 57 | + $secondAlbumKeySet, |
| 58 | + ['MarketingBudget'], |
| 59 | + ['limit' => 1] |
| 60 | + ); |
| 61 | + $firstRow = $secondAlbumResult->rows()->current(); |
| 62 | + $secondAlbumBudget = $firstRow['MarketingBudget']; |
| 63 | + |
| 64 | + printf('Setting second album\'s budget as the first album\'s budget.' . PHP_EOL); |
| 65 | + |
| 66 | + // Update the row. |
| 67 | + $t->updateBatch('Albums', [ |
| 68 | + ['SingerId' => 1, 'AlbumId' => 1, 'MarketingBudget' => $secondAlbumBudget], |
| 69 | + ]); |
| 70 | + |
| 71 | + // Commit the transaction! |
| 72 | + $t->commit(); |
| 73 | + |
| 74 | + print('Transaction complete.' . PHP_EOL); |
| 75 | + }, ['maxRetries' => $maxRetries]); |
| 76 | +} |
| 77 | +// [END spanner_read_write_retry] |
| 78 | + |
| 79 | +// The following 2 lines are only needed to run the samples |
| 80 | +require_once __DIR__ . '/../../testing/sample_helpers.php'; |
| 81 | +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
0 commit comments