Skip to content

Commit 5dc3e19

Browse files
rajatbhattagcf-owl-bot[bot]olavloite
authored
fix: do not delete session in close method for BatchReadOnlyTransactionImpl (#1688)
* fix: do not delete session in close method for BatchReadOnlyTransactionImpl - remove session.close() from close() method implementation in BatchReadOnlyTransactionImpl class. - add a cleanup() method to BatchReadOnlyTransaction interface and give user the control to delete session when the session is no longer in use. - add tests for txn.cleanup() method. * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Incorporate review comments Co-authored-by: Knut Olav Løite <[email protected]> * Incorporate review comments. Co-authored-by: Knut Olav Løite <[email protected]> * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Knut Olav Løite <[email protected]>
1 parent b8da725 commit 5dc3e19

File tree

4 files changed

+33
-10
lines changed

4 files changed

+33
-10
lines changed

google-cloud-spanner/clirr-ignored-differences.xml

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
<className>com/google/cloud/spanner/connection/Connection</className>
77
<method>com.google.cloud.spanner.Dialect getDialect()</method>
88
</difference>
9+
<difference>
10+
<differenceType>7012</differenceType>
11+
<className>com/google/cloud/spanner/BatchReadOnlyTransaction</className>
12+
<method>void cleanup()</method>
13+
</difference>
914
<difference>
1015
<differenceType>8001</differenceType>
1116
<className>com/google/cloud/spanner/connection/StatementParser</className>

google-cloud-spanner/src/main/java/com/google/cloud/spanner/BatchClientImpl.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,14 @@ public ResultSet execute(Partition partition) throws SpannerException {
214214
partition.getPartitionToken());
215215
}
216216

217+
/**
218+
* Closes the session as part of the cleanup. It is the responsibility of the caller to make a
219+
* call to this method once the transaction completes execution across all the channels (which
220+
* is understandably hard to identify). It is okay if the caller does not call the method
221+
* because the backend will anyways clean up the unused session.
222+
*/
217223
@Override
218-
public void close() {
219-
super.close();
224+
public void cleanup() {
220225
session.close();
221226
}
222227
}

google-cloud-spanner/src/main/java/com/google/cloud/spanner/BatchReadOnlyTransaction.java

+8
Original file line numberDiff line numberDiff line change
@@ -197,4 +197,12 @@ List<Partition> partitionQuery(
197197
* BatchTransactionId guarantees the subsequent read/query to be executed at the same timestamp.
198198
*/
199199
BatchTransactionId getBatchTransactionId();
200+
201+
/**
202+
* Closes the session as part of the cleanup. It is the responsibility of the caller to make a
203+
* call to this method once the transaction completes execution across all the channels (which is
204+
* understandably hard to identify). It is okay if the caller does not call the method because the
205+
* backend will anyways clean up the unused session.
206+
*/
207+
default void cleanup() {}
200208
}

google-cloud-spanner/src/test/java/com/google/cloud/spanner/DatabaseClientImplTest.java

+13-8
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import com.google.common.util.concurrent.SettableFuture;
5555
import com.google.protobuf.AbstractMessage;
5656
import com.google.spanner.v1.CommitRequest;
57+
import com.google.spanner.v1.DeleteSessionRequest;
5758
import com.google.spanner.v1.ExecuteBatchDmlRequest;
5859
import com.google.spanner.v1.ExecuteSqlRequest;
5960
import com.google.spanner.v1.ExecuteSqlRequest.QueryMode;
@@ -1630,16 +1631,20 @@ public void testBackendPartitionQueryOptions() {
16301631
try (ResultSet rs = transaction.execute(partitions.get(0))) {
16311632
// Just iterate over the results to execute the query.
16321633
while (rs.next()) {}
1634+
} finally {
1635+
transaction.cleanup();
16331636
}
1634-
// Check that the last query was executed using a custom optimizer version and statistics
1635-
// package.
1637+
// Check if the last query executed is a DeleteSessionRequest and the second last query
1638+
// executed is a ExecuteSqlRequest and was executed using a custom optimizer version and
1639+
// statistics package.
16361640
List<AbstractMessage> requests = mockSpanner.getRequests();
1637-
assertThat(requests).isNotEmpty();
1638-
assertThat(requests.get(requests.size() - 1)).isInstanceOf(ExecuteSqlRequest.class);
1639-
ExecuteSqlRequest request = (ExecuteSqlRequest) requests.get(requests.size() - 1);
1640-
assertThat(request.getQueryOptions()).isNotNull();
1641-
assertThat(request.getQueryOptions().getOptimizerVersion()).isEqualTo("1");
1642-
assertThat(request.getQueryOptions().getOptimizerStatisticsPackage())
1641+
assert requests.size() >= 2 : "required to have at least 2 requests";
1642+
assertThat(requests.get(requests.size() - 1)).isInstanceOf(DeleteSessionRequest.class);
1643+
assertThat(requests.get(requests.size() - 2)).isInstanceOf(ExecuteSqlRequest.class);
1644+
ExecuteSqlRequest executeSqlRequest = (ExecuteSqlRequest) requests.get(requests.size() - 2);
1645+
assertThat(executeSqlRequest.getQueryOptions()).isNotNull();
1646+
assertThat(executeSqlRequest.getQueryOptions().getOptimizerVersion()).isEqualTo("1");
1647+
assertThat(executeSqlRequest.getQueryOptions().getOptimizerStatisticsPackage())
16431648
.isEqualTo("custom-package");
16441649
}
16451650
}

0 commit comments

Comments
 (0)