Skip to content

Commit b2f22da

Browse files
committed
chore(spanner): support AsyncTransactionManager
1 parent d0de490 commit b2f22da

File tree

5 files changed

+31
-3
lines changed

5 files changed

+31
-3
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ interface AsyncTransactionFunction<I, O> {
170170
*/
171171
TransactionContextFuture beginAsync();
172172

173+
TransactionContextFuture beginAsync(AbortedException abortedException);
174+
173175
/**
174176
* Rolls back the currently active transaction. In most cases there should be no need to call this
175177
* explicitly since {@link #close()} would automatically roll back any active transaction.

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,24 @@ public ApiFuture<Void> closeAsync() {
7676
@Override
7777
public TransactionContextFutureImpl beginAsync() {
7878
Preconditions.checkState(txn == null, "begin can only be called once");
79-
return new TransactionContextFutureImpl(this, internalBeginAsync(true));
79+
return new TransactionContextFutureImpl(this, internalBeginAsync(true, ByteString.EMPTY));
8080
}
8181

82-
private ApiFuture<TransactionContext> internalBeginAsync(boolean firstAttempt) {
82+
@Override
83+
public TransactionContextFutureImpl beginAsync(AbortedException abortedException) {
84+
Preconditions.checkState(txn == null, "begin can only be called once");
85+
ByteString abortedTransactionId = abortedException.getTransactionID() != null ? abortedException.getTransactionID() : ByteString.EMPTY;
86+
return new TransactionContextFutureImpl(this, internalBeginAsync(true, abortedTransactionId));
87+
}
88+
89+
private ApiFuture<TransactionContext> internalBeginAsync(boolean firstAttempt, ByteString abortedTransactionID) {
8390
txnState = TransactionState.STARTED;
8491

8592
// Determine the latest transactionId when using a multiplexed session.
8693
ByteString multiplexedSessionPreviousTransactionId = ByteString.EMPTY;
94+
if (firstAttempt && session.getIsMultiplexed()) {
95+
multiplexedSessionPreviousTransactionId = abortedTransactionID;
96+
}
8797
if (txn != null && session.getIsMultiplexed() && !firstAttempt) {
8898
// Use the current transactionId if available, otherwise fallback to the previous aborted
8999
// transactionId.
@@ -187,7 +197,7 @@ public TransactionContextFuture resetForRetryAsync() {
187197
throw new IllegalStateException(
188198
"resetForRetry can only be called if the previous attempt aborted");
189199
}
190-
return new TransactionContextFutureImpl(this, internalBeginAsync(false));
200+
return new TransactionContextFutureImpl(this, internalBeginAsync(false, ByteString.EMPTY));
191201
}
192202

193203
@Override

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ public TransactionContextFuture beginAsync() {
5050
return getAsyncTransactionManager().beginAsync();
5151
}
5252

53+
@Override
54+
public TransactionContextFuture beginAsync(AbortedException abortedException) {
55+
return getAsyncTransactionManager().beginAsync(abortedException);
56+
}
57+
5358
@Override
5459
public ApiFuture<Void> rollbackAsync() {
5560
return getAsyncTransactionManager().rollbackAsync();

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ public void onSuccess(TransactionContext result) {
163163
return new TransactionContextFutureImpl(this, delegateTxnFuture);
164164
}
165165

166+
@Override
167+
public TransactionContextFuture beginAsync(AbortedException abortedException) {
168+
return beginAsync();
169+
}
170+
166171
@Override
167172
public void onError(Throwable t) {
168173
if (t instanceof AbortedException) {

google-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/ConnectionImplTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import com.google.api.gax.longrunning.OperationFuture;
4747
import com.google.cloud.NoCredentials;
4848
import com.google.cloud.Timestamp;
49+
import com.google.cloud.spanner.AbortedException;
4950
import com.google.cloud.spanner.BatchClient;
5051
import com.google.cloud.spanner.BatchReadOnlyTransaction;
5152
import com.google.cloud.spanner.BatchTransactionId;
@@ -120,6 +121,11 @@ public TransactionContext begin() {
120121
return txContext;
121122
}
122123

124+
@Override
125+
public TransactionContext begin(AbortedException exception) {
126+
return begin();
127+
}
128+
123129
@Override
124130
public void commit() {
125131
Timestamp commitTimestamp = Timestamp.now();

0 commit comments

Comments
 (0)