Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variety of NHibernate errors relating to transactions during spidering

We have an ASP.Net 4 / MVC 3 hybrid web application which uses NInject 3 and (Fluent) NHibernate 3.2. DB is SQL Server 2008 R2. Server is 6-core 28 GB Windows 2008 64-bit server.

Our customer has recently started testing the site using a spidering tool. As soon as the site experiences the load produced by the spider, our log starts to fill up with exceptions.

We see a variety of errors from NHibernate, including some of the following:

  • NHibernate.TransactionException: Commit failed with SQL exception ---> System.Data.SqlClient.SqlException: The transaction operation cannot be performed because there are pending requests working on this transaction.

  • System.Data.SqlClient.SqlException (0x80131904): The server failed to resume the transaction. Desc:410000050f. The transaction active in this session has been committed or aborted by another session.

  • System.NullReferenceException: Object reference not set to an instance of an object. at System.Data.SqlClient.SqlInternalTransaction.GetServerTransactionLevel()....

  • NHibernate.Exceptions.GenericADOException: could not execute native bulk manipulation query:exec [Stats.InsertListingStatsList] @ListingStats =:ListingStats[SQL: exec [Stats.InsertListingStatsList] @ListingStats =@p0] ---> System.Data.SqlClient.SqlException: New request is not allowed to start because it should come with valid transaction descriptor.

to give just four examples. All have a similar flavour - they all seem to relate to the management of transactions by ADO.Net as the substrate of NHibernate.

Now, some details of our NH implementation:

  • SessionFactory is static;
  • SessionFactory uses AdoNetTransactionFactory;
  • ISession is in request scope, and stored in the HttpContext.Items collection;
  • Repositories are also in request scope;
  • We are now using config.CurrentSessionContext();
  • Each call to our generic repository uses a transaction

Here are two methods from our repository.

public T GetById<T>(int id)
{
    using (var t = Session.BeginTransaction())
    {
        var entity = Session.Get<T>(id);
        t.Commit();
        return entity;
    }
}

public void Add<T>(T entity)
{
    using (var t = Session.BeginTransaction())
    {
        Session.Save(entity);
        t.Commit();
    }
}

My question is simple: what is going wrong? What is causing these apparent conflicts between transactions, or between the various data-related operations that our domain instigates as it de/hydrates our domain?

UPDATE: here is our full configuration:

public FluentConfiguration BuildConfiguration(string connectionString)
{
    var sqlConfig = MsSqlConfiguration.MsSql2008.ConnectionString(connectionString).AdoNetBatchSize(30);

     var config = Fluently.Configure().Database(sqlConfig);

     var entityMapping = AutoMap.AssemblyOf<User>(new AutomappingConfiguration())
            .UseOverridesFromAssemblyOf<UserMappingOverride>()
            .AddMappingsFromAssemblyOf<TableNamingConvention>()
            .Conventions.AddFromAssemblyOf<TableNamingConvention>();

        var cqrsMapping = AutoMap.AssemblyOf<AdvertView>(new QueryAutomappingConfiguration())
            .UseOverridesFromAssemblyOf<AdvertViewMappingOverride>();

        config.Mappings(c => c.AutoMappings.Add(entityMapping));
        config.Mappings(c => c.AutoMappings.Add(cqrsMapping));

        config.Mappings(c => c.HbmMappings.AddFromAssemblyOf<AdvertView>());

        config.ExposeConfiguration(c => c.SetProperty(Environment.TransactionStrategy, typeof(AdoNetTransactionFactory).FullName));

        config.CurrentSessionContext<WebSessionContext>();

        return config;
    }

More code for you guys and gals. Here is the relevant section of our IoC Container configuration.

var domainEntityBootstrapper = new DomainEntitySessionBootStrapper("Domain", "NHibernate.ISession.Domain", _enableLucine, HttpContextItemsProvider);
Bind<ISessionFactory>().ToMethod(domainEntityBootstrapper.CreateSessionFactory).InSingletonScope().Named(domainEntityBootstrapper.Name);
Bind<ISession>().ToMethod(domainEntityBootstrapper.GetSession).InRequestScope();

