The ban handler is in-process. Either the ban handler is part of the same
unit of work or it is not. If it is not then it should not share the same
session. It these are seperation actions then these actions should be
performed sequentially to avoid possible transaction deadlocks.

Try to seperate the transaction management and session creation/disposing
logic from your application logic. This helps when you want to combine
several tasks in the same unit of work or reuse a task.

Ramon

On Wed, Apr 25, 2012 at 10:04 AM, Alex Vilela <[email protected]> wrote:

> I have a problem with Transaction management.
> How should that be done with NHibernate in a Windows Service application?
> The session management is OK, I am struggling with the transactions.
>
>
> Full explanation:
>
> I have a Windows Service application to moderate a chat system.
> If a user sends too many span messages this chat system decides to ban
> this user.
>
> MessageReceiver
> SessionManager (OpenSession(), bind to context)
>
> BanHandler
>  protected void Handle(Ban action)
>  {
>     var session = SessionFactory.GetCurrentSession();
>     using (var tx = session.BeginTransaction())
>     {
>        //Load user
>        //Update entity
>        //Save
>
>        tx.Commit();
>     }
>  }
>
> All works fine.
>
> There is also an Admin application where I can decide to ban someone. This
> is then saved as a message on the DB (no ban happens now).
>
> The Windows Service then handles this message:
>
> SessionManager (OpenSession(), bind to context)
> QueueManager (consumes the DB messages saved from the Admin Application)
>     var session = SessionFactory.GetCurrentSession();
>     using (var tx = session.BeginTransaction())
>     {
>         //Load next message from DB
>         var nextAction = session...;
>
>         //Tell the BanHandler to do its stuff
>         banHandler.Handle(nextAction);
>
>         //Mark the message as completed and save it
>         nextAction.SetAsCompleted();
>         session.SaveOrUpdate(nextAction);
>
>
> //THE PROBLEM HAPPENS HERE! The transaction was already committed by the 
> BanHandler!
>         tx.Commit();
>     }
>
> I understand that NH doesn't support nested transactions.
> What is the best way to manage the transaction?
>
>
> I've googled a lot and found some useful resources targeting NH2.0, and I
> don't know if that is still the case to NH 3.x, hence this question.
> I want to avoid reinventing the weel and want to use the most from NH.
>
> I am using NHib 3.3 (just upgraded from 3.2).
>
>
> Alex Vilela
>
> --
> 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.
>



-- 
Ramon

-- 
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