MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/nodejs-docs-samples / main

Function main

spanner/statement-timeout.js:22–68  ·  view source on GitHub ↗
(instanceId, databaseId, projectId)

Source from the content-addressed store, hash-verified

20'use strict';
21
22async function main(instanceId, databaseId, projectId) {
23 // [START spanner_set_statement_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 executeSqlWithTimeout() {
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 try {
45 await database.runTransactionAsync(async tx => {
46 // NOTE: You can use gaxOptions to set a custom timeout for a single RPC
47 // invocation. This timeout can however ONLY BE SHORTER than the default timeout
48 // for the RPC. If you set a timeout that is longer than the default timeout, then
49 // the default timeout will be used.
50 const query = {
51 sql: "INSERT INTO Singers (SingerId, FirstName, LastName) VALUES (110, 'George', 'Washington')",
52 gaxOptions: {
53 timeout: 60000, // 60 seconds timeout
54 },
55 };
56 const [, stats] = await tx.run(query);
57 console.log(`${stats.rowCountExact} record inserted.`);
58 await tx.commit();
59 });
60 } catch (err) {
61 console.error('ERROR:', err);
62 } finally {
63 await database.close();
64 }
65 }
66 await executeSqlWithTimeout();
67 // [END spanner_set_statement_timeout]
68}
69
70main(...process.argv.slice(2));

Callers 1

Calls 1

executeSqlWithTimeoutFunction · 0.85

Tested by

no test coverage detected