Skip to content

Commit e8223b0

Browse files
authored
Merge branch 'main' into metrics_projectId
2 parents a04df7f + c1d7138 commit e8223b0

File tree

4 files changed

+135
-0
lines changed

4 files changed

+135
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-spanner/tre
209209
| Queryoptions | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/queryoptions.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/queryoptions.js,samples/README.md) |
210210
| Quickstart | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/quickstart.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/quickstart.js,samples/README.md) |
211211
| Read data with database role | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/read-data-with-database-role.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/read-data-with-database-role.js,samples/README.md) |
212+
| Performs a read-write transaction with isolation level option | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/repeatable-reads.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/repeatable-reads.js,samples/README.md) |
212213
| Sets a request tag for a single query | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/request-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/request-tag.js,samples/README.md) |
213214
| Run Batch update with RPC priority | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/rpc-priority-batch-dml.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/rpc-priority-batch-dml.js,samples/README.md) |
214215
| Run partitioned update with RPC priority | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/rpc-priority-partitioned-dml.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/rpc-priority-partitioned-dml.js,samples/README.md) |

samples/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ and automatic, synchronous replication for high availability.
116116
* [Queryoptions](#queryoptions)
117117
* [Quickstart](#quickstart)
118118
* [Read data with database role](#read-data-with-database-role)
119+
* [Performs a read-write transaction with isolation level option](#performs-a-read-write-transaction-with-isolation-level-option)
119120
* [Sets a request tag for a single query](#sets-a-request-tag-for-a-single-query)
120121
* [Run Batch update with RPC priority](#run-batch-update-with-rpc-priority)
121122
* [Run partitioned update with RPC priority](#run-partitioned-update-with-rpc-priority)
@@ -1888,6 +1889,23 @@ __Usage:__
18881889

18891890

18901891

1892+
### Performs a read-write transaction with isolation level option
1893+
1894+
View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/repeatable-reads.js).
1895+
1896+
[![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/repeatable-reads.js,samples/README.md)
1897+
1898+
__Usage:__
1899+
1900+
1901+
`node repeatable-reads.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>`
1902+
1903+
1904+
-----
1905+
1906+
1907+
1908+
18911909
### Sets a request tag for a single query
18921910

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

samples/repeatable-reads.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Copyright 2025 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+
15+
// sample-metadata:
16+
// title: Performs a read-write transaction with isolation level option
17+
// usage: node repeatable-reads.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
18+
19+
'use strict';
20+
21+
function main(
22+
instanceId = 'my-instance',
23+
databaseId = 'my-database',
24+
projectId = 'my-project-id',
25+
) {
26+
// [START spanner_isolation_level]
27+
// Imports the Google Cloud Spanner client library
28+
const {Spanner, protos} = require('@google-cloud/spanner');
29+
// The isolation level specified at the client-level will be applied
30+
// to all RW transactions.
31+
const isolationOptionsForClient = {
32+
defaultTransactionOptions: {
33+
isolationLevel:
34+
protos.google.spanner.v1.TransactionOptions.IsolationLevel.SERIALIZABLE,
35+
},
36+
};
37+
38+
// Instantiates a client with defaultTransactionOptions
39+
const spanner = new Spanner({
40+
projectId: projectId,
41+
defaultTransactionOptions: isolationOptionsForClient,
42+
});
43+
44+
function runTransactionWithIsolationLevel() {
45+
// Gets a reference to a Cloud Spanner instance and database
46+
const instance = spanner.instance(instanceId);
47+
const database = instance.database(databaseId);
48+
// The isolation level specified at the request level takes precedence over the isolation level configured at the client level.
49+
const isolationOptionsForTransaction = {
50+
isolationLevel:
51+
protos.google.spanner.v1.TransactionOptions.IsolationLevel
52+
.REPEATABLE_READ,
53+
};
54+
55+
database.runTransaction(
56+
isolationOptionsForTransaction,
57+
async (err, transaction) => {
58+
if (err) {
59+
console.error(err);
60+
return;
61+
}
62+
try {
63+
const query =
64+
'SELECT AlbumTitle FROM Albums WHERE SingerId = 1 AND AlbumId = 1';
65+
const results = await transaction.run(query);
66+
// Gets first album's title
67+
const rows = results[0].map(row => row.toJSON());
68+
const albumTitle = rows[0].AlbumTitle;
69+
console.log(`previous album title ${albumTitle}`);
70+
71+
const update =
72+
"UPDATE Albums SET AlbumTitle = 'New Album Title' WHERE SingerId = 1 AND AlbumId = 1";
73+
const [rowCount] = await transaction.runUpdate(update);
74+
console.log(
75+
`Successfully updated ${rowCount} record in Albums table.`,
76+
);
77+
await transaction.commit();
78+
console.log(
79+
'Successfully executed read-write transaction with isolationLevel option.',
80+
);
81+
} catch (err) {
82+
console.error('ERROR:', err);
83+
} finally {
84+
transaction.end();
85+
// Close the database when finished.
86+
await database.close();
87+
}
88+
},
89+
);
90+
}
91+
runTransactionWithIsolationLevel();
92+
// [END spanner_isolation_level]
93+
}
94+
process.on('unhandledRejection', err => {
95+
console.error(err.message);
96+
process.exitCode = 1;
97+
});
98+
main(...process.argv.slice(2));

samples/system-test/spanner.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,6 +1287,24 @@ describe('Autogenerated Admin Clients', () => {
12871287
assert.match(output, /VenueId: 19, Details: {"open":true,"rating":9}/);
12881288
});
12891289

1290+
// isolation_level_option
1291+
it('should run read-write transaction with isolation level option set', () => {
1292+
const output = execSync(
1293+
`node repeatable-reads.js ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`,
1294+
);
1295+
assert.match(output, new RegExp('previous album title Total Junk'));
1296+
assert.match(
1297+
output,
1298+
new RegExp('Successfully updated 1 record in Albums table.'),
1299+
);
1300+
assert.match(
1301+
output,
1302+
new RegExp(
1303+
'Successfully executed read-write transaction with isolationLevel option.',
1304+
),
1305+
);
1306+
});
1307+
12901308
// add_and_drop_new_database_role
12911309
it('should add and drop new database roles', async () => {
12921310
const output = execSync(

0 commit comments

Comments
 (0)