Google app engine: the problem of getting objects from the data warehouse in the task

My application builds an object Parentin a static factory, predefines Children with it , and then runs tasks to perform some calculations in Children, for example:

public static Parent make(User owner, List<Integer> data, int size) {
        Parent result = new Parent(owner,data,size);
        PersistenceManager pm = PersistenceSource.get();
        Transaction tx = pm.currentTransaction();

        try {
            tx.begin();
            result = pm.makePersistent(result);
            for (int i=0; i<size; pm.makePersistent(new Child(result,i++)));
            pm.close();
            tx.commit();
        } finally {
            if (tx.isActive()) { tx.rollback(); result=null; }
        }

        if (result!=null) {
            Queue q = QueueFactory.getDefaultQueue();
            for (Child c : result.getChild()) {
                q.add(url("/task/child").param("key", KeyFactory.keyToString(c.getKey())).method(Method.PUT));
            }
        }
        pm.close();
        return result;
    }

however in the real task

public void doPut(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    PersistenceManager pm = PersistenceSource.get();

    Child c = pm.getObjectById(Child.class, KeyFactory.stringToKey(request.getParameter("key"))); //...

He dies trying to find an object:

Could not retrieve entity of kind Child with key Child(24)
org.datanucleus.exceptions.NucleusObjectNotFoundException: Could not retrieve entity of kind Child with key Child(24)

Any ideas? Also, if that matters, parent-child relationships are defined by the parent as a field in the child (hence, building with the parent as arg).

+3
source share
2 answers

, Child:

    Key k = new KeyFactory
        .Builder(Parent.class.getSimpleName(), Long.valueOf(request.getParameter("parent")))
        .addChild(Child.class.getSimpleName(), Long.valueOf(request.getParameter("child")))
        .getKey();
    Child c = pm.getObjectById(Child.class, k);

, -DataStore , + , , . + SQL, , , , , , pm.getObjectId(Child, KeyFactory./* etc */).

+3
public Object retrieveChildByKey(Key parent, Class childClass, int idChild){
    try{
        pm = PMF.get().getPersistenceManager();
        Key objectKey = KeyFactory.createKey(parent, childClass.getSimpleName(), idChild);
        Object result = pm.getObjectById(childClass, objectKey);
        return result;
    }
    catch (DatastoreTimeoutException ex) {
        // Display a timeout-specific error page
        message = "Timeout: An error has occurred: Was not possible to retrieve the Object";
        System.out.println(message);
        ex.printStackTrace();
    }
    catch(Exception ex){
        message = "Timeout: An error has occurred: Was not possible to retrieve the Object";
        System.out.println(message);
        ex.printStackTrace();
    }finally{
        pm.close();
    }
    return null;
}
0

Source: https://habr.com/ru/post/1761496/


All Articles