> I understand that NH doesn't support nested transactions.
> What is the best way to manage the transaction?


I don't know what the best practice is, but I always create the transaction
in the outmost/outer caller, not in in the inner one. It can be in the view
or the controller in a MVC concept. It's the most making-sense thing to do
for me.

So for your code, I wouldn't create transaction in Handle(Ban). I would
create the transaction in the outer caller of Handle(Ban) or in the service
level.





On Wed, Apr 25, 2012 at 3:04 PM, 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.
>



-- 
Regards,

Maximilian Haru Raditya

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