use of jdk.incubator.sql2.Session in project java-spanner by googleapis.
the class MockSpannerServiceImpl method commit.
@Override
public void commit(CommitRequest request, StreamObserver<CommitResponse> responseObserver) {
requests.add(request);
Preconditions.checkNotNull(request.getSession());
Session session = sessions.get(request.getSession());
if (session == null) {
setSessionNotFound(request.getSession(), responseObserver);
return;
}
sessionLastUsed.put(session.getName(), Instant.now());
try {
commitExecutionTime.simulateExecutionTime(exceptions, stickyGlobalExceptions, freezeLock);
// Find or start a transaction
Transaction transaction;
if (request.hasSingleUseTransaction()) {
// Start a temporary transaction
transaction = beginTransaction(session, TransactionOptions.newBuilder().setReadWrite(ReadWrite.getDefaultInstance()).build());
} else if (request.getTransactionId() != null) {
transaction = transactions.get(request.getTransactionId());
Optional<Boolean> aborted = Optional.fromNullable(abortedTransactions.get(request.getTransactionId()));
if (aborted.or(Boolean.FALSE)) {
throwTransactionAborted(request.getTransactionId());
}
} else {
// No transaction mode specified
responseObserver.onError(Status.INVALID_ARGUMENT.withDescription("No transaction mode specified").asRuntimeException());
return;
}
if (transaction == null) {
setTransactionNotFound(request.getTransactionId(), responseObserver);
return;
}
simulateAbort(session, request.getTransactionId());
commitTransaction(transaction.getId());
CommitResponse.Builder responseBuilder = CommitResponse.newBuilder().setCommitTimestamp(getCurrentGoogleTimestamp());
if (request.getReturnCommitStats()) {
responseBuilder.setCommitStats(com.google.spanner.v1.CommitResponse.CommitStats.newBuilder().setMutationCount(request.getMutationsCount()).build());
}
responseObserver.onNext(responseBuilder.build());
responseObserver.onCompleted();
} catch (StatusRuntimeException t) {
responseObserver.onError(t);
} catch (Throwable t) {
responseObserver.onError(Status.INTERNAL.asRuntimeException());
}
}
use of jdk.incubator.sql2.Session in project java-spanner by googleapis.
the class MockSpannerServiceImpl method partition.
private void partition(String sessionName, TransactionSelector transactionSelector, StreamObserver<PartitionResponse> responseObserver) {
Session session = sessions.get(sessionName);
if (session == null) {
setSessionNotFound(sessionName, responseObserver);
return;
}
sessionLastUsed.put(session.getName(), Instant.now());
try {
ByteString transactionId = getTransactionId(session, transactionSelector);
responseObserver.onNext(PartitionResponse.newBuilder().addPartitions(Partition.newBuilder().setPartitionToken(generatePartitionToken(session.getName(), transactionId)).build()).build());
responseObserver.onCompleted();
} catch (StatusRuntimeException e) {
responseObserver.onError(e);
} catch (Throwable t) {
responseObserver.onError(Status.INTERNAL.asRuntimeException());
}
}
use of jdk.incubator.sql2.Session 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 jdk.incubator.sql2.Session in project java-spanner by googleapis.
the class MockSpannerServiceImpl method beginTransaction.
@Override
public void beginTransaction(BeginTransactionRequest request, StreamObserver<Transaction> responseObserver) {
requests.add(request);
Preconditions.checkNotNull(request.getSession());
Session session = sessions.get(request.getSession());
if (session == null) {
setSessionNotFound(request.getSession(), responseObserver);
return;
}
sessionLastUsed.put(session.getName(), Instant.now());
try {
beginTransactionExecutionTime.simulateExecutionTime(exceptions, stickyGlobalExceptions, freezeLock);
Transaction transaction = beginTransaction(session, request.getOptions());
responseObserver.onNext(transaction);
responseObserver.onCompleted();
} catch (StatusRuntimeException t) {
responseObserver.onError(t);
} catch (Throwable t) {
responseObserver.onError(Status.INTERNAL.asRuntimeException());
}
}
use of jdk.incubator.sql2.Session in project java-spanner by googleapis.
the class MockSpannerServiceImpl method getSession.
@Override
public void getSession(GetSessionRequest request, StreamObserver<Session> responseObserver) {
requests.add(request);
Preconditions.checkNotNull(request.getName());
try {
getSessionExecutionTime.simulateExecutionTime(exceptions, stickyGlobalExceptions, freezeLock);
Session session = sessions.get(request.getName());
if (session == null) {
setSessionNotFound(request.getName(), responseObserver);
} else {
session = session.toBuilder().setApproximateLastUseTime(getCurrentGoogleTimestamp()).build();
responseObserver.onNext(session);
responseObserver.onCompleted();
}
} catch (StatusRuntimeException e) {
responseObserver.onError(e);
} catch (Throwable t) {
responseObserver.onError(Status.INTERNAL.asRuntimeException());
}
}
Aggregations