You have current session in listener. Then:

private static void Save(IAuditLogEntry auditLogEntry, ISession session)
{
session
.GetSession(EntityMode.Poco)
.Save(auditLogEntry);
}


On Fri, Aug 20, 2010 at 5:41 PM, Chris J <[email protected]> wrote:

>
> I am only aware of the one version of NHibernate in Action, covering
> 1.2.
>
> Yes, I reviewed the Audit Log implementation, which was a helpful
> example of how to use event listeners instead of an IInspector
> implementation. However, the piece that I'm curious about, which is
> not covered, it how to use a session within the event listener to
> write a separate record to the database. I don't think it would work
> to use the same session that is being used to perform the write that
> triggered the event. Would it work to create a StatelessSession with
> the same DbConnection as the non-stateless session that triggered the
> event?
>
>
> On Aug 20, 1:33 am, Valeriu Caraulean <[email protected]> wrote:
> > BTW, what version of NH the "NHibernate in Actions" describing? 1.2? Or
> 2.x?
> >
> > Have you looked at "Audit Log" implementation using event listeners from
> > more recent versions of NH? It may turn to easier and just better
> solution.
> > An example:
> http://nhforge.org/wikis/howtonh/creating-an-audit-log-using-nhiberna...
> >
> > On Fri, Aug 20, 2010 at 12:57 AM, Chris J <[email protected]> wrote:
> >
> > > Chapter 8 of the NiA book describes a way to use an IInterceptor
> > > (actually a subclass of EmptyInterceptor) to audit entity changes. The
> > > example uses a "temporary session pattern" by using the connection
> > > from the active session to create a new, temporary session for writing
> > > a new audit entity:
> >
> > >     foreach(object entity in inserts) {
> > >             AuditLog.LogEvent(LogType.Create, entity, userId,
> > > session.Connection);
> > >     }
> > >     ...
> >
> > >    public static void LogEvent(
> > >        LogType logType,
> > >        object entity,
> > >        long userId,
> > >        IDbConnection connection) {
> > >        using(ISession tempSession =
> > > sessionFactory.OpenSession(connection)) {
> > >            AuditLogRecord record = new
> > > AuditLogRecord(logType.ToString(),
> > >                                   tempSession.GetIdentifier(entity),
> >
> > > entity.GetType(),
> >
> > > userId);
> >
> > > tempSession.Save(record);
> >
> > > tempSession.Flush();
> > >        }
> > >    }
> >
> > > I tried implementing this, but unfortunately it does not work, at
> > > least not when the top-level session has an active transaction. When I
> > > implemented it, the Save failed with this exception:
> >
> > >      System.InvalidOperationException : ExecuteNonQuery requires the
> > > command to have a transaction when the connection assigned to the
> > > command is in a pending local transaction.  The Transaction property
> > > of the command has not been initialized.
> >
> > > Looks like this was brought up a couple of years ago:
> >
> > >http://groups.google.com/group/nhusers/browse_thread/thread/f19ff3332.
> ..
> >
> > > Is this a known issue, and is there a workaround?
> >
> > > --
> > > You received this message because you are subscribed to the Google
> Groups
> > > "nhusers" group.
> > > To post to this group, send email to [email protected].
> > > To unsubscribe from this group, send email to
> > > [email protected]<nhusers%[email protected]>
> <nhusers%[email protected]<nhusers%[email protected]>
> >
> > > .
> > > For more options, visit this group at
> > >http://groups.google.com/group/nhusers?hl=en.
> >
> >
>
> --
> You received this message because you are subscribed to the Google Groups
> "nhusers" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<nhusers%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/nhusers?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to