【问题标题】:How can achieve strong consistency of Entity without parent in JDO for datastore如何在 JDO for datastore 中实现无父实体的强一致性
【发布时间】:2014-02-12 14:34:19
【问题描述】:

这是我的简单实体:

import com.google.appengine.api.datastore.Key;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

@PersistenceCapable 

public class Food {

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key id;
    @Persistent
    private String name;
    @Persistent
    private String description;
    public Key getId() {
        return (id);
    }
    public void setId(Key id) {
        this.id = id;
    }
    public String getName() {
        return (name);
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDescription() {
        return (description);
    }
    public void setDescription(String description) {
        this.description = description;
    }
}

我正在以这种方式加载我的食物列表:

try {
            pm = PMF.get().getPersistenceManager();
            Query q = pm.newQuery(Food.class);
            foods = (List<Food>) q.execute();
        } catch (Exception exc) {
            exc.printStackTrace();
        }
        finally {
            pm.close();
        }

为每个 Food 实体自动生成密钥。我也在使用 HRD。所以...上面的查询 (pm.newQuery(Food.class);) 不具有强一致性,一次返回所有结果,第二次不返回新创建的食物。如何以这种方式更改此查询,每次我从数据库中获取所有食物。在我读过的数据存储文档中应该是祖先查询,但我没有祖先路径。 (我应该说我是数据存储区的新手,仍然从昨天开始学习,但这一次花了我很多时间)。请帮我。

【问题讨论】:

  • 不使用父级就无法实现强一致性。这就是重点。要么您使用祖先,要么您接受有时您的查询结果会过时(并使用例如 memcache 来解决这个问题)。

标签: google-app-engine google-cloud-datastore jdo


【解决方案1】:

关于应用引擎数据存储的工作原理和强一致性的两个优秀且易于理解的视频:

第 1 部分:Datastore Introduction

https://www.youtube.com/watch?v=fQazhzcC-rg

第 2 部分:数据存储区查询、索引和事务

https://www.youtube.com/watch?v=d4CiMWy0J70#t=645

【讨论】:

    【解决方案2】:

    不使用父级就无法实现强一致性。这就是重点。要么您使用祖先,要么您接受有时您的查询结果会过时(并使用例如 memcache 来解决这个问题)。

    【讨论】:

      猜你喜欢
      • 2013-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多