Hi,
I am using PreInsert, PreUpdate and PreDelete Listeners to save audit records. I have two problems with my current implementation:
1. OnPreInsert, I don't know what the Id of the entity is, as the Id is generated using an identity field in the database. I will try using a PostInsertEvent listener instead. Is this a good solution?
2. I am saving the audit record like this, from within the OnPreInsert, OnPreUpdate and OnPreDelete methods:
Code:
// Oops, just noticed this is not disposed!
IStatelessSession newSession = updateEvent.Persister.Factory.OpenStatelessSession();
using (ITransaction transaction = newSession.BeginTransaction())
{
newSession.Insert(auditRecord);
transaction.Commit();
}
Unsurprisingly, I found that the audit record is committed to the database even if the transaction doing the update etc is rolled back. I would like the audit record to be committed if and only if the associated transaction committed. What is the recommended way of doing this?
We have tried saving the audit record using a child session of the original session as shown below, but there were errors (InvalidOperationException: collection was modified, enumeration operation may not execute):
Code:
ISession newSession = updateEvent.Source.PersistenceContext.Session.GetSession().GetSession(EntityMode.Poco);
newSession.Save(auditRecord);
I am using NHibernate 2.0.1
Thanks,
Alice