Nhibernate查询默认失败



我很难确定来自NHibernate(3.3)的问题。该应用程序在六节点 NLB 群集中提供大约 150 个 req/s 的服务。应用程序通常运行良好,但有时可能在 1-2 天内日志中出现以下错误,并且所有查询都失败。

我使用的是 15 adonet.batch_sizeMultipleActiveResultSets=True设置为 true。所有控制器都继承自AsyncController(这是一个 ASP.NET MVC 4 应用),因此会话状态是只读的,以最大限度地提高并发性。

最初,我们在NHibernate会话处理方面遇到了很多麻烦,因为AbstractBatcher在管理哪些读者与哪个连接相关方面做得很差。因此,在高负载下,读者会尝试从已经关闭的连接中读取。 我们通过手动定义稳定事物的 SQLConnection 的生存期来解决此问题。

但是,以下情况经常出现。我认为这是会话工厂真正出现问题的症状。此时,我正在考虑在抛出这种类型的未处理异常时重新初始化会话工厂,但这并不好。有没有人知道为什么会发生这种情况?

System.NotSupportedException: PartialEvaluationExceptionExpression
   at NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.VisitExpression(Expression expression)
   at NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.VisitBinaryExpression(BinaryExpression expression)
   at NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.VisitExpression(Expression expression)
   at NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.VisitBinaryExpression(BinaryExpression expression)
   at NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.VisitExpression(Expression expression)
   at NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.VisitBinaryExpression(BinaryExpression expression)
   at NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.VisitExpression(Expression expression)
   at NHibernate.Linq.Visitors.QueryModelVisitor.VisitWhereClause(WhereClause whereClause, QueryModel queryModel, Int32 index)
   at Remotion.Linq.Clauses.WhereClause.Accept(IQueryModelVisitor visitor, QueryModel queryModel, Int32 index)
   at Remotion.Linq.QueryModelVisitorBase.VisitBodyClauses(ObservableCollection`1 bodyClauses, QueryModel queryModel)
   at Remotion.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel queryModel)
   at NHibernate.Linq.Visitors.QueryModelVisitor.GenerateHqlQuery(QueryModel queryModel, VisitorParameters parameters, Boolean root)
   at NHibernate.Linq.NhLinqExpression.Translate(ISessionFactoryImplementor sessionFactory)
   at NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(String queryIdentifier, IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary`2 filters, ISessionFactoryImplementor factory)
   at NHibernate.Engine.Query.QueryPlanCache.GetHQLQueryPlan(IQueryExpression queryExpression, Boolean shallow, IDictionary`2 enabledFilters)
   at NHibernate.Impl.AbstractSessionImpl.GetHQLQueryPlan(IQueryExpression queryExpression, Boolean shallow)
   at NHibernate.Impl.AbstractSessionImpl.CreateQuery(IQueryExpression queryExpression)
   at NHibernate.Linq.DefaultQueryProvider.PrepareQuery(Expression expression, IQuery& query, NhLinqExpression& nhQuery)
   at NHibernate.Linq.DefaultQueryProvider.Execute(Expression expression)
   at NHibernate.Linq.DefaultQueryProvider.Execute[TResult](Expression expression)
   at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source)
对于那些

可能面临此问题的人,我想我会对我们为解决此问题而实施的修复程序的两年生产(数千名用户)提供一些见解。

在基构造函数中,我们有这样的东西:

        public MvcBaseController()
        {
            try
            {
                var currentRepositorySession = RepositorySession.Current;
                dbConnection = new SqlConnection(currentRepositorySession.GetConnectionString());
                dbConnection.Open();
                session = currentRepositorySession.Create(false, dbConnection);
            }
            catch (Exception e)
            {
                LogDebug("Error was thrown: " + e.Message);
            }
        }

强制将数据库连接绑定到 NHibernate 会话是确保AbstractBatcher不会在所有读取器完成之前关闭会话的唯一方法。关于Dispose我们有以下内容:

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            try
            {
                if (session != null)
                {
                    session.Dispose();
                    session = null;
                }
            }
            catch (Exception ex)
            {
                LogDebug("Error was thrown: " + ex.Message);
            }
            try
            {
                if (dbConnection != null)
                {
                    dbConnection.Close();
                    dbConnection.Dispose();
                    dbConnection = null;
                }
            }
            catch (Exception ex)
            {
                LogDebug("Error was thrown: " + ex.Message);
            }
        }
        base.Dispose(disposing);
    }

这意味着为每个请求打开一个连接,这很好。我们不得不增加SQL Server工作人员,但系统已经稳定了两年。我们唯一一次看到 NHibernate 会话出厂中断是当有大量数据从数据库中分页时,比如 100 万条记录。然后,应用程序池溢出,并成为孤立池。这些大多是代码错误,与问题中描述的并发问题无关。

相关内容

最新更新