|
| 1 | +/** |
| 2 | + * Copyright 2025 Google LLC |
| 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 | + |
| 16 | +// sample-metadata: |
| 17 | +// title: Executes a read/write transaction with transaction timeout |
| 18 | +// usage: node transaction-timeout.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID> |
| 19 | + |
| 20 | +'use strict'; |
| 21 | + |
| 22 | +function main(instanceId, databaseId, projectId) { |
| 23 | + // [START spanner_transaction_timeout] |
| 24 | + /** |
| 25 | + * TODO(developer): Uncomment the following lines before running the sample. |
| 26 | + */ |
| 27 | + // const projectId = 'my-project-id'; |
| 28 | + // const instanceId = 'my-instance'; |
| 29 | + // const databaseId = 'my-database'; |
| 30 | + |
| 31 | + // Imports the Google Cloud client library |
| 32 | + const {Spanner} = require('@google-cloud/spanner'); |
| 33 | + |
| 34 | + // Creates a client |
| 35 | + const spanner = new Spanner({ |
| 36 | + projectId: projectId, |
| 37 | + }); |
| 38 | + |
| 39 | + async function executeTransactionWithTimeout() { |
| 40 | + // Gets a reference to a Cloud Spanner instance and database. |
| 41 | + const instance = spanner.instance(instanceId); |
| 42 | + const database = instance.database(databaseId); |
| 43 | + |
| 44 | + const options = { |
| 45 | + timeout: 60000, // 60 seconds timeout |
| 46 | + }; |
| 47 | + |
| 48 | + try { |
| 49 | + await database.runTransactionAsync(options, async tx => { |
| 50 | + const [results] = await tx.run( |
| 51 | + 'SELECT SingerId, FirstName, LastName FROM Singers ORDER BY LastName, FirstName', |
| 52 | + ); |
| 53 | + results.forEach(result => { |
| 54 | + console.log( |
| 55 | + `${result[0].name}: ${result[0].value.value}, ${result[1].name}: ${result[1].value}, ${result[2].name}: ${result[2].value}`, |
| 56 | + ); |
| 57 | + }); |
| 58 | + const sql = |
| 59 | + "INSERT INTO Singers (SingerId, FirstName, LastName) VALUES (100, 'George', 'Washington')"; |
| 60 | + const [rowCount] = await tx.runUpdate(sql); |
| 61 | + console.log(`${rowCount} record inserted.`); |
| 62 | + await tx.commit(); |
| 63 | + }); |
| 64 | + } catch (err) { |
| 65 | + console.error('ERROR:', err); |
| 66 | + } finally { |
| 67 | + await database.close(); |
| 68 | + } |
| 69 | + } |
| 70 | + executeTransactionWithTimeout(); |
| 71 | + // [END spanner_transaction_timeout] |
| 72 | +} |
| 73 | +process.on('unhandledRejection', err => { |
| 74 | + console.error(err.message); |
| 75 | + process.exitCode = 1; |
| 76 | +}); |
| 77 | +main(...process.argv.slice(2)); |
0 commit comments