use of com.google.spanner.v1.SessionName in project google-cloud-java by GoogleCloudPlatform.
the class GrpcSpannerRpc method deleteSession.
@Override
public void deleteSession(String sessionName, @Nullable Map<Option, ?> options) {
DeleteSessionRequest request = DeleteSessionRequest.newBuilder().setName(sessionName).build();
get(doUnaryCall(SpannerGrpc.METHOD_DELETE_SESSION, request, sessionName, Option.CHANNEL_HINT.getLong(options)));
}
use of com.google.spanner.v1.SessionName in project google-cloud-java by GoogleCloudPlatform.
the class SessionImplTest method setUp.
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
SpannerImpl spanner = new SpannerImpl(rpc, 1, null);
String dbName = "projects/p1/instances/i1/databases/d1";
String sessionName = dbName + "/sessions/s1";
DatabaseId db = DatabaseId.of(dbName);
Session sessionProto = Session.newBuilder().setName(sessionName).build();
Mockito.when(rpc.createSession(Mockito.eq(dbName), optionsCaptor.capture())).thenReturn(sessionProto);
session = spanner.createSession(db);
// We expect the same options, "options", on all calls on "session".
options = optionsCaptor.getValue();
Mockito.reset(rpc);
}
use of com.google.spanner.v1.SessionName 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.SessionName 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 com.google.spanner.v1.SessionName in project java-spanner by googleapis.
the class SessionPoolTest method testSessionNotFoundReadOnlyTransaction.
@Test
public void testSessionNotFoundReadOnlyTransaction() {
Statement statement = Statement.of("SELECT 1");
final SessionImpl closedSession = mockSession();
when(closedSession.readOnlyTransaction()).thenThrow(SpannerExceptionFactoryTest.newSessionNotFoundException(sessionName));
final SessionImpl openSession = mockSession();
ReadOnlyTransaction openTransaction = mock(ReadOnlyTransaction.class);
ResultSet openResultSet = mock(ResultSet.class);
when(openResultSet.next()).thenReturn(true, false);
when(openTransaction.executeQuery(statement)).thenReturn(openResultSet);
when(openSession.readOnlyTransaction()).thenReturn(openTransaction);
doAnswer(invocation -> {
executor.submit(() -> {
SessionConsumerImpl consumer = invocation.getArgument(2, SessionConsumerImpl.class);
consumer.onSessionReady(closedSession);
});
return null;
}).doAnswer(invocation -> {
executor.submit(() -> {
SessionConsumerImpl consumer = invocation.getArgument(2, SessionConsumerImpl.class);
consumer.onSessionReady(openSession);
});
return null;
}).when(sessionClient).asyncBatchCreateSessions(Mockito.eq(1), Mockito.anyBoolean(), any(SessionConsumer.class));
FakeClock clock = new FakeClock();
clock.currentTimeMillis = System.currentTimeMillis();
pool = createPool(clock);
ReadOnlyTransaction transaction = pool.getSession().readOnlyTransaction();
ResultSet resultSet = transaction.executeQuery(statement);
assertThat(resultSet.next()).isTrue();
}
Aggregations