Objectify BATCH删除没有效果 [英] Objectify BATCH delete has no effect

查看:134
本文介绍了Objectify BATCH删除没有效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面有一个DAO,事务性删除每个实体批量
一次删除一个实体可以很好地工作。

批量删除没有任何效果:
下面的代码很简单直接的IMO,但是调用 deleteMyObjects(Long [] ids) - 调用 Objectify 删除(Iterable keysOrEntities) - 无效!

  public class MyObjectDao {

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

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

private String namespace = null;
$ b $ public MyObjectDao(String namespace){
Preconditions.checkNotNull(namespace,命名空间不能为NULL);
this.namespace = namespace;

$ b $ **
*设置命名空间并获得一个非事务性的Objectify实例
*
* @return
* /
protected Objectify nontxn(){
NamespaceManager.set(namespace);
return ObjectifyService.factory()。begin(nonTransactional);

$ b / **
*设置命名空间并得到Objectify的事务实例
*
* @return
* /
保护Objectify txn(){
NamespaceManager.set(namespace);
Objectify txn = ObjectifyService.factory()。begin(transactional);
log.log(Level.FINE,transaction<+ txn.getTxn()。getId()+> started);
返回txn;
}

protected void commit(Objectify txn){
if(txn!= null& txn.getTxn()。isActive()){
txn.getTxn()提交();
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()+>回滚);
txn.getTxn()。rollback();
} else if(txn == null || txn.getTxn()== null){
log.log(Level.WARNING,完成NULL事务,不回滚);
} else if(!txn.getTxn()。isActive()){
log.log(Level.FINEST,transaction<+ txn.getTxn()。getId()+> ;不回滚);
}
}


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

public void deleteMyObjects(Long [] ids){
Objectify txn = null;
列表<键<?扩展MyObject>> keys = new ArrayList< Key<?扩展MyObject>>();
for(long id:ids){
keys.add(new Key< MyObject>(MyObject.class,id));
}
尝试{
txn = txn();
txn.delete(keys);
commit(txn);
} finally {
rollbackIfNeeded(txn);


$ b $ / code>

当我调用deleteMyObjects(Long []),我在下面的日志中看不到任何可疑内容。事务提交没有错误。但数据不受影响。循环使用相同的ID列表并一次删除一个对象,工作得很好。

  2012年2月29日8: 37:42 AM com.test.MyObjectDao txn 
FINE:transaction< 6>开始
Feb 29,2012 8:37:42 AM com.test.MyObjectDao commit
FINE:transaction< 6> commit
2012年2月29日上午8时37分42秒com.test.MyObjectDao rollbackIfNeeded
FINEST:transaction< 6>不回滚

但是数据没有变化,并且存在于数据存储区中!?!?!



欢迎任何帮助。

更新



进入Objectify代码,我想知道这与名称空间有关吗?在这里的objectify代码中:

  @Override 
public Result< Void>删除(Iterable <?> keysOrEntities)
{
//我们在这里必须小心,objs可以包含原始键或键或实体对象或两者!
列表< 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));

返回新的ResultAdapter< Void>(this.ads.delete(this.txn,keys));
}

当我检查 this.factory.getRawKey(obj)在调试中,我注意到该键的名称空间是空的。然而, NamespaceManager.get()返回正确的命名空间!





命名空间必须在创建密钥之前设置!



所以像这样重写它,解决了我的问题:

  public void deleteMyObjects(Long [] ids){
Objectify txn = null;
尝试{
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);


然后我称之为:

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


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 !?

解决方案

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 });

这篇关于Objectify BATCH删除没有效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