File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,15 @@ public Struct getCurrentRowAsStruct() {
6161

6262
@Override
6363
public void close() {
64-
delegate.get().close();
64+
ResultSet rs;
65+
try {
66+
rs = delegate.get();
67+
} catch (Exception e) {
68+
// Ignore any exceptions when getting the underlying result set, as that means that there is
69+
// nothing that needs to be closed.
70+
return;
71+
}
72+
rs.close();
6573
}
6674

6775
@Override
Original file line numberDiff line numberDiff line change
@@ -1255,7 +1255,10 @@ public void close() {
12551255
Exception = null;
12561256
checkedOutSessions.remove(this);
12571257
}
1258-
get().close();
1258+
PooledSession delegate = getOrNull();
1259+
if (delegate != null) {
1260+
delegate.close();
1261+
}
12591262
}
12601263

12611264
@Override
@@ -1264,7 +1267,19 @@ public ApiFuture<Empty> asyncClose() {
12641267
Exception = null;
12651268
checkedOutSessions.remove(this);
12661269
}
1267-
return get().asyncClose();
1270+
PooledSession delegate = getOrNull();
1271+
if (delegate != null) {
1272+
return delegate.asyncClose();
1273+
}
1274+
return ApiFutures.immediateFuture(Empty.getDefaultInstance());
1275+
}
1276+
1277+
private PooledSession getOrNull() {
1278+
try {
1279+
return get();
1280+
} catch (Throwable t) {
1281+
return null;
1282+
}
12681283
}
12691284

12701285
@Override
Original file line numberDiff line numberDiff line change
@@ -1599,4 +1599,31 @@ public Long run(TransactionContext transaction) throws Exception {
15991599
}
16001600
});
16011601
}
1602+
1603+
@Test
1604+
public void testBatchCreateSessionsFailure_shouldNotPropagateToCloseMethod() {
1605+
try {
1606+
// Simulate session creation failures on the backend.
1607+
mockSpanner.setBatchCreateSessionsExecutionTime(
1608+
SimulatedExecutionTime.ofStickyException(Status.RESOURCE_EXHAUSTED.asRuntimeException()));
1609+
DatabaseClient client =
1610+
spannerWithEmptySessionPool.getDatabaseClient(
1611+
DatabaseId.of(TEST_PROJECT, TEST_INSTANCE, TEST_DATABASE));
1612+
// This will not cause any failure as getting a session from the pool is guaranteed to be
1613+
// non-blocking, and any exceptions will be delayed until actual query execution.
1614+
ResultSet rs = client.singleUse().executeQuery(SELECT1);
1615+
try {
1616+
while (rs.next()) {
1617+
fail("Missing expected exception");
1618+
}
1619+
} catch (SpannerException e) {
1620+
assertThat(e.getErrorCode()).isEqualTo(ErrorCode.RESOURCE_EXHAUSTED);
1621+
} finally {
1622+
// This should not cause any failures.
1623+
rs.close();
1624+
}
1625+
} finally {
1626+
mockSpanner.setBatchCreateSessionsExecutionTime(SimulatedExecutionTime.none());
1627+
}
1628+
}
16021629
}

0 commit comments

Comments
 (0)