use of com.google.spanner.v1.RollbackRequest in project spring-cloud-gcp by spring-cloud.
the class ReactiveFirestoreTransactionManagerTest method getFirestoreTemplate.
private FirestoreTemplate getFirestoreTemplate() {
doAnswer(invocation -> {
StreamObserver<BeginTransactionResponse> streamObserver = invocation.getArgument(1);
streamObserver.onNext(BeginTransactionResponse.newBuilder().setTransaction(ByteString.copyFromUtf8("transaction1")).build());
streamObserver.onCompleted();
return null;
}).when(this.firestoreStub).beginTransaction(any(), any());
doAnswer(invocation -> {
CommitRequest commitRequest = invocation.getArgument(0);
StreamObserver<CommitResponse> streamObserver = invocation.getArgument(1);
assertThat(commitRequest.getTransaction()).isEqualTo(ByteString.copyFromUtf8("transaction1"));
streamObserver.onNext(CommitResponse.newBuilder().build());
streamObserver.onCompleted();
return null;
}).when(this.firestoreStub).commit(any(), any());
doAnswer(invocation -> {
RollbackRequest rollbackRequest = invocation.getArgument(0);
StreamObserver<Empty> streamObserver = invocation.getArgument(1);
assertThat(rollbackRequest.getTransaction()).isEqualTo(ByteString.copyFromUtf8("transaction1"));
streamObserver.onCompleted();
return null;
}).when(this.firestoreStub).rollback(any(), any());
doAnswer(invocation -> {
GetDocumentRequest request = invocation.getArgument(0);
StreamObserver<Document> streamObserver = invocation.getArgument(1);
assertThat(request.getTransaction()).isEqualTo(ByteString.copyFromUtf8("transaction1"));
String name = request.getName();
streamObserver.onNext(FirestoreTemplateTests.buildDocument(name.substring(name.length() - 2), 100L));
streamObserver.onCompleted();
return null;
}).when(this.firestoreStub).getDocument(any(), any());
FirestoreTemplate template = new FirestoreTemplate(this.firestoreStub, this.parent, new FirestoreDefaultClassMapper(), new FirestoreMappingContext());
StepVerifier.setDefaultTimeout(Duration.ofSeconds(5));
return template;
}
use of com.google.spanner.v1.RollbackRequest in project java-spanner by googleapis.
the class SpannerClient method rollback.
// AUTO-GENERATED DOCUMENTATION AND METHOD.
/**
* Rolls back a transaction, releasing any locks it holds. It is a good idea to call this for any
* transaction that includes one or more [Read][google.spanner.v1.Spanner.Read] or
* [ExecuteSql][google.spanner.v1.Spanner.ExecuteSql] requests and ultimately decides not to
* commit.
*
* <p>`Rollback` returns `OK` if it successfully aborts the transaction, the transaction was
* already aborted, or the transaction is not found. `Rollback` never returns `ABORTED`.
*
* <p>Sample code:
*
* <pre>{@code
* try (SpannerClient spannerClient = SpannerClient.create()) {
* String session =
* SessionName.of("[PROJECT]", "[INSTANCE]", "[DATABASE]", "[SESSION]").toString();
* ByteString transactionId = ByteString.EMPTY;
* spannerClient.rollback(session, transactionId);
* }
* }</pre>
*
* @param session Required. The session in which the transaction to roll back is running.
* @param transactionId Required. The transaction to roll back.
* @throws com.google.api.gax.rpc.ApiException if the remote call fails
*/
public final void rollback(String session, ByteString transactionId) {
RollbackRequest request = RollbackRequest.newBuilder().setSession(session).setTransactionId(transactionId).build();
rollback(request);
}
use of com.google.spanner.v1.RollbackRequest in project java-spanner by googleapis.
the class SpannerClient method rollback.
// AUTO-GENERATED DOCUMENTATION AND METHOD.
/**
* Rolls back a transaction, releasing any locks it holds. It is a good idea to call this for any
* transaction that includes one or more [Read][google.spanner.v1.Spanner.Read] or
* [ExecuteSql][google.spanner.v1.Spanner.ExecuteSql] requests and ultimately decides not to
* commit.
*
* <p>`Rollback` returns `OK` if it successfully aborts the transaction, the transaction was
* already aborted, or the transaction is not found. `Rollback` never returns `ABORTED`.
*
* <p>Sample code:
*
* <pre>{@code
* try (SpannerClient spannerClient = SpannerClient.create()) {
* SessionName session = SessionName.of("[PROJECT]", "[INSTANCE]", "[DATABASE]", "[SESSION]");
* ByteString transactionId = ByteString.EMPTY;
* spannerClient.rollback(session, transactionId);
* }
* }</pre>
*
* @param session Required. The session in which the transaction to roll back is running.
* @param transactionId Required. The transaction to roll back.
* @throws com.google.api.gax.rpc.ApiException if the remote call fails
*/
public final void rollback(SessionName session, ByteString transactionId) {
RollbackRequest request = RollbackRequest.newBuilder().setSession(session == null ? null : session.toString()).setTransactionId(transactionId).build();
rollback(request);
}
use of com.google.spanner.v1.RollbackRequest in project java-spanner by googleapis.
the class MockSpannerServiceImpl method rollback.
@Override
public void rollback(RollbackRequest request, StreamObserver<Empty> responseObserver) {
requests.add(request);
Preconditions.checkNotNull(request.getTransactionId());
Session session = sessions.get(request.getSession());
if (session == null) {
setSessionNotFound(request.getSession(), responseObserver);
return;
}
sessionLastUsed.put(session.getName(), Instant.now());
try {
rollbackExecutionTime.simulateExecutionTime(exceptions, stickyGlobalExceptions, freezeLock);
Transaction transaction = transactions.get(request.getTransactionId());
if (transaction != null) {
rollbackTransaction(transaction.getId());
}
responseObserver.onNext(Empty.getDefaultInstance());
responseObserver.onCompleted();
} catch (StatusRuntimeException t) {
responseObserver.onError(t);
} catch (Throwable t) {
responseObserver.onError(Status.INTERNAL.asRuntimeException());
}
}
use of com.google.spanner.v1.RollbackRequest in project java-spanner by googleapis.
the class SpannerClientTest method rollbackTest.
@Test
public void rollbackTest() throws Exception {
Empty expectedResponse = Empty.newBuilder().build();
mockSpanner.addResponse(expectedResponse);
SessionName session = SessionName.of("[PROJECT]", "[INSTANCE]", "[DATABASE]", "[SESSION]");
ByteString transactionId = ByteString.EMPTY;
client.rollback(session, transactionId);
List<AbstractMessage> actualRequests = mockSpanner.getRequests();
Assert.assertEquals(1, actualRequests.size());
RollbackRequest actualRequest = ((RollbackRequest) actualRequests.get(0));
Assert.assertEquals(session.toString(), actualRequest.getSession());
Assert.assertEquals(transactionId, actualRequest.getTransactionId());
Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Aggregations