【问题标题】:How to enable Objectify XA transaction?如何启用 Objectify XA 事务?
【发布时间】:2013-01-21 17:47:53
【问题描述】:

我正在实现同一类型的实体之间的友谊功能Profile。此实体类型是根(非父)实体。 Profile 有一个名为 friends 的 Set<Ref<Profile>> 字段,它是 getter getFriends()。

代码如下:

public boolean makeFriends(final Profile profile1, final Profile profile2) {
    final Ref<Profile> profileRef1 = Ref.create(profile1);
    final Ref<Profile> profileRef2 = Ref.create(profile2);

    boolean result = false;

    // test to avoid useless transaction
    if (!profile1.getFriends().contains(profileRef2) && !profile2.getFriends().contains(profileRef1)) {
        // add to friends (Set<Ref<Profile>>) the Ref of each other
        result = ofy().transact(new Work<Boolean>() {
            @Override
            public Boolean run() {
                profile1.getFriends().add(profileRef2);
                profile2.getFriends().add(profileRef1);
                ofy().save().entities(profile1, profile2).now();
                return true;
            }
        });

    }
    return result;
}

这段代码给我一个:

java.lang.IllegalArgumentException: cross-group transaction need to be explicitly specified, see TransactionOptions.Builder.withXG

即使 Objectify 文档说:

Objectify 不需要特殊标志来启用跨组 交易。如果您访问一个实体组中的多个实体组 交易,交易为XG交易。如果你这样做 访问只有一个,它不是。 5 EG 的标准限制适用于所有 交易。

那么,为什么我的交易失败了?

我的代码应该涉及两个实体组(每个 Profile 一个),所以在 5 个限制之后。 查看TransactionOptions.Builder.withXG 文档,我应该先致电TransactionOptions.Builder.withXG(true);。 这个方法返回一个TransactionOptions,但我不知道传递它的方法!

提前致谢

【问题讨论】:

标签: google-app-engine transactions objectify


【解决方案1】:

如果环境支持,Objectify 总是打开 XG 事务。

很可能,您正在运行一个未启用 HRD 的测试用例。您必须在 LocalDatastoreServiceTestConfig 中明确执行此操作;检查本地单元测试文档。如果您在开发实例中收到此消息,请确保选中 eclipse 项目首选项中的“使用 HRD”复选框。

【讨论】:

  • 你是对的,像往常一样。我当前的测试用例说INFO: Local Datastore initialized: Type: Master/Slave Storage: In-memory 如果我用LocalDatastoreServiceTestConfig config = new LocalDatastoreServiceTestConfig().setDefaultHighRepJobPolicyUnappliedJobPercentage(100); 更改配置@ 说INFO: Local Datastore initialized: Type: High Replication Storage: In-memory 但所有测试都失败了:(
  • 已解决LocalDatastoreServiceTestConfig config = new LocalDatastoreServiceTestConfig().setApplyAllHighRepJobPolicy();
  • 谢谢,这对我有用,但只有在我将 .setApplyAllHighRepJobPolicy 添加到所有使用数据存储的测试类之后。
【解决方案2】:

确保使用 VM 标志在本地 AppEngine 中激活 HRD 进行测试:

-Ddatastore.default_high_rep_job_policy_unapplied_job_pct=1

【讨论】:

    猜你喜欢
    • 2015-10-01
    • 2017-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多