Vinay C
Vinay C

Reputation: 397

Read from datastore not consistent

I am new to Google App engine, i was trying some sample code and i am stuck :(

Below is the code:

datastore = DatastoreServiceFactory.getDatastoreService();

    Transaction txn = datastore.beginTransaction();

    Entity oSet = new Entity("Set", "Set1");
    datastore.put(oSet);

    Entity oItem1 = new Entity("item", "item1", oSet.getKey());
    oItem1.setProperty("qty", "two");
    datastore.put(oItem1);

    Entity oItem2 = new Entity("item", "item2", oSet.getKey());
    oItem2.setProperty("qty", "five");
    datastore.put(oItem2);

    Query query = new Query("item").setAncestor(oSet.getKey());


    List<Entity> oItems = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(50));

    for(Entity i : oItems) {
        System.out.println("item qty: " + i.getProperty("qty"));
    }

    txn.commit();

I am trying to create two "item" entities with one property "qty". These two "item" entities are descendants of entity "Set". But i am not able to retrieve the "item" entities back. Is something wrong with the query?

Upvotes: 1

Views: 171

Answers (1)

skywalker
skywalker

Reputation: 415

you need to put txn.commit(); just after datastore.put(oItem2); that will ensure that your write operation is completed, after that running query (with or without a separate transaction) will fetch you correct results

hope it helps.

Upvotes: 1

Related Questions