【发布时间】:2010-07-21 18:39:00
【问题描述】:
我们已经通过 NHibernate 事件监听器实现了一个审计系统。在我们的侦听器中,我们跟踪所有更改并将它们写到我们的审计表中。为了尽量提高性能,我们在审计表中使用了 Guid,以便我们可以尽可能多地批量更新。
我们正在写出对“子会话”的更新,如下所示:
protected ISession GetSession(AbstractEvent @event)
{
if (@event == null)
{
throw new ArgumentNullException("event");
}
ISession childSession = @event.Session.GetSession(EntityMode.Poco);
return childSession;
}
根据 NHibernate 文档,该会话应该是一个“子”会话,它继承其父级的所有属性 - 包括事务。
创建实体后,我们将其保存到会话中:
childSession.Save(auditLogEntry);
所有这些都是在事务中调用的,我希望对 childSession 所做的更改会在事务提交后刷新。不幸的是,什么都没有发生,更改也没有刷新。
应该注意的是,我可以在保存后立即使用手动刷新,但这对我们不起作用,因为更改将不再被批处理(这将产生不可接受的性能)。
起初我认为这种行为仅限于事件,但我能够将其抽象为单元测试以复制行为。
public void When_Saving_Audit_Log_Records_To_Child_Session_Flushes_When_Transaction_Committed()
{
ISession session = GetSession();
session.FlushMode = FlushMode.Commit;
ITransaction transaction = session.BeginTransaction();
ISession childSession = session.GetSession(EntityMode.Poco);
AuditLogEntry entry = CreateAuditLogEntry();
entry.AddAuditLogEntryDetail(CreateAuditLogEntryDetail());
entry.AddAuditLogEntryDetail(CreateAuditLogEntryDetail());
childSession.Save(entry);
transaction.Commit();
}
protected ISession GetSession()
{
return _sessionFactory.OpenSession();
}
我知道这不是您的 NHibernate 问题,但如果有人有任何经验或建议可以分享,我很想听听。
我距离将审计记录写入队列还有 2 秒的时间,但我想在放弃之前用尽所有可能性。
提前致谢,
史蒂夫
【问题讨论】:
标签: nhibernate