Google App Engine-不能在一次交易中对多个实体组进行操作 [英] Google App Engine - can't operate on multiple entity groups in a single transaction

查看:113
本文介绍了Google App Engine-不能在一次交易中对多个实体组进行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果两个实体的类型均为PersistentLogin,为什么会收到以下异常消息?我以为那意味着他们在同一个实体组中,但是我想这是一个错误的假设.有任何解决方法的想法吗?

Why am I receiving the exception below if the type of both entities is PersistentLogin? I thought that would mean they are in the same entity group, but I guess that is an incorrect assumption. Any ideas how to fix this?

这是代码:

// the class is marked with @Transactional
@Override
public final void removeUserTokens(final String username) {
    final Query query = entityManager.createQuery(
        "SELECT p FROM PersistentLogin p WHERE username = :username");
    query.setParameter("username", username);

    for (Object token : query.getResultList()) {
        entityManager.remove(token);
    }
}

这是一个例外:

Caused by: javax.persistence.PersistenceException: Illegal argument
    at org.datanucleus.jpa.NucleusJPAHelper.getJPAExceptionForNucleusException(NucleusJPAHelper.java:260)
    at org.datanucleus.jpa.EntityTransactionImpl.commit(EntityTransactionImpl.java:122)
    at org.datanucleus.store.appengine.jpa.DatastoreEntityTransactionImpl.commit(DatastoreEntityTransactionImpl.java:50)
    at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:467)
    ... 42 more
Caused by: java.lang.IllegalArgumentException: can't operate on multiple entity groups in a single transaction. found both Element {
  type: "PersistentLogin"
  name: "1WfCYx8bmwUGkjzP2PpmFA=="
}
 and Element {
  type: "PersistentLogin"
  name: "SfI0P8RVBjTvu0WHMSuaVA=="
}

推荐答案

相同的实体组意味着这些实体具有一个公共的父实体.

Same entity group means that the entities have a common parent entity.

两个顶级实体永远不在同一实体组中.

Two top-level entities are never in the same entity group.

有什么办法解决这个问题吗?

Any ideas how to fix this?

最简单的方法是放宽交易要求.在您的情况下,这意味着要一一删除PersistentLogin实体(尽力而为循环,尽可能多地删除,重试错误,不能保证原子性).

The easiest way is to relax the transaction requirements. In your case that would mean deleting the PersistentLogin entities one by one (best effort loop, delete as much as you can, retry on errors, no guarantee of atomicity).

如果要将PersistentLogin用于同一用户放在同一实体组中,则需要对数据模型进行相当大的更改,而对整体性能的影响不确定.

If you wanted to put the PersistentLogin for the same user in the same entity group, you would need to make rather big changes to your data model, with uncertain impact on overall performance.

对于非关系数据库,您必须在不依赖数据存储上的事务的情况下对应用程序进行编码.他们只是不支持我们已经习惯的程度.

With non-relational databases, you have to code your application without relying on transactions on the data store. They just do not support them to the extent that we have become used to.

这篇关于Google App Engine-不能在一次交易中对多个实体组进行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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