GAE DatastoreService查询没有得到任何结果?

时间:2015-03-11 16:44:05

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

更新:如果我删除与交易相关的代码,它似乎有效,为什么会这样?

我在使用GAE DatastoreService的简单问候世界时遇到了麻烦。我只是想把东西放进然后列出来,下面的代码应该创建两个实体并放置它们然后查询它们并将它们列回。但是我没有获得列表,查询迭代是空的。没有错误发生。

public static void main (String[] args) throws Throwable {
  // setup HRD datastore
  LocalUserServiceTestConfig lustc = new LocalUserServiceTestConfig ();
  LocalDatastoreServiceTestConfig ldstc = new LocalDatastoreServiceTestConfig ();
  ldstc.setDefaultHighRepJobPolicyUnappliedJobPercentage (100);
  LocalServiceTestHelper helper = new LocalServiceTestHelper (lustc, ldstc).setEnvIsLoggedIn (true)
                                                                           .setEnvAuthDomain ("localhost")
                                                                           .setEnvEmail ("test@localhost");
  helper.setUp ();

  // hello world
  try {
    DatastoreService service = DatastoreServiceFactory.getDatastoreService ();
    Entity e = new Entity ("foo");
    e.setProperty ("fooey", new Text ("hello world"));
    Transaction t = service.beginTransaction (withXG (true));
    service.put (t, e);
    e = new Entity ("bar");
    e.setProperty ("bary", "goodbye world");
    service.put (t, e);
    t.commitAsync ().get ();

    System.err.println ("foo:");
    for (Entity s : service.prepare (new Query ("foo")).asIterable ())
      System.err.println (s);
    System.err.println ("bar:");
    for (Entity s : service.prepare (new Query ("bar")).asIterable ())
      System.err.println (s);
  } finally {
  // teardown
    helper.tearDown ();
  }
}

我的实际输出:

Mar 11, 2015 4:46:24 PM com.google.appengine.api.datastore.dev.LocalDatastoreService init
INFO: Local Datastore initialized: 
    Type: High Replication
    Storage: In-memory
foo:
bar:

我希望我的实体出现在那里,为什么我不能看到它们?

编辑:

我非常确定这些是制作此作品所需的maven jar:

<dependency>
  <groupId>com.google.appengine</groupId>
  <artifactId>appengine-api-1.0-sdk</artifactId>
  <version>1.9.18</version>
</dependency>
<dependency>
  <groupId>com.google.appengine</groupId>
  <artifactId>appengine-testing</artifactId>
  <version>1.9.18</version>
  <!--scope>test</scope-->
</dependency>
<dependency>
  <groupId>com.google.appengine</groupId>
  <artifactId>appengine-api-stubs</artifactId>
  <version>1.9.18</version>
  <!--scope>test</scope-->
</dependency>

1 个答案:

答案 0 :(得分:1)

所有没有父级的查询最终都是一致的。您的JUnit设置告诉app-engine永远不允许最终的一致读取。特别是这一行 -

ldstc.setDefaultHighRepJobPolicyUnappliedJobPercentage (100);

将其更改为:

ldstc.setApplyAllHighRepJobPolicy(); // 100% consistent (impractical but easy to test)

一切都会按预期工作。在生产中,您的系统将在这两个设置之间的某个位置工作。最终一致的查询将在“一段时间”后显示数据。