|
| 1 | +// Copyright 2024 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 | +// sample-metadata: |
| 15 | +// title: Executes request with max commit delay |
| 16 | +// usage: node max-commit-delay.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID> |
| 17 | + |
| 18 | +'use strict'; |
| 19 | + |
| 20 | +function main( |
| 21 | + instanceId = 'my-instance', |
| 22 | + databaseId = 'my-database', |
| 23 | + projectId = 'my-project-id' |
| 24 | +) { |
| 25 | + // [START spanner_set_max_commit_delay] |
| 26 | + // Imports the Google Cloud client library. |
| 27 | + const {Spanner, protos} = require('@google-cloud/spanner'); |
| 28 | + |
| 29 | + /** |
| 30 | + * TODO(developer): Uncomment the following lines before running the sample. |
| 31 | + */ |
| 32 | + // const projectId = 'my-project-id'; |
| 33 | + // const instanceId = 'my-instance'; |
| 34 | + // const databaseId = 'my-database'; |
| 35 | + |
| 36 | + // Creates a client. |
| 37 | + const spanner = new Spanner({ |
| 38 | + projectId: projectId, |
| 39 | + }); |
| 40 | + |
| 41 | + async function spannerSetMaxCommitDelay() { |
| 42 | + // Gets a reference to a Cloud Spanner instance and database. |
| 43 | + const instance = spanner.instance(instanceId); |
| 44 | + const database = instance.database(databaseId); |
| 45 | + |
| 46 | + database.runTransaction(async (err, transaction) => { |
| 47 | + if (err) { |
| 48 | + console.error(err); |
| 49 | + return; |
| 50 | + } |
| 51 | + try { |
| 52 | + const [rowCount] = await transaction.runUpdate({ |
| 53 | + sql: 'INSERT Singers (SingerId, FirstName, LastName) VALUES (111, @firstName, @lastName)', |
| 54 | + params: { |
| 55 | + firstName: 'Virginia', |
| 56 | + lastName: 'Watson', |
| 57 | + }, |
| 58 | + }); |
| 59 | + |
| 60 | + console.log( |
| 61 | + `Successfully inserted ${rowCount} record into the Singers table.` |
| 62 | + ); |
| 63 | + |
| 64 | + await transaction.commit({ |
| 65 | + // The maximum amount of time to delay the transaction to improve |
| 66 | + // throughput. |
| 67 | + maxCommitDelay: protos.google.protobuf.Duration({ |
| 68 | + seconds: 0, // 0 seconds |
| 69 | + nanos: 100000000, // 100,000,000 nanoseconds = 100 milliseconds |
| 70 | + }), |
| 71 | + }); |
| 72 | + } catch (err) { |
| 73 | + console.error('ERROR:', err); |
| 74 | + } finally { |
| 75 | + // Close the database when finished. |
| 76 | + database.close(); |
| 77 | + } |
| 78 | + }); |
| 79 | + } |
| 80 | + spannerSetMaxCommitDelay(); |
| 81 | + // [END spanner_set_max_commit_delay] |
| 82 | +} |
| 83 | +process.on('unhandledRejection', err => { |
| 84 | + console.error(err.message); |
| 85 | + process.exitCode = 1; |
| 86 | +}); |
| 87 | +main(...process.argv.slice(2)); |
0 commit comments