@@ -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