use of com.google.spanner.v1.BeginTransactionRequest in project grpc-gcp-java by GoogleCloudPlatform.
the class SpannerProbes method transactionProber.
/**
* Probe to test BeginTransaction, Commit and Rollback grpc from Spanner stub.
*/
public static void transactionProber(SpannerGrpc.SpannerBlockingStub stub) {
Session session = null;
try {
session = stub.createSession(CreateSessionRequest.newBuilder().setDatabase(DATABASE).build());
// Probing begin transaction call.
TransactionOptions options = TransactionOptions.newBuilder().setReadWrite(TransactionOptions.ReadWrite.getDefaultInstance()).build();
BeginTransactionRequest request = BeginTransactionRequest.newBuilder().setSession(session.getName()).setOptions(options).build();
Transaction txn = stub.beginTransaction(request);
// Probing commit call.
stub.commit(CommitRequest.newBuilder().setSession(session.getName()).setTransactionId(txn.getId()).build());
// Probing rollback call.
txn = stub.beginTransaction(request);
stub.rollback(RollbackRequest.newBuilder().setSession(session.getName()).setTransactionId(txn.getId()).build());
} finally {
deleteSession(stub, session);
}
}
use of com.google.spanner.v1.BeginTransactionRequest in project grpc-gcp-java by GoogleCloudPlatform.
the class BeginTransaction method beginTransactionCall.
public void beginTransactionCall() {
FirestoreBlockingStub blockingStub = new GRPCFirebaseClientFactory().createFirebaseClient().getBlockingStub();
TransactionOptions tOpts = TransactionOptions.newBuilder().build();
BeginTransactionRequest beginTransactionRequest = BeginTransactionRequest.newBuilder().setDatabase("projects/firestoretestclient/databases/(default)").setOptions(tOpts).build();
BeginTransactionResponse response;
try {
response = blockingStub.beginTransaction(beginTransactionRequest);
} catch (Exception e) {
System.out.println("Error during call: " + e.getMessage() + e.getCause());
return;
}
System.out.println("Began Transaction: " + response.getTransaction().toString());
Main.transactionId = response.getTransaction();
Menu menu = new Menu();
menu.draw();
}
use of com.google.spanner.v1.BeginTransactionRequest in project grpc-gcp-java by GoogleCloudPlatform.
the class SpannerClientV1TestCases method prepareTestData.
void prepareTestData() throws InterruptedException {
int columnBytes = Integer.min(payload, MAX_SIZE_PER_COLUMN);
int rows = (payload - 1) / columnBytes + 1;
char[] charArray = new char[columnBytes];
Arrays.fill(charArray, 'z');
String colContent = new String(charArray);
SpannerClient client = getClient();
Session session = client.createSession(CreateSessionRequest.newBuilder().setDatabase(database).build());
long start = System.currentTimeMillis();
// Clean the existing data.
BeginTransactionRequest request = BeginTransactionRequest.newBuilder().setSession(session.getName()).setOptions(TransactionOptions.newBuilder().setReadWrite(TransactionOptions.ReadWrite.getDefaultInstance()).build()).build();
Transaction txn = client.beginTransaction(request);
client.commit(CommitRequest.newBuilder().addMutations(Mutation.newBuilder().setDelete(Mutation.Delete.newBuilder().setTable(LARGE_TABLE).setKeySet(KeySet.newBuilder().setAll(true).build()).build()).build()).setSession(session.getName()).setTransactionId(txn.getId()).build());
System.out.println(String.format("\nDeleted the previous large_table in %d ms.", System.currentTimeMillis() - start));
// Add the payload data.
start = System.currentTimeMillis();
for (int i = 0; i < rows; i++) {
txn = client.beginTransaction(request);
client.commit(CommitRequest.newBuilder().addMutations(Mutation.newBuilder().setInsertOrUpdate(Mutation.Write.newBuilder().addColumns("id").addColumns("data").addValues(ListValue.newBuilder().addValues(Value.newBuilder().setStringValue("payload" + i)).addValues(Value.newBuilder().setStringValue(colContent)).build()).setTable(LARGE_TABLE).build()).build()).setSession(session.getName()).setTransactionId(txn.getId()).build());
}
System.out.println(String.format("Successfully added ColumnBytes: %d, Rows: %d to large_table in %d ms.", columnBytes, rows, System.currentTimeMillis() - start));
cleanUpClient(client, session.getName());
}
use of com.google.spanner.v1.BeginTransactionRequest in project grpc-gcp-java by GoogleCloudPlatform.
the class SpannerTestCases method prepareTestData.
void prepareTestData() throws InterruptedException {
ManagedChannel channel = getChannel();
SpannerBlockingStub stub = getBlockingStub(channel);
// Because of max data size, we need to separate into different rows.
int columnBytes = Integer.min(payload, MAX_SIZE_PER_COLUMN);
int rows = (payload - 1) / columnBytes + 1;
char[] charArray = new char[columnBytes];
Arrays.fill(charArray, 'z');
String colContent = new String(charArray);
Session session = stub.createSession(CreateSessionRequest.newBuilder().setDatabase(database).build());
long start = System.currentTimeMillis();
// Clean the data in the table.
BeginTransactionRequest request = BeginTransactionRequest.newBuilder().setSession(session.getName()).setOptions(TransactionOptions.newBuilder().setReadWrite(TransactionOptions.ReadWrite.getDefaultInstance()).build()).build();
Transaction txn = stub.beginTransaction(request);
stub.commit(CommitRequest.newBuilder().addMutations(Mutation.newBuilder().setDelete(Mutation.Delete.newBuilder().setTable(LARGE_TABLE).setKeySet(KeySet.newBuilder().setAll(true).build()).build()).build()).setSession(session.getName()).setTransactionId(txn.getId()).build());
System.out.println(String.format("\nDeleted the previous large_table in %d ms.", System.currentTimeMillis() - start));
// Add the payload data.
start = System.currentTimeMillis();
for (int i = 0; i < rows; i++) {
txn = stub.beginTransaction(request);
stub.commit(CommitRequest.newBuilder().addMutations(Mutation.newBuilder().setInsertOrUpdate(Mutation.Write.newBuilder().addColumns("id").addColumns("data").addValues(ListValue.newBuilder().addValues(Value.newBuilder().setStringValue("payload" + i)).addValues(Value.newBuilder().setStringValue(colContent)).build()).setTable(LARGE_TABLE).build()).build()).setSession(session.getName()).setTransactionId(txn.getId()).build());
}
System.out.println(String.format("Successfully added ColumnBytes: %d, Rows: %d to large_table in %d ms.", columnBytes, rows, System.currentTimeMillis() - start));
stub.deleteSession(DeleteSessionRequest.newBuilder().setName(session.getName()).build());
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
}
use of com.google.spanner.v1.BeginTransactionRequest in project appengine-java-standard by GoogleCloudPlatform.
the class CloudDatastoreV1ClientImplTest method testRetries_MaxRetriesExceeded.
@Test
public void testRetries_MaxRetriesExceeded() throws Exception {
BeginTransactionRequest req = BeginTransactionRequest.getDefaultInstance();
CloudDatastoreV1ClientImpl client = new CloudDatastoreV1ClientImpl(datastore, 2);
DatastoreException lastException = retryableException();
when(datastore.beginTransaction(req)).thenThrow(retryableException()).thenThrow(retryableException()).thenThrow(lastException);
try {
client.beginTransaction(req).get();
fail("expected exception");
} catch (ExecutionException e) {
assertThat(e).hasCauseThat().hasCauseThat().isEqualTo(lastException);
}
}
Aggregations