Skip to content

Commit 3c3db13

Browse files
chore: add sample for transaction timeout configuration (#2308)
* chore: add sample for transaction timeout configuration * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent e0ef6d8 commit 3c3db13

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-spanner/tre
208208
| Drops a foreign key constraint with delete cascade action | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/table-drop-foreign-key-constraint-delete-cascade.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/table-drop-foreign-key-constraint-delete-cascade.js,samples/README.md) |
209209
| Timestamp | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/timestamp.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/timestamp.js,samples/README.md) |
210210
| Executes a read/write transaction with transaction and request tags | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/transaction-tag.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/transaction-tag.js,samples/README.md) |
211+
| Executes a read/write transaction with transaction timeout | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/transaction-timeout.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/transaction-timeout.js,samples/README.md) |
211212
| Transaction | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/transaction.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/transaction.js,samples/README.md) |
212213
| Updates a backup schedule | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/update-backup-schedule.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/update-backup-schedule.js,samples/README.md) |
213214
| Updates an instance. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/update-instance-default-backup-schedule-type.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/update-instance-default-backup-schedule-type.js,samples/README.md) |

samples/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ and automatic, synchronous replication for high availability.
133133
* [Drops a foreign key constraint with delete cascade action](#drops-a-foreign-key-constraint-with-delete-cascade-action)
134134
* [Timestamp](#timestamp)
135135
* [Executes a read/write transaction with transaction and request tags](#executes-a-read/write-transaction-with-transaction-and-request-tags)
136+
* [Executes a read/write transaction with transaction timeout](#executes-a-read/write-transaction-with-transaction-timeout)
136137
* [Transaction](#transaction)
137138
* [Updates a backup schedule](#updates-a-backup-schedule)
138139
* [Updates an instance.](#updates-an-instance.)
@@ -2175,6 +2176,23 @@ __Usage:__
21752176

21762177

21772178

2179+
### Executes a read/write transaction with transaction timeout
2180+
2181+
View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/transaction-timeout.js).
2182+
2183+
[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/transaction-timeout.js,samples/README.md)
2184+
2185+
__Usage:__
2186+
2187+
2188+
`node transaction-timeout.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>`
2189+
2190+
2191+
-----
2192+
2193+
2194+
2195+
21782196
### Transaction
21792197

21802198
View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/transaction.js).

samples/system-test/spanner.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const rpcPriorityQueryPartitionsCommand =
3636
'node rpc-priority-query-partitions.js';
3737
const transactionCmd = 'node transaction.js';
3838
const transactionTagCommand = 'node transaction-tag.js';
39+
const transactionTimeoutCommand = 'node transaction-timeout.js';
3940
const requestTagCommand = 'node request-tag.js';
4041
const timestampCmd = 'node timestamp.js';
4142
const structCmd = 'node struct.js';
@@ -1237,6 +1238,14 @@ describe('Autogenerated Admin Clients', () => {
12371238
assert.include(output, 'Inserted new outdoor venue');
12381239
});
12391240

1241+
// read_write_transaction with transaction timeout
1242+
it('should execute a read/write transaction with a transaction timeout of 60 seconds', async () => {
1243+
const output = execSync(
1244+
`${transactionTimeoutCommand} ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`,
1245+
);
1246+
assert.include(output, '1 record inserted.');
1247+
});
1248+
12401249
// add_json_column
12411250
it('should add a VenueDetails column to Venues example table', async () => {
12421251
const output = execSync(

samples/transaction-timeout.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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

Comments
 (0)