Search in sources :

Example 6 with Transaction

use of com.google.cloud.datastore.Transaction in project google-cloud-java by GoogleCloudPlatform.

the class ITTransactionSnippets method testRollback.

@Test
public void testRollback() {
    Transaction transaction = datastore.newTransaction();
    TransactionSnippets transactionSnippets = new TransactionSnippets(transaction);
    Key key = transactionSnippets.rollback();
    Entity result = datastore.get(key);
    assertNull(result);
}
Also used : Entity(com.google.cloud.datastore.Entity) Transaction(com.google.cloud.datastore.Transaction) Key(com.google.cloud.datastore.Key) Test(org.junit.Test)

Example 7 with Transaction

use of com.google.cloud.datastore.Transaction in project google-cloud-java by GoogleCloudPlatform.

the class ITTransactionSnippets method testFetchDeleteEntitiesWithKeys.

@Test
public void testFetchDeleteEntitiesWithKeys() {
    Key key1 = datastore.newKeyFactory().setKind("MyKind").newKey("fetch_key_1");
    Key key2 = datastore.newKeyFactory().setKind("MyKind").newKey("fetch_key_2");
    Entity entity1 = Entity.newBuilder(key1).set("description", "fetch1").build();
    Entity entity2 = Entity.newBuilder(key2).set("description", "fetch2").build();
    datastore.put(entity1, entity2);
    registerKey("fetch_key_1");
    registerKey("fetch_key_2");
    Transaction transaction = datastore.newTransaction();
    TransactionSnippets transactionSnippets = new TransactionSnippets(transaction);
    Set<Entity> entities = Sets.newHashSet(transactionSnippets.fetchEntitiesWithKeys("fetch_key_1", "fetch_key_2"));
    assertEquals(2, entities.size());
    assertTrue(entities.contains(entity1));
    assertTrue(entities.contains(entity2));
    transaction = datastore.newTransaction();
    transactionSnippets = new TransactionSnippets(transaction);
    transactionSnippets.multipleDeleteEntities("fetch_key_1", "fetch_key_2");
    transaction = datastore.newTransaction();
    transactionSnippets = new TransactionSnippets(transaction);
    List<Entity> deletedEntities = transactionSnippets.fetchEntitiesWithKeys("fetch_key_1", "fetch_key_2");
    assertNull(deletedEntities.get(0));
    assertNull(deletedEntities.get(1));
}
Also used : Entity(com.google.cloud.datastore.Entity) Transaction(com.google.cloud.datastore.Transaction) Key(com.google.cloud.datastore.Key) Test(org.junit.Test)

Example 8 with Transaction

use of com.google.cloud.datastore.Transaction in project google-cloud-java by GoogleCloudPlatform.

the class ITDatastoreTest method testNewTransactionRollback.

@Test
public void testNewTransactionRollback() {
    Transaction transaction = DATASTORE.newTransaction();
    transaction.add(ENTITY3);
    Entity entity2 = Entity.newBuilder(ENTITY2).clear().setNull("bla").set("list3", StringValue.of("bla"), StringValue.newBuilder("bla").build()).build();
    transaction.update(entity2);
    transaction.delete(KEY1);
    transaction.rollback();
    // should be safe to repeat rollback calls
    transaction.rollback();
    try {
        transaction.commit();
        fail("Expecting a failure");
    } catch (DatastoreException expected) {
        assertEquals("FAILED_PRECONDITION", expected.getReason());
    }
    List<Entity> list = DATASTORE.fetch(KEY1, KEY2, KEY3);
    assertEquals(ENTITY1, list.get(0));
    assertEquals(ENTITY2, list.get(1));
    assertNull(list.get(2));
    assertEquals(3, list.size());
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) Entity(com.google.cloud.datastore.Entity) ProjectionEntity(com.google.cloud.datastore.ProjectionEntity) Transaction(com.google.cloud.datastore.Transaction) DatastoreException(com.google.cloud.datastore.DatastoreException) Test(org.junit.Test)

