Skip to content

Commit a2c179f

Browse files
authored
fix: getCommitResponse() should return error if tx has not committed (#4021)
getCommitResponse() returned an error if it was called before the transaction had been committed when using regular sessions. This did not happen when using multiplexed sessions. This change makes sure that the behavior is the same for both regular and multiplexed sessions, by adding a check to the getCommitResponse() method directly.
1 parent 7d752d6 commit a2c179f

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,10 @@ interface AsyncTransactionFunction<I, O> {
203203
/** Returns the state of the transaction. */
204204
TransactionState getState();
205205

206-
/** Returns the {@link CommitResponse} of this transaction. */
206+
/**
207+
* Returns the {@link CommitResponse} of this transaction. This method may only be called after
208+
* committing the transaction.
209+
*/
207210
ApiFuture<CommitResponse> getCommitResponse();
208211

209212
/**

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,9 @@ public TransactionState getState() {
221221

222222
@Override
223223
public ApiFuture<CommitResponse> getCommitResponse() {
224+
Preconditions.checkState(
225+
txnState == TransactionState.COMMITTED,
226+
"getCommitResponse can only be invoked if the transaction was successfully committed");
224227
return commitResponse;
225228
}
226229

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,14 @@ public void asyncTransactionManager_shouldRollbackOnCloseAsync() throws Exceptio
200200
0L);
201201
}
202202

203+
@Test
204+
public void testAsyncTransactionManager_getCommitResponseReturnsErrorBeforeCommit() {
205+
try (AsyncTransactionManager manager = client().transactionManagerAsync()) {
206+
TransactionContextFuture transactionContextFuture = manager.beginAsync();
207+
assertThrows(IllegalStateException.class, manager::getCommitResponse);
208+
}
209+
}
210+
203211
@Test
204212
public void testAsyncTransactionManager_returnsCommitStats() throws Exception {
205213
try (AsyncTransactionManager manager =

0 commit comments

Comments
 (0)