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

Function main

spanner/transaction-timeout.js:22–73  ·  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_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 const jsonRow = result.toJSON();
55 console.log(
56 `SingerId: ${jsonRow.SingerId}, FirstName: ${jsonRow.FirstName}, LastName: ${jsonRow.LastName}`
57 );
58 });
59 const sql =
60 "INSERT INTO Singers (SingerId, FirstName, LastName) VALUES (100, 'George', 'Washington')";
61 const [rowCount] = await tx.runUpdate(sql);
62 console.log(`${rowCount} record inserted.`);
63 await tx.commit();
64 });
65 } catch (err) {
66 console.error('ERROR:', err);
67 } finally {
68 await database.close();
69 }
70 }
71 await executeTransactionWithTimeout();
72 // [END spanner_transaction_timeout]
73}
74
75main(...process.argv.slice(2));

Callers 1

Calls 1

Tested by

no test coverage detected