use of com.google.cloud.datastore.Transaction in project google-cloud-java by GoogleCloudPlatform.
the class ITDatastoreTest method testTransactionWithRead.
@Test
public void testTransactionWithRead() {
Transaction transaction = DATASTORE.newTransaction();
assertNull(transaction.get(KEY3));
transaction.add(ENTITY3);
transaction.commit();
assertEquals(ENTITY3, DATASTORE.get(KEY3));
transaction = DATASTORE.newTransaction();
assertEquals(ENTITY3, transaction.get(KEY3));
// update entity3 during the transaction
DATASTORE.put(Entity.newBuilder(ENTITY2).clear().set("from", "datastore").build());
transaction.update(Entity.newBuilder(ENTITY2).clear().set("from", "transaction").build());
try {
transaction.commit();
fail("Expecting a failure");
} catch (DatastoreException expected) {
assertEquals("ABORTED", expected.getReason());
}
}
use of com.google.cloud.datastore.Transaction in project google-cloud-java by GoogleCloudPlatform.
the class ITTransactionSnippets method testEntityAddGet.
@Test
public void testEntityAddGet() {
Transaction transaction = datastore.newTransaction();
TransactionSnippets transactionSnippets = new TransactionSnippets(transaction);
transactionSnippets.addSingleEntity(registerKey("add_get_key"));
transaction = datastore.newTransaction();
transactionSnippets = new TransactionSnippets(transaction);
Entity entity = transactionSnippets.get("add_get_key");
assertEquals("value", entity.getString("propertyName"));
}
use of com.google.cloud.datastore.Transaction in project google-cloud-java by GoogleCloudPlatform.
the class ITTransactionSnippets method testPutGetMultiple.
@Test
public void testPutGetMultiple() {
Transaction transaction = datastore.newTransaction();
TransactionSnippets transactionSnippets = new TransactionSnippets(transaction);
transactionSnippets.multipleAddEntities(registerKey("add_get_multiple_key_1"), registerKey("put_get_multiple_key_2"));
transaction = datastore.newTransaction();
transactionSnippets = new TransactionSnippets(transaction);
List<Entity> entities = transactionSnippets.getMultiple("add_get_multiple_key_1", "put_get_multiple_key_2");
assertEquals(2, entities.size());
Set<String> values = ImmutableSet.of(entities.get(0).getString("propertyName"), entities.get(1).getString("propertyName"));
assertTrue(values.contains("value1"));
assertTrue(values.contains("value2"));
}
use of com.google.cloud.datastore.Transaction in project google-cloud-java by GoogleCloudPlatform.
the class ITTransactionSnippets method testEntityPutGet.
@Test
public void testEntityPutGet() {
Transaction transaction = datastore.newTransaction();
TransactionSnippets transactionSnippets = new TransactionSnippets(transaction);
transactionSnippets.putSingleEntity(registerKey("put_get_key"));
transaction = datastore.newTransaction();
transactionSnippets = new TransactionSnippets(transaction);
Entity entity = transactionSnippets.get("put_get_key");
assertEquals("value", entity.getString("propertyName"));
}
use of com.google.cloud.datastore.Transaction in project google-cloud-java by GoogleCloudPlatform.
the class ITTransactionSnippets method testAddGetUpdateMultiple.
@Test
public void testAddGetUpdateMultiple() {
Transaction transaction = datastore.newTransaction();
TransactionSnippets transactionSnippets = new TransactionSnippets(transaction);
transactionSnippets.multipleAddEntities(registerKey("add_get_multiple_key_1"), registerKey("add_get_multiple_key_2"));
transaction = datastore.newTransaction();
transactionSnippets = new TransactionSnippets(transaction);
List<Entity> entities = transactionSnippets.getMultiple("add_get_multiple_key_1", "add_get_multiple_key_2");
assertEquals(2, entities.size());
Set<String> values = ImmutableSet.of(entities.get(0).getString("propertyName"), entities.get(1).getString("propertyName"));
assertTrue(values.contains("value1"));
assertTrue(values.contains("value2"));
transaction = datastore.newTransaction();
transactionSnippets = new TransactionSnippets(transaction);
transactionSnippets.multipleUpdateEntities(registerKey("add_get_multiple_key_1"), registerKey("add_get_multiple_key_2"));
transaction = datastore.newTransaction();
transactionSnippets = new TransactionSnippets(transaction);
entities = transactionSnippets.getMultiple("add_get_multiple_key_1", "add_get_multiple_key_2");
assertEquals(2, entities.size());
values = ImmutableSet.of(entities.get(0).getString("propertyName"), entities.get(1).getString("propertyName"));
assertTrue(values.contains("value3"));
assertTrue(values.contains("value4"));
}
Aggregations