audit listener using IFlushEntityEventListener (continuation of NH-2596)

825 views
Skip to first unread message

Filip Kinsky

unread,
May 11, 2011, 8:01:32 AM5/11/11
to nhu...@googlegroups.com
The initial problem is in detail described on http://216.121.112.228/browse/NH-2596 . In short - I need to implement last modification time tracking for my entities, but IPreInsert/UpdateEventListener approach which is suggested on many places doesn't fully work. The problem is mainly with dirty entities flushed implicitly using NH dirty entities tracking like this:

using(var session = fac.OpenSession())
{
  var e = session.Load<MyEntity>(123);
  e.Value++;
}

In this case IPreInsert/UpdateEventListener aren't fired at all and so the modification time isn't logged. I was suggested by Fabio to use IFlushEntityEventListener for this scenario, which I just did, but I face another problem now - "HibernateException: Found shared references to a collection" when I try to update any entity which contains a mapped collection. My IFlushEntityEventListener implementation looks like this:

public class SetModificationDateListener: DefaultFlushEntityEventListener
{
public CurrentDateTimeDelegate CurrentDateTime { get; set; }

private void SetModificationDateIfPossible(object entity)
{
var trackable = entity as ITrackModificationDate;
if (trackable != null)
{
trackable.LastModified = CurrentDateTime();
}
}

public override void OnFlushEntity(FlushEntityEvent @event)
{
if (@event.EntityEntry.Status != Status.Deleted &&
(!@event.EntityEntry.ExistsInDatabase ||@event.Session.IsDirtyEntity(@event.Entity)))
{
SetModificationDateIfPossible(@event.Entity);
}

base.OnFlushEntity(@event);
}
}

The exception occurs on base.OnFlushEntity(@event) call. I was already trying to implement just pure IFlushEntityEventListener, but it looks like it disables default IFlushEntityEventListener and no changes are persisted to DB at all.

Anybody has any suggestion what should I try to create a mechanism for modification time tracking in NH? IPreInsert/UpdateEventListener approach is widely suggested on many places, but it obviously doesn't fully work.

Scott Findlater

unread,
May 11, 2011, 8:39:56 AM5/11/11
to nhu...@googlegroups.com

Filip Kinsky

unread,
May 11, 2011, 8:41:58 AM5/11/11
to nhu...@googlegroups.com
I'll take a look on the thread, thanks for the link..

Meanwhile I added my test-case source codes on github if anyone would be interested: https://github.com/Buthrakaur/NHListenerTests
Message has been deleted

Filip Kinsky

unread,
May 11, 2011, 10:41:53 AM5/11/11
to nhu...@googlegroups.com

José F. Romaniello

unread,
May 11, 2011, 10:45:41 AM5/11/11
to nhu...@googlegroups.com
Hi Filip, what hapen if you put your event *after* the nhibernate events (instead of before)?

2011/5/11 Filip Kinsky <fi...@filovo.net>

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

José F. Romaniello

unread,
May 11, 2011, 10:45:55 AM5/11/11
to nhu...@googlegroups.com
(i mean the your handler)...

2011/5/11 José F. Romaniello <jfroma...@gmail.com>

Filip Kinský

unread,
May 11, 2011, 10:53:33 AM5/11/11
to nhu...@googlegroups.com
I guess by *after* you mean this:

listeners.FlushEntityEventListeners = listeners.FlushEntityEventListeners.Concat(new IFlushEntityEventListener[] {new MyModificationTimeTrackingListener()}).ToArray();

That's just the problem I was facing before and wasn't aware of. It looks like the last IFlushEntityEventListener is responsible for persisting data into DB, so if the auditing listener is the last one and not the DefaultFlushEntityEventListener (which is probably added by NH as default listner) than data isn't peristed into DB as they should in some cases. In this case two tests were failing in this test: https://github.com/Buthrakaur/NHListenerTests/blob/master/NHListenerTest/SetModificationDateListenerTests.cs - InheritedThing_LastModified_Should_BeSetOnUpdate + InheritedThing_LastModified_Should_BeSetOnImplicitUpdate.

José F. Romaniello

unread,
May 11, 2011, 11:10:38 AM5/11/11
to nhu...@googlegroups.com
Sorry filip you are right, i downloaded your example in github and all tests are in green.. Did you find the solution to your problem or missed something?

2011/5/11 Filip Kinský <fi...@filovo.net>

Fabio Maulo

unread,
May 11, 2011, 11:41:38 AM5/11/11
to nhu...@googlegroups.com
He done what I said instead do what he said.

José F. Romaniello

unread,
May 11, 2011, 12:56:33 PM5/11/11
to nhu...@googlegroups.com
Hi Filip.. I modified your example adding a "ModifiedBy" property which is a Many-To-One relationship, I did this to answer the "bizzare" thread.
OTOH; in my branch i have enabled logging.. if you run your tests you will see than there is an extra-superflous update when doing only inserts,
To fix this, i believe you have to implement ISaveOrUpdateEventListener in the same listener, call the same function and add the listener to the configuration just as you did the other one.

The listener i have added for doing the User thing already has this.
Note that i am calling SaveOrUpdate instead or Save in my tests.

I hope you can merge my pull request.
2011/5/11 Filip Kinsky <fi...@filovo.net>

--

Filip Kinsky

unread,
May 11, 2011, 1:12:30 PM5/11/11
to nhu...@googlegroups.com
Thanks - I'll take a look on the extra-superflous update...

José F. Romaniello

unread,
May 11, 2011, 1:41:50 PM5/11/11
to nhu...@googlegroups.com
I fixed two superflous update... is all on this branch:
(this doesn't have the previous changes... ModifiedBy)

one of the superflous update was caused because you missed an "Inverse" in the collection
the second one for the listener thing i said in my previous mail.

My test, use a FakeAppender (log4net) to count the queries;

the code was doing 5 queries, and now it does 3.




2011/5/11 Filip Kinsky <fi...@filovo.net>
Thanks - I'll take a look on the extra-superflous update...

--
Reply all
Reply to author
Forward
0 new messages