Skip to content

Commit a11b756

Browse files
committed
chore(spanner): support AbortedException in begin
1 parent a8dba0a commit a11b756

File tree

5 files changed

+30
-0
lines changed

5 files changed

+30
-0
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.google.cloud.spanner;
1818

1919
import com.google.api.gax.rpc.ApiException;
20+
import com.google.protobuf.ByteString;
2021
import javax.annotation.Nullable;
2122

2223
/**
@@ -31,6 +32,7 @@ public class AbortedException extends SpannerException {
3132
* new transaction attempt) before a retry can succeed.
3233
*/
3334
private static final boolean IS_RETRYABLE = false;
35+
private ByteString transactionID;
3436

3537
/** Private constructor. Use {@link SpannerExceptionFactory} to create instances. */
3638
AbortedException(
@@ -46,6 +48,9 @@ public class AbortedException extends SpannerException {
4648
@Nullable ApiException apiException,
4749
@Nullable XGoogSpannerRequestId reqId) {
4850
super(token, ErrorCode.ABORTED, IS_RETRYABLE, message, cause, apiException, reqId);
51+
if (cause instanceof AbortedException) {
52+
this.transactionID = ((AbortedException) cause).getTransactionID();
53+
}
4954
}
5055

5156
/**
@@ -55,4 +60,12 @@ public class AbortedException extends SpannerException {
5560
public boolean isEmulatorOnlySupportsOneTransactionException() {
5661
return getMessage().endsWith("The emulator only supports one transaction at a time.");
5762
}
63+
64+
void setTransactionID(ByteString transactionID) {
65+
this.transactionID = transactionID;
66+
}
67+
68+
ByteString getTransactionID() {
69+
return this.transactionID;
70+
}
5871
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,11 @@ public TransactionContext begin() {
900900
return internalBegin();
901901
}
902902

903+
@Override
904+
public TransactionContext begin(AbortedException exception) {
905+
return begin();
906+
}
907+
903908
private TransactionContext internalBegin() {
904909
TransactionContext res = new SessionPoolTransactionContext(this, delegate.begin());
905910
session.get().markUsed();

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,7 @@ public SpannerException onError(SpannerException e, boolean withBeginTransaction
793793
long delay = -1L;
794794
if (exceptionToThrow instanceof AbortedException) {
795795
delay = exceptionToThrow.getRetryDelayInMillis();
796+
((AbortedException) exceptionToThrow).setTransactionID(this.transactionId);
796797
}
797798
if (delay == -1L) {
798799
txnLogger.log(

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ public TransactionContext begin() {
9797
return txContext;
9898
}
9999

100+
@Override
101+
public TransactionContext begin(AbortedException exception) {
102+
return begin();
103+
}
104+
100105
@Override
101106
public void commit() {
102107
switch (commitBehavior) {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.google.api.core.ApiFuture;
3737
import com.google.api.gax.longrunning.OperationFuture;
3838
import com.google.cloud.Timestamp;
39+
import com.google.cloud.spanner.AbortedException;
3940
import com.google.cloud.spanner.AsyncResultSet;
4041
import com.google.cloud.spanner.BatchClient;
4142
import com.google.cloud.spanner.CommitResponse;
@@ -130,6 +131,11 @@ public TransactionContext begin() {
130131
return txContext;
131132
}
132133

134+
@Override
135+
public TransactionContext begin(AbortedException exception) {
136+
return begin();
137+
}
138+
133139
@Override
134140
public void commit() {
135141
switch (commitBehavior) {

0 commit comments

Comments
 (0)