Question

I have created a simple application in GAE. This is my simple entity/JDO class

@PersistenceCapable(detachable = "true")
public class Domain implements Serializable{
    @Persistent
    private String url;
    @Persistent
        private String aliasName;
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;
}

I am performing CRUD operation on this class. What i am doing is just add different object and then view that objects give option to delete and edit already created entities. When I click on the delete link against any entity in the view page. i just delete it. Issue is with me that after deleting that entity i redirect the user again to the view page so that user can see entity has been deleted. But when i see the view page again view page also show the deleted entity. means entity has not been deleted. If i refresh the page 3,4 times then after that entity didn't show. This is because entity has been deleted at first place but it take some time to complete this process. I want to know why i am facing this behavior of GAE?? If GAE or JDO is slow in performing CRUD operations. I am sharing my code for deletion of entity and for view all entities. Is there is any issue in my code please tell me.

    @SuppressWarnings({ "rawtypes", "unchecked" })
public List retrieveDomains() {
    PersistenceManager pm = PMFSingleton.get().getPersistenceManager();
    Query query = pm.newQuery(Domain.class);
    List<Domain> list = null;
    List<Domain> detachedList = null;
    try {
        list = (List<Domain>) query.execute();
        detachedList = new ArrayList<Domain>();
        for (Domain obj : list) {
            detachedList.add(pm.detachCopy(obj));
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        query.closeAll();
        pm.close();
    }
    return detachedList;
}

  public void deleteDomain(String name) {
    PersistenceManager pm = PMFSingleton.get().getPersistenceManager();
    Domain domain = null;
    Transaction txn = null;
    try {
        txn = pm.currentTransaction();
        txn.begin();
        domain = (Domain) pm.getObjectById(Domain.class,name);
        pm.deletePersistent(domain);
        txn.commit();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if ( txn.isActive()) {
            txn.rollback();
        }
        pm.close();
    }       
}
Was it helpful?

Solution

What you've encountered is known as eventual consistency: after you make changes to entities (=delete), the indexes are updated asynchronously. This means that delete operation returns, but indexes are not yet updated. Since query relies on indexes it will show deleted data until indexes are finally updated.

There is a series of articles if you want to learn how datastore works under the hood.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top