Example 9 with Transaction

use of com.google.cloud.datastore.Transaction in project google-cloud-java by GoogleCloudPlatform.

the class ITDatastoreTest method testNewTransactionCommit.

@Test
public void testNewTransactionCommit() {
    Transaction transaction = DATASTORE.newTransaction();
    transaction.add(ENTITY3);
    Entity entity2 = Entity.newBuilder(ENTITY2).clear().setNull("bla").build();
    transaction.update(entity2);
    transaction.delete(KEY1);
    transaction.commit();
    assertFalse(transaction.isActive());
    List<Entity> list = DATASTORE.fetch(KEY1, KEY2, KEY3);
    assertNull(list.get(0));
    assertEquals(entity2, list.get(1));
    assertEquals(ENTITY3, list.get(2));
    assertEquals(3, list.size());
    try {
        transaction.commit();
        fail("Expecting a failure");
    } catch (DatastoreException expected) {
        assertEquals("FAILED_PRECONDITION", expected.getReason());
    }
    try {
        transaction.rollback();
        fail("Expecting a failure");
    } catch (DatastoreException expected) {
        assertEquals("FAILED_PRECONDITION", expected.getReason());
    }
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) Entity(com.google.cloud.datastore.Entity) ProjectionEntity(com.google.cloud.datastore.ProjectionEntity) Transaction(com.google.cloud.datastore.Transaction) DatastoreException(com.google.cloud.datastore.DatastoreException) Test(org.junit.Test)

Example 10 with Transaction

use of com.google.cloud.datastore.Transaction in project google-cloud-java by GoogleCloudPlatform.

the class ITDatastoreTest method testTransactionWithQuery.

@Test
public void testTransactionWithQuery() throws InterruptedException {
    Query<Entity> query = Query.newEntityQueryBuilder().setKind(KIND2).setFilter(PropertyFilter.hasAncestor(KEY2)).setNamespace(NAMESPACE).build();
    Transaction transaction = DATASTORE.newTransaction();
    QueryResults<Entity> results = transaction.run(query);
    assertTrue(results.hasNext());
    assertEquals(ENTITY2, results.next());
    assertFalse(results.hasNext());
    transaction.add(ENTITY3);
    transaction.commit();
    assertEquals(ENTITY3, DATASTORE.get(KEY3));
    transaction = DATASTORE.newTransaction();
    results = transaction.run(query);
    assertTrue(results.hasNext());
    assertEquals(ENTITY2, results.next());
    assertFalse(results.hasNext());
    transaction.delete(ENTITY3.getKey());
    // update entity2 during the transaction
    DATASTORE.put(Entity.newBuilder(ENTITY2).clear().build());
    try {
        transaction.commit();
        fail("Expecting a failure");
    } catch (DatastoreException expected) {
        assertEquals("ABORTED", expected.getReason());
    }
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) Entity(com.google.cloud.datastore.Entity) ProjectionEntity(com.google.cloud.datastore.ProjectionEntity) Transaction(com.google.cloud.datastore.Transaction) DatastoreException(com.google.cloud.datastore.DatastoreException) Test(org.junit.Test)

Aggregations

Transaction (com.google.cloud.datastore.Transaction)17 Test (org.junit.Test)16 Entity (com.google.cloud.datastore.Entity)15 Key (com.google.cloud.datastore.Key)9 DatastoreException (com.google.cloud.datastore.DatastoreException)4 FullEntity (com.google.cloud.datastore.FullEntity)3 ProjectionEntity (com.google.cloud.datastore.ProjectionEntity)3 Datastore (com.google.cloud.datastore.Datastore)1 DatastoreOptions (com.google.cloud.datastore.DatastoreOptions)1 IncompleteKey (com.google.cloud.datastore.IncompleteKey)1 KeyFactory (com.google.cloud.datastore.KeyFactory)1