【问题标题】:Playframework 1.2.* EventStream, @PostUpdate and @PostPersistPlayframework 1.2.* EventStream、@PostUpdate 和 @PostPersist
【发布时间】:2014-10-14 08:07:15
【问题描述】:

我有一个应用程序,其中一些请求允许修改某些实体,而一个请求会阻塞直到执行修改(通知客户端的更改)

我做了以下事情:

@Entity
class TheEntity extends Model {

    public static Collection<TheEntity> getLastEntities(after) {
       //finds the entities where the `lastUpdateTimestamp` is greater than `after`
       return TheEntity.find("byLastUpdateTimestampGreaterThan", after).fetch();
    }

    @PostUpdate
    @PostPersist
    private void notifyChannel() {
        lastUpdateTimestamp = System.currentTime();
        Job asyncNotifier = new Job() {
            @Override
            public void doJob() throws Exception {
                //we manage the updates. the message channel trigs the channel
                MyChannel.publish(AcEvent.this);
            }

        };
        asyncNotifier.now();
    }
}

现在,我有一个监听频道的控制器

public static void listenMessageUpdate(Long after,Long blockDuration) {
    Collection<TheEntity> evts = MyChannel.getLast(after);
    if (evts.isEmpty()) {
        Timeout timeout = F.Timeout(blockDuration*1000);
        Promise<TheEntity> nextFlt = MyChannel.nextEvent();
        Either<Timeout, TheEntity> await = await(F.Promise.waitEither(
                timeout, nextFlt));

        evts = TheEntity.getLastEntities(after);
    }
    renderJSON(TheEntity.toJson(evts));
}

事实上,TheEntity.getLastEntities(after) 在 PROD 模式下不会返回最近修改或创建的元素。

经过调查,通知事件流时未提交事务,因此,实体在数据库中不是最新的

在每次实体修改时收到通知的最佳做法是什么?

【问题讨论】:

  • 您能否将asyncNotifier.now(); 更改为asyncNotifier.in(5),以便延迟作业初始化
  • @hzog 实际上这是一个非常糟糕的主意。在生产模式下,我在使用 postgresql 时遇到了这个非确定性错误:2014-11-29 18:46:04,239 错误 ~ 发生断言失败(这可能表明 Hibernate 中存在错误,但更可能是由于使用不安全造成的会话的)我最终丢弃了 PostUpdate 注释注释并取代了 save 方法来调用 notifyChannel()

标签: jpa playframework playframework-1.x


【解决方案1】:

问题出在MyChannel.publish(AcEvent.this);这一行

事实上,AcEvent.this 是会话+事务中的一个实体。 listenMessageUpdate 属于另一个会话+事务,因此,访问不是确定性的。解决方案是将非实体的东西传递给发布。不能既不是 ID,因为它可能尚未刷新(listenMessageUpdate 执行时未提交事务)。

我们必须传递一个带有有用字段的 IO 结构

【讨论】:

    猜你喜欢
    • 2011-06-21
    • 1970-01-01
    • 1970-01-01
    • 2013-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    • 1970-01-01
    相关资源
    最近更新 更多