Skip to content
This repository was archived by the owner on Mar 4, 2026. It is now read-only.

Commit 91c7204

Browse files
nginsberg-googlesurbhigarg92gcf-owl-bot[bot]
authored
feat(spanner): add a sample for max commit delays (#1993)
* Add a sample * update copywrite year * Comments. * Lint * Change to transaction.commit * Update test. * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Remove added lines from merge * Change SingerId * Add an integration test * Add a comment * fix regex --------- Co-authored-by: surbhigarg92 <surbhigarg.92@gmail.com> Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 4381047 commit 91c7204

4 files changed

Lines changed: 119 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-spanner/tre
139139
| Json-update-data | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/json-update-data.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/json-update-data.js,samples/README.md) |
140140
| Lists all databases on the selected instance | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/list-databases.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/list-databases.js,samples/README.md) |
141141
| Lists all the available instance configs for the selected project. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/list-instance-configs.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/list-instance-configs.js,samples/README.md) |
142+
| Executes request with max commit delay | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/max-commit-delay.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/max-commit-delay.js,samples/README.md) |
142143
| Numeric-add-column | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/numeric-add-column.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/numeric-add-column.js,samples/README.md) |
143144
| Numeric-query-parameter | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/numeric-query-parameter.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/numeric-query-parameter.js,samples/README.md) |
144145
| Numeric-update-data | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/numeric-update-data.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/numeric-update-data.js,samples/README.md) |

samples/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ and automatic, synchronous replication for high availability.
6464
* [Json-update-data](#json-update-data)
6565
* [Lists all databases on the selected instance](#lists-all-databases-on-the-selected-instance)
6666
* [Lists all the available instance configs for the selected project.](#lists-all-the-available-instance-configs-for-the-selected-project.)
67+
* [Executes request with max commit delay](#executes-request-with-max-commit-delay)
6768
* [Numeric-add-column](#numeric-add-column)
6869
* [Numeric-query-parameter](#numeric-query-parameter)
6970
* [Numeric-update-data](#numeric-update-data)
@@ -978,6 +979,23 @@ __Usage:__
978979

979980

980981

982+
### Executes request with max commit delay
983+
984+
View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/max-commit-delay.js).
985+
986+
[![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/max-commit-delay.js,samples/README.md)
987+
988+
__Usage:__
989+
990+
991+
`node max-commit-delay.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>`
992+
993+
994+
-----
995+
996+
997+
998+
981999
### Numeric-add-column
9821000

9831001
View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/numeric-add-column.js).

samples/max-commit-delay.js

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

samples/system-test/spanner.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,5 +1365,18 @@ describe('Autogenerated Admin Clients', () => {
13651365
);
13661366
assert.include(output, 'CREATE TABLE Singers');
13671367
});
1368+
1369+
// max_commit_delay
1370+
it('should update rows in Albums example table when max commit delay is set', async () => {
1371+
const output = execSync(
1372+
`node max-commit-delay.js "${INSTANCE_ID}" "${DATABASE_ID}" "${PROJECT_ID}"`
1373+
);
1374+
assert.match(
1375+
output,
1376+
new RegExp(
1377+
'Successfully inserted (\\d+) record into the Singers table.'
1378+
)
1379+
);
1380+
});
13681381
});
13691382
});

0 commit comments

Comments
 (0)