These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: How should I save audit records?
PostPosted: Thu Aug 13, 2009 12:22 am 
Newbie

Joined: Tue Mar 31, 2009 8:27 pm
Posts: 2
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.