我的情况是,在某些NHibernate实体更新后,我向RabbitMQ发送消息。理想情况下,我希望这些消息的消费者能够看到反映更新的实体版本。
我决定尝试通过实现IPostUpdateEventListener
来引发事件,认为这将确保消费者看到最新的行,,但我似乎有一个竞争条件,消息消费者有时在其行在数据库中更新之前加载实体。
IPostUpdateEventListener
接口上的文档似乎表明,在数据存储更新后,代码运行,但情况似乎并非如此。
我打开Sql Server管理工作室,NhProf,做了一些调试。我通过步进代码验证了我的OnPostUpdate
代码在实体在数据库中的行更新之前运行。
我正在更新事务中的实体,代码类似于这样…
using(var transaction = session.BeginTransaction()) {
session.SaveOrUpdate(entity);
transaction.Commit();
}
我是否误解或误用了IPostUpdateEventListener
?我是否可以使用另一个"钩子"来排队消息,以确保发生了更新?
如果您正在使用事务,那么我使用以下事件来做到这一点:
post-commit-insert
post-commit-update
post-commit-delete
配置示例:
<listener class="yournamespace.youreventhandler, yournamespace" type="post-commit-insert" />
<listener class="yournamespace.youreventhandler, yournamespace" type="post-commit-update" />
<listener class="yournamespace.youreventhandler, yournamespace" type="post-commit-delete" />