Skip to content

Commit 7bfc62d

Browse files
docs: Update SpannerSample.java to align with best practices (#3625)
* Update SpannerSample.java Encapsulates both statements in a try-with-resources statement * Update SpannerSample.java * Update SpannerSample.java Fix spacing linter errors * Update SpannerSample.java Clear Linter errors for indent --------- Co-authored-by: Sakthivel Subramanian <[email protected]>
1 parent 43ea4fa commit 7bfc62d

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

samples/snippets/src/main/java/com/example/spanner/SpannerSample.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -679,24 +679,25 @@ static void readOnlyTransaction(DatabaseClient dbClient) {
679679
// ReadOnlyTransaction must be closed by calling close() on it to release resources held by it.
680680
// We use a try-with-resource block to automatically do so.
681681
try (ReadOnlyTransaction transaction = dbClient.readOnlyTransaction()) {
682-
ResultSet queryResultSet =
682+
try (ResultSet queryResultSet =
683683
transaction.executeQuery(
684-
Statement.of("SELECT SingerId, AlbumId, AlbumTitle FROM Albums"));
685-
while (queryResultSet.next()) {
686-
System.out.printf(
687-
"%d %d %s\n",
688-
queryResultSet.getLong(0), queryResultSet.getLong(1), queryResultSet.getString(2));
689-
}
684+
Statement.of("SELECT SingerId, AlbumId, AlbumTitle FROM Albums"))) {
685+
while (queryResultSet.next()) {
686+
System.out.printf(
687+
"%d %d %s\n",
688+
queryResultSet.getLong(0), queryResultSet.getLong(1), queryResultSet.getString(2));
689+
}
690+
} // queryResultSet.close() is automatically called here
690691
try (ResultSet readResultSet =
691692
transaction.read(
692-
"Albums", KeySet.all(), Arrays.asList("SingerId", "AlbumId", "AlbumTitle"))) {
693+
"Albums", KeySet.all(), Arrays.asList("SingerId", "AlbumId", "AlbumTitle"))) {
693694
while (readResultSet.next()) {
694695
System.out.printf(
695696
"%d %d %s\n",
696697
readResultSet.getLong(0), readResultSet.getLong(1), readResultSet.getString(2));
697698
}
698-
}
699-
}
699+
} // readResultSet.close() is automatically called here
700+
} // transaction.close() is automatically called here
700701
}
701702
// [END spanner_read_only_transaction]
702703

0 commit comments

Comments
 (0)