@@ -679,24 +679,25 @@ static void readOnlyTransaction(DatabaseClient dbClient) {
679
679
// ReadOnlyTransaction must be closed by calling close() on it to release resources held by it.
680
680
// We use a try-with-resource block to automatically do so.
681
681
try (ReadOnlyTransaction transaction = dbClient .readOnlyTransaction ()) {
682
- ResultSet queryResultSet =
682
+ try ( ResultSet queryResultSet =
683
683
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
690
691
try (ResultSet readResultSet =
691
692
transaction .read (
692
- "Albums" , KeySet .all (), Arrays .asList ("SingerId" , "AlbumId" , "AlbumTitle" ))) {
693
+ "Albums" , KeySet .all (), Arrays .asList ("SingerId" , "AlbumId" , "AlbumTitle" ))) {
693
694
while (readResultSet .next ()) {
694
695
System .out .printf (
695
696
"%d %d %s\n " ,
696
697
readResultSet .getLong (0 ), readResultSet .getLong (1 ), readResultSet .getString (2 ));
697
698
}
698
- }
699
- }
699
+ } // readResultSet.close() is automatically called here
700
+ } // transaction.close() is automatically called here
700
701
}
701
702
// [END spanner_read_only_transaction]
702
703
0 commit comments