HI there,
I got some issues about using pre-insert and post-insert. I don't know
exactly if I got the intended behavior right.
As what I think pre event is fired before db access - post event after
db access.
But from the beginning:
I need to register the current app-user in the database before any
update / insert or delete is done. Afterwards I need to unregister him
again.
To do this not manually every time again I decided to use the listener
registration on pre and post insert / update and delete.
Two methods are doing this job:
private static void SetCurrentUser(AbstractPreDatabaseOperationEvent
@event)
{
SqlConnection connection = (SqlConnection)
((ISession)@event.Session).Connection;
using (SqlCommand command = new SqlCommand { Connection =
connection, CommandType = CommandType.StoredProcedure, CommandText =
"[system_management].[SetCurrentUser]" })
{
// Enlist IDbCommand into the NHibernate transaction
@event.Session.Transaction.Enlist(command);
/* TODO: Fest kodierten user durch Context-User
ersetzen*/
command.Parameters.AddWithValue("@user", "System");
command.ExecuteNonQuery();
}
}
private static void
UnSetCurrentUser(AbstractPostDatabaseOperationEvent @event)
{
SqlConnection connection = (SqlConnection)
((ISession)@event.Session).Connection;
using (SqlCommand command = new SqlCommand { Connection =
connection, CommandType = CommandType.StoredProcedure, CommandText =
"[system_management].[UnSetCurrentUser]" })
{
// Enlist IDbCommand into the NHibernate transaction
@event.Session.Transaction.Enlist(command);
// Execute Command
command.ExecuteNonQuery();
}
}
Doing updates work as suspected - will say: first the pre event is
fired than the update statement is executed and aftwards the post
event is fired.
But when I try to insert data first the pre event is fired than the
post event and last the insert-statement into the db. To verify the
exception I caught I used SQLProfiler to get the order of statements
fired. It confirms the order I described.
Im not sure if I got the intention right of those events - can I use
them for what I need? If yes - have you guys any suggestions why the
events are fired in such order?
I am using small repositories to do the work - the insert and update
mehtod are using session.SaveOrUpdate(). Code is pasted below:
protected virtual bool InsertOrUpdate(T @object)
{
Contract.Requires(@object != null);
try
{
using (ISession session =
SessionFactory.GetSession())
using (ITransaction transaction =
session.BeginTransaction(IsolationLevel.RepeatableRead))
{
session.SaveOrUpdate(@object);
transaction.Commit();
}
}
catch (HibernateException hibernateException)
{
this.LoggingService.Error(this.GetType(),
hibernateException);
transaction.Rollback();
return false;
}
return true;
}
Thanks for helping me out,
kind regards,
Marcus
P.S.: I am using NHibernate 2.1.2.4000
--
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.