Skip to content

Commit 854482a

Browse files
authored
Merge branch 'main' into main
2 parents 6c46a98 + e0a3e5b commit 854482a

File tree

4 files changed

+65
-8
lines changed

4 files changed

+65
-8
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ public void commit() {
8080
} catch (SpannerException e2) {
8181
txnState = TransactionState.COMMIT_FAILED;
8282
throw e2;
83+
} finally {
84+
// At this point, if the TransactionState is not ABORTED, then the transaction has reached an
85+
// end state.
86+
// We can safely call close() to release resources.
87+
if (getState() != TransactionState.ABORTED) {
88+
close();
89+
}
8390
}
8491
}
8592

@@ -92,6 +99,9 @@ public void rollback() {
9299
txn.rollback();
93100
} finally {
94101
txnState = TransactionState.ROLLED_BACK;
102+
// At this point, the TransactionState is ROLLED_BACK which is an end state.
103+
// We can safely call close() to release resources.
104+
close();
95105
}
96106
}
97107

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

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,51 @@ public void clearRequests() {
6060
@Test
6161
public void testAsyncRunner_doesNotReturnCommitTimestampBeforeCommit() {
6262
AsyncRunner runner = client().runAsync();
63-
IllegalStateException e =
64-
assertThrows(IllegalStateException.class, () -> runner.getCommitTimestamp());
65-
assertTrue(e.getMessage().contains("runAsync() has not yet been called"));
63+
if (isMultiplexedSessionsEnabledForRW()) {
64+
Throwable e = assertThrows(Throwable.class, () -> runner.getCommitTimestamp().get());
65+
// If the error occurs within the future, it gets wrapped in an ExecutionException.
66+
// This happens when DelayedAsyncRunner is invoked while the multiplexed session is not yet
67+
// created.
68+
// If the error occurs before the future is created, it may throw an IllegalStateException
69+
// instead.
70+
assertTrue(e instanceof ExecutionException || e instanceof IllegalStateException);
71+
if (e instanceof ExecutionException) {
72+
Throwable cause = e.getCause();
73+
assertTrue(cause instanceof IllegalStateException);
74+
assertTrue(cause.getMessage().contains("runAsync() has not yet been called"));
75+
} else {
76+
assertTrue(e.getMessage().contains("runAsync() has not yet been called"));
77+
}
78+
} else {
79+
IllegalStateException e =
80+
assertThrows(IllegalStateException.class, () -> runner.getCommitTimestamp());
81+
assertTrue(e.getMessage().contains("runAsync() has not yet been called"));
82+
}
6683
}
6784

6885
@Test
6986
public void testAsyncRunner_doesNotReturnCommitResponseBeforeCommit() {
7087
AsyncRunner runner = client().runAsync();
71-
IllegalStateException e =
72-
assertThrows(IllegalStateException.class, () -> runner.getCommitResponse());
73-
assertTrue(e.getMessage().contains("runAsync() has not yet been called"));
88+
if (isMultiplexedSessionsEnabledForRW()) {
89+
Throwable e = assertThrows(Throwable.class, () -> runner.getCommitResponse().get());
90+
// If the error occurs within the future, it gets wrapped in an ExecutionException.
91+
// This happens when DelayedAsyncRunner is invoked while the multiplexed session is not yet
92+
// created.
93+
// If the error occurs before the future is created, it may throw an IllegalStateException
94+
// instead.
95+
assertTrue(e instanceof ExecutionException || e instanceof IllegalStateException);
96+
if (e instanceof ExecutionException) {
97+
Throwable cause = e.getCause();
98+
assertTrue(cause instanceof IllegalStateException);
99+
assertTrue(cause.getMessage().contains("runAsync() has not yet been called"));
100+
} else {
101+
assertTrue(e.getMessage().contains("runAsync() has not yet been called"));
102+
}
103+
} else {
104+
IllegalStateException e =
105+
assertThrows(IllegalStateException.class, () -> runner.getCommitResponse());
106+
assertTrue(e.getMessage().contains("runAsync() has not yet been called"));
107+
}
74108
}
75109

76110
@Test
@@ -558,7 +592,9 @@ public void closeTransactionBeforeEndOfAsyncQuery() throws Exception {
558592
// Wait until at least one row has been fetched. At that moment there should be one session
559593
// checked out.
560594
dataReceived.await();
561-
assertThat(clientImpl.pool.getNumberOfSessionsInUse()).isEqualTo(1);
595+
if (!isMultiplexedSessionsEnabledForRW()) {
596+
assertThat(clientImpl.pool.getNumberOfSessionsInUse()).isEqualTo(1);
597+
}
562598
assertThat(res.isDone()).isFalse();
563599
dataChecked.countDown();
564600
// Get the data from the transaction.

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import static org.junit.Assert.assertNotNull;
2929
import static org.junit.Assert.assertThrows;
3030
import static org.junit.Assert.assertTrue;
31+
import static org.junit.Assume.assumeFalse;
3132

3233
import com.google.api.core.ApiFuture;
3334
import com.google.api.core.ApiFutureCallback;
@@ -250,6 +251,11 @@ public void asyncTransactionManagerUpdate() throws Exception {
250251

251252
@Test
252253
public void asyncTransactionManagerIsNonBlocking() throws Exception {
254+
// TODO: Remove this condition once DelayedAsyncTransactionManager is made non-blocking with
255+
// multiplexed sessions.
256+
assumeFalse(
257+
"DelayedAsyncTransactionManager is currently blocking with multiplexed sessions.",
258+
spanner.getOptions().getSessionPoolOptions().getUseMultiplexedSessionForRW());
253259
mockSpanner.freeze();
254260
try (AsyncTransactionManager manager = clientWithEmptySessionPool().transactionManagerAsync()) {
255261
TransactionContextFuture transactionContextFuture = manager.beginAsync();
@@ -633,6 +639,11 @@ public void asyncTransactionManagerBatchUpdate() throws Exception {
633639

634640
@Test
635641
public void asyncTransactionManagerIsNonBlockingWithBatchUpdate() throws Exception {
642+
// TODO: Remove this condition once DelayedAsyncTransactionManager is made non-blocking with
643+
// multiplexed sessions.
644+
assumeFalse(
645+
"DelayedAsyncTransactionManager is currently blocking with multiplexed sessions.",
646+
spanner.getOptions().getSessionPoolOptions().getUseMultiplexedSessionForRW());
636647
mockSpanner.freeze();
637648
try (AsyncTransactionManager manager = clientWithEmptySessionPool().transactionManagerAsync()) {
638649
TransactionContextFuture transactionContextFuture = manager.beginAsync();

samples/snippets/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
<dependency>
3535
<groupId>com.google.cloud</groupId>
3636
<artifactId>libraries-bom</artifactId>
37-
<version>26.53.0</version>
37+
<version>26.54.0</version>
3838
<type>pom</type>
3939
<scope>import</scope>
4040
</dependency>

0 commit comments

Comments
 (0)