var queryBootstrapper = new QueryEntitySessionBootStrapper("Query", "NHibernate.ISession.Query", HttpContextItemsProvider);
Bind<ISessionFactory>().ToMethod(queryBootstrapper.CreateSessionFactory).InSingletonScope().Named(queryBootstrapper.Name);
Bind<ISession>().ToMethod(queryBootstrapper.GetSession).WhenInjectedInto(typeof (QueryExecutor)).InRequestScope();

and here is the code from the GetSession() method of the base class for these SessionBootstrappers (please note that the CreateSessionFactory method calls the BuildConfiguration method above and then calls BuildSessionFactory()).

public virtual ISession GetSession(IContext context)
{
    var items = GetHttpContextItems();
    var session = default(ISession);
    var sessionExists = items.Contains(SessionKey);

    if (!sessionExists)
    {
        session = context.Kernel.Get<ISessionFactory>(Name).OpenSession();
        items.Add(SessionKey, session);
    }
    else
    {
        session = (ISession)items[SessionKey];
    }

    return session;
}

// a Func which serves access to the HttpContext.Current.Items collection
private Func<IDictionary> GetHttpContextItems { get; set; }

Please note that we use two sessions, one for ordinary domain de/hydration and one for CQRS, hence the pair of bindings in the Container.

like image 679
Mr Moo Avatar asked Nov 12 '22 15:11

Mr Moo


1 Answers

The error messages indicate that you are not managing transactions correctly. I think the root cause is that you are handling transactions in the repository methods which in my opinion is a very poor design. Your repositories should have an ISession injected into their constructors, and your controllers should have any repositories they are dependent upon injected into their constructors. It's easy to wire this all up with Ninject. With this approach you can use transaction-per-request or (much better imo) manage the transaction in the action methods.

Here's how I'm setting up NHibernate with Ninject in NinjectWebCommon. The root cause of your problem may be that you are binding the ISession in request scope and storing it in HttpContext, which is unnecessary. I am also confused why you have two sets of bindings for Domain and Query.

    private static void RegisterServices(IKernel kernel)
    {
        kernel.Bind<ISessionFactory>().ToProvider(new SessionFactoryProvider()).InSingletonScope();
        kernel.Bind<ISession>().ToProvider(new SessionProvider()).InRequestScope();
    }

    private class SessionFactoryProvider : Provider<ISessionFactory>
    {
        protected override ISessionFactory CreateInstance(IContext context)
        {
           // create and configure the session factory
           // I have a utility class to do this so the code isn't shown
           return nhibernateHelper.BuildSessionFactory();
        }
    }

    private class SessionProvider : Provider<ISession>
    {
        protected override ISession CreateInstance(IContext context)
        {
            var sessionFactory = context.Kernel.Get<ISessionFactory>();
            var session = sessionFactory.OpenSession();
            session.FlushMode = FlushMode.Commit;
            return session;
        }
    }

A sample controller action using a transaction. Managing transactions outside of the repositories is important for several reasons:

  • Allows multiple repositories to participate in a transaction
  • Allows the controller to set the transaction boundaries (unit of work)
  • Allows lazy loads to occur in the transaction
  • Transactions are needed for read operations if second level caching is used. Even if it caching isn't used I think it's a best practice

    public ActionResult EditDocuments(int id, string name)
    {
        using (var txn = _session.BeginTransaction())
        {
            var summary = _characterizationRepository
                .GetCharacterization(id)
                .AsCharacterizationSummaryView()
                .ToFutureValue();
    
            var documents = _characterizationRepository
                .GetCharacterization(id)
                .SelectMany(c => c.Documents)
                .OrderBy(d => d.FileName)
                .AsDocumentSelectView(true)
                .ToFuture();
    
            if (summary.Value == null)
            {
                throw new NotFoundException(_characterizationRepository.ManualId, "Characterization", id);
            }
    
            CheckSlug(name, summary.Value.Title);
    
            var model = new DocumentSectionEditView()
                {
                    CharacterizationSummary = summary.Value,
                    Documents = documents.ToArray()
                };
    
            txn.Commit();
            return View(model);
        }
    }
    
like image 63
Jamie Ide Avatar answered Nov 15 '22 12:11

Jamie Ide