4

I have the following wrapper:

 public interface ITransactionScopeWrapper : IDisposable
{
    void Complete();
}

public class TransactionScopeWrapper : ITransactionScopeWrapper
{
    private readonly TransactionScope _scope;
    private readonly ISession _session;
    private readonly ITransaction _transaction;

    public TransactionScopeWrapper(ISession session)
    {
        _session = session;
        _scope = new TransactionScope(TransactionScopeOption.Required,
                                      new TransactionOptions {IsolationLevel = IsolationLevel.ReadCommitted});
        _transaction = session.BeginTransaction();
    }

    #region ITransactionScopeWrapper Members

    public void Dispose()
    {
        try
        {
            _transaction.Dispose();
        }
        finally
        {
            _scope.Dispose();
        }
    }

    public void Complete()
    {
        _session.Flush();
        _transaction.Commit();
        _scope.Complete();
    }

    #endregion
}

In my ActionFilter I have the following:

 public class NhibernateTransactionAttribute : ActionFilterAttribute
{
    public ITransactionScopeWrapper TransactionScopeWrapper { get; set; }
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
    }
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        TransactionScopeWrapper.Complete();

        base.OnActionExecuted(filterContext);
    }

}

I am using Castle to manage my ISession using a lifestyle of per web request:

container.Register(
            Component.For<ISessionFactory>().UsingFactoryMethod(
                x => x.Resolve<INHibernateInit>().GetConfiguration().BuildSessionFactory()).LifeStyle.Is(
                    LifestyleType.Singleton));

container.Register(
            Component.For<ISession>().UsingFactoryMethod(x => container.Resolve<ISessionFactory>().OpenSession()).
                LifeStyle.Is(LifestyleType.PerWebRequest));

container.Register(
          Component.For<ITransactionScopeWrapper>().ImplementedBy<TransactionScopeWrapper>().LifeStyle.Is(
              LifestyleType.PerWebRequest));

So now on to my questions.

  1. Any issues with managing the transaction this way
  2. Does an ActionFilter OnActionExecuting and OnActionExecuted methods use the same thread.

I ask number 2 because BeginRequest and EndRequest are not guaranteed to operate on the same thread and if you toss transactions on them you will run into big problems.

In my ActionFilter TransactionScopeWrapper is property injected.

5
  • Probably best to move BeginTransaction() into OnActionExecuting, otherwise you're starting the transaction a little earlier than necessary. AFAIK ActionFilter events are executed on the same thread and the filter is only created once for each request.
    – dotjoe
    Mar 21, 2012 at 21:38
  • @dotjoe easy enough I will add a BeginTransaction Method instead of doing it from the ctor Mar 21, 2012 at 21:46
  • I don't really care for property injection but neither does anyone else. Yay to Microsoft for that one. Mar 21, 2012 at 23:43
  • You should be able to do ctor injection with an ActionFilter. ninject.web.mvc can do it.
    – dotjoe
    Mar 22, 2012 at 1:42
  • I miss that about Ninect. Castle wont do it :( Mar 22, 2012 at 2:52

2 Answers 2

5

There are some other aspects you should also look into.

First I would say is to decide where to dispose of your transaction. Be aware that if you use lazy loading and pass a data entity back to your view and access a property or reference that is configured to be lazy loaded, you'll encounter problems because your transaction has already been closed in your OnActionExecuted. Though as much as I know you should only use viewmodels in your views, sometimes an entity is a little more convenient. Regardless of the reason if you do want to use lazy loading and access them in your views you'll have to move your transaction completion into the OnResultExecuted method so that it doesn't get prematurely committed.

Second you should also look into checking if there were any exceptions or model errors before committing your transaction. I ended up using inspiration from here and here for my final Filter for dealing with my nHibernate Transaction.

Third, if you decide to dispose of your transaction in the OnResultExecuted handler that you do not do so if it's a request for a child actions. The reason being that like you I scoped my session to the web request, but I found that child actions don't count as a new request and when they are called and they try to open their own session they were getting the already open session context instead. When the child action then completed it was trying to close ITS session but was actually closing the session used by the parent view as well. This caused any logic after the child action that relied on lazy loaded data to fail as well.

I'd like to go through and try to remove my lazy loaded data from my app when it comes to views but until I get the time to do so you should be aware of these issues that may come up.

I was going to post my own action filter when I realized I had some DRY issues I needed to fix. suffice to say I am checking that filterContext.Exception and filterContext.ExceptionHandled to see if there were any errors and if they have been handled already. Note that just because an exception was handled doesn't mean that your transaction is OK to be committed. And though this is more subjective to how your app is coded you may also want to check filterContext.Controller.ViewData.ModelState.IsValid before your commit your transaction as well.

UPDATE: Unlike you, I'm using StructureMap, not Castle for Dependency Injection but in my case I added this line to my Application_EndRequest method in the gobal.asax file as a final bit of cleanup. I'm assuming there is something similar in Castle? StructureMap.ObjectFactory.ReleaseAndDisposeAllHttpScopedObjects();

UPDATE 2: Anyway, a more direct answer to your question. I don't see anything wrong with using a wrapper like you opt'd to, though I am not sure why you feel the need to wrap it? nHibernate does a really good job of handling the transaction itself so another abstraction layer around that seems unneeded to me. You could just as easily explicitly start the transaction in your OnActionExecuting and explicitly complete it in the OnActionExecuted. By retrieving the ISession object through the DependencyResolver you eliminate any concerns you may have with threading as the IoC container is thread-safe I believe, and from there you can get your current transaction using Session.Transaction and check it's current state from the IsActive property. My understanding is that it's possible for the two methods to occur on different threads though, particularly when dealing with an action on a class inheriting from AsynController.

9
  • 1.) I would never pass an entity even to my controllers. I have a "Service" layer that takes an injected repository and the controllers call the service layer like _userService.GetUser(_userDto); userDto being a Data Transfer Object. If I need to get data to my ViewModels I use automapper which is an awesome tool. I will look into the ChildAction potential issue, I currently don't have any as I use quite a bit of AJAX in my web ui. Mar 21, 2012 at 23:34
  • I use Castle and it is great about cleanup Mar 21, 2012 at 23:38
  • As far as your bit about filterContext.Controller.ViewData.ModelState.IsValid. Just because I start a transaction does not mean I am doing anything with the Nhibernate Session if the modelstate was not valid. Transactions are cheap. Sessions are cheap for that matter which is why I let Castle handle them per webrequest Mar 21, 2012 at 23:41
  • +1 lots of good points here. I remember trying to write a Filter to handle child action session/transactions...I gave up and simply found that I had no real need for a child action.
    – dotjoe
    Mar 22, 2012 at 1:49
  • I do prefer closed sessions in view...leads to heavy use of AutoMapper, lots of specific viewmodels, and projecting queries into dto. Using the actual entities in the view can get nasty when you need to do null checks while digging into object graph.
    – dotjoe
    Mar 22, 2012 at 1:57
0

I've got a problem with a such method. What it do if you use "@Html.Action("TestMethod", "TestController")" ?

As for me I prefer to use explicit transaction call:

using (var tx = session.BeginTransaction())
{
    // perform your insert here
    tx.Commit();
}

What's about threadsafe, I'd like to know too.

1
  • I wrap the Nhibernate transaction in an Ambient _scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions {IsolationLevel = IsolationLevel.ReadCommitted}); Mar 22, 2012 at 2:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.