|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// sample-metadata: |
| 16 | +// title: Performs a read-write transaction with isolation level option |
| 17 | +// usage: node repeatable-reads.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID> |
| 18 | + |
| 19 | +'use strict'; |
| 20 | + |
| 21 | +function main( |
| 22 | + instanceId = 'my-instance', |
| 23 | + databaseId = 'my-database', |
| 24 | + projectId = 'my-project-id', |
| 25 | +) { |
| 26 | + // [START spanner_isolation_level] |
| 27 | + // Imports the Google Cloud Spanner client library |
| 28 | + const {Spanner, protos} = require('@google-cloud/spanner'); |
| 29 | + // The isolation level specified at the client-level will be applied |
| 30 | + // to all RW transactions. |
| 31 | + const isolationOptionsForClient = { |
| 32 | + defaultTransactionOptions: { |
| 33 | + isolationLevel: |
| 34 | + protos.google.spanner.v1.TransactionOptions.IsolationLevel.SERIALIZABLE, |
| 35 | + }, |
| 36 | + }; |
| 37 | + |
| 38 | + // Instantiates a client with defaultTransactionOptions |
| 39 | + const spanner = new Spanner({ |
| 40 | + projectId: projectId, |
| 41 | + defaultTransactionOptions: isolationOptionsForClient, |
| 42 | + }); |
| 43 | + |
| 44 | + function runTransactionWithIsolationLevel() { |
| 45 | + // Gets a reference to a Cloud Spanner instance and database |
| 46 | + const instance = spanner.instance(instanceId); |
| 47 | + const database = instance.database(databaseId); |
| 48 | + // The isolation level specified at the request level takes precedence over the isolation level configured at the client level. |
| 49 | + const isolationOptionsForTransaction = { |
| 50 | + isolationLevel: |
| 51 | + protos.google.spanner.v1.TransactionOptions.IsolationLevel |
| 52 | + .REPEATABLE_READ, |
| 53 | + }; |
| 54 | + |
| 55 | + database.runTransaction( |
| 56 | + isolationOptionsForTransaction, |
| 57 | + async (err, transaction) => { |
| 58 | + if (err) { |
| 59 | + console.error(err); |
| 60 | + return; |
| 61 | + } |
| 62 | + try { |
| 63 | + const query = |
| 64 | + 'SELECT AlbumTitle FROM Albums WHERE SingerId = 1 AND AlbumId = 1'; |
| 65 | + const results = await transaction.run(query); |
| 66 | + // Gets first album's title |
| 67 | + const rows = results[0].map(row => row.toJSON()); |
| 68 | + const albumTitle = rows[0].AlbumTitle; |
| 69 | + console.log(`previous album title ${albumTitle}`); |
| 70 | + |
| 71 | + const update = |
| 72 | + "UPDATE Albums SET AlbumTitle = 'New Album Title' WHERE SingerId = 1 AND AlbumId = 1"; |
| 73 | + const [rowCount] = await transaction.runUpdate(update); |
| 74 | + console.log( |
| 75 | + `Successfully updated ${rowCount} record in Albums table.`, |
| 76 | + ); |
| 77 | + await transaction.commit(); |
| 78 | + console.log( |
| 79 | + 'Successfully executed read-write transaction with isolationLevel option.', |
| 80 | + ); |
| 81 | + } catch (err) { |
| 82 | + console.error('ERROR:', err); |
| 83 | + } finally { |
| 84 | + transaction.end(); |
| 85 | + // Close the database when finished. |
| 86 | + await database.close(); |
| 87 | + } |
| 88 | + }, |
| 89 | + ); |
| 90 | + } |
| 91 | + runTransactionWithIsolationLevel(); |
| 92 | + // [END spanner_isolation_level] |
| 93 | +} |
| 94 | +process.on('unhandledRejection', err => { |
| 95 | + console.error(err.message); |
| 96 | + process.exitCode = 1; |
| 97 | +}); |
| 98 | +main(...process.argv.slice(2)); |
0 commit comments