Objectify BATCH delete has no effect

一世执手 提交于 2020-01-05 09:15:22

问题


I have a DAO below, with a transactional delete per entity and in batch. Deleting one entity at a time works just fine.

Batch delete has NO effect whatsoever : the code below is simple and straightforward IMO, but the call to deleteMyObjects(Long[] ids) - which calls delete(Iterable keysOrEntities) of Objectify - has no effect !

public class MyObjectDao {

    private ObjectifyOpts transactional = new ObjectifyOpts().setBeginTransaction(true);

    private ObjectifyOpts nonTransactional = new ObjectifyOpts().setBeginTransaction(false);

    private String namespace = null;

    public MyObjectDao(String namespace) {
        Preconditions.checkNotNull(namespace, "Namespace cannot be NULL");
        this.namespace = namespace;
    }

    /**
     * set namespace and get a non-transactional instance of Objectify
     * 
     * @return
     */
    protected Objectify nontxn() {
        NamespaceManager.set(namespace);
        return ObjectifyService.factory().begin(nonTransactional);
    }

    /**
     * set namespace and get a transactional instance of Objectify
     * 
     * @return
     */
    protected Objectify txn() {
        NamespaceManager.set(namespace);
        Objectify txn =  ObjectifyService.factory().begin(transactional);
        log.log(Level.FINE, "transaction <" + txn.getTxn().getId() + "> started");
        return txn;
    }

    protected void commit(Objectify txn) {
        if (txn != null && txn.getTxn().isActive()) {
            txn.getTxn().commit();
            log.log(Level.FINE, "transaction <" + txn.getTxn().getId() + "> committed");
        } else {
            log.log(Level.WARNING, "commit NULL transaction");
        }
    }

    protected void rollbackIfNeeded(Objectify txn) {
        if (txn != null && txn.getTxn() != null && txn.getTxn().isActive()) {
            log.log(Level.WARNING, "transaction <" + txn.getTxn().getId() + "> rolling back");
            txn.getTxn().rollback();
        } else if (txn == null || txn.getTxn() == null) {
            log.log(Level.WARNING, "finalizing NULL transaction, not rolling back");
        } else if (!txn.getTxn().isActive()) {
            log.log(Level.FINEST, "transaction <" + txn.getTxn().getId() + "> NOT rolling back");
        }
    }


    public void deleteMyObject(Long id) {
        Objectify txn = null;
        try {
            txn = txn();
            txn.delete(new Key<MyObject>(MyObject.class, id));
            commit(txn);
        } finally {
            rollbackIfNeeded(txn);
        }
    }

    public void deleteMyObjects(Long[] ids) {
        Objectify txn = null;
        List<Key<? extends MyObject>> keys = new ArrayList<Key<? extends MyObject>>();
        for (long id : ids) {
            keys.add(new Key<MyObject>(MyObject.class, id));
        }
        try {
            txn = txn();
            txn.delete(keys);
            commit(txn);
        } finally {
            rollbackIfNeeded(txn);
        }
    }
}

When I call deleteMyObjects(Long[] ), I see nothing suspicious in the logs below. The transaction commits just fine without errors. But the data is not effected. Looping through the same list of Ids and deleting the objects one at a time, works just fine.

Feb 29, 2012 8:37:42 AM com.test.MyObjectDao txn
FINE: transaction <6> started
Feb 29, 2012 8:37:42 AM com.test.MyObjectDao commit
FINE: transaction <6> committed
Feb 29, 2012 8:37:42 AM com.test.MyObjectDao rollbackIfNeeded
FINEST: transaction <6> NOT rolling back

But the data is unchanged and present in the datastore !?!?!

Any help welcome.

UPDATE

Stepping into the Objectify code, I wonder wether this has something to do with the namespace ? Right here in the objectify code :

@Override
public Result<Void> delete(Iterable<?> keysOrEntities)
{
    // We have to be careful here, objs could contain raw Keys or Keys or entity objects or both!
    List<com.google.appengine.api.datastore.Key> keys = new ArrayList<com.google.appengine.api.datastore.Key>();

    for (Object obj: keysOrEntities)
        keys.add(this.factory.getRawKey(obj));

    return new ResultAdapter<Void>(this.ads.delete(this.txn, keys));
}

When I inspect this.factory.getRawKey(obj) in debug, I notice that the namespace of the key is empty. NamespaceManager.get() however returns the correct namespace !?


回答1:


Namespace was not set when creating the keys.

The namespace must be set BEFORE creating a key !

So rewriting it like this, fixed my problem :

public void deleteMyObjects(Long[] ids) {
    Objectify txn = null;
    try {
        txn = txn();
        List<Key<MyObject>> keys = new ArrayList<Key<MyObject>>();
        for (long id : ids) {
            keys.add(new Key<MyObject>(MyObject.class, id));
        }
        txn.delete(keys);
        commit(txn);
    } finally {
        rollbackIfNeeded(txn);
    }
}

Then I call this :

new MyObjectDAO("somenamespace").delete({ 1L, 34L, 116L });


来源:https://stackoverflow.com/questions/9495680/objectify-batch-delete-has-no-effect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!