0

在 MVC 4 应用程序中使用 .NET 4、LINQtoSQL 我刚刚开始遇到大量请求失败的问题。请求似乎在从数据库中进行非常简单的选择时失败。服务器或代码库已经有大约一周没有更新了,但这个问题直到今天早上才开始出现。
异常是(看似)随机抛出的,它可能发生在所有页面上。
事件查看器日志显示以下堆栈跟踪:

Exception information: 
    Exception type: TransactionException 
    Exception message: The operation is not valid for the state of the transaction.
   at System.Transactions.TransactionState.EnlistPromotableSinglePhase(InternalTransaction tx, IPromotableSinglePhaseNotification promotableSinglePhaseNotification, Transaction atomicTransaction)
   at System.Transactions.Transaction.EnlistPromotableSinglePhase(IPromotableSinglePhaseNotification promotableSinglePhaseNotification)
   at System.Data.SqlClient.SqlInternalConnection.EnlistNonNull(Transaction tx)
   at System.Data.ProviderBase.DbConnectionInternal.ActivateConnection(Transaction transaction)
   at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)
   at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
   at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
   at System.Data.SqlClient.SqlConnection.Open()
   at System.Data.Linq.SqlClient.SqlConnectionManager.UseConnection(IConnectionUser user)
   at System.Data.Linq.SqlClient.SqlProvider.get_IsSqlCe()
   at System.Data.Linq.SqlClient.SqlProvider.InitializeProviderMode()
   at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
   at System.Data.Linq.Table`1.System.Linq.IQueryProvider.Execute[TResult](Expression expression)
   at System.Linq.Queryable.SingleOrDefault[TSource](IQueryable`1 source, Expression`1 predicate)
   at MYPROJECT.Services.Concrete.SettingsService.GetString(Settings settingID, String defaultValue) in c:\TeamCity\buildAgent\work\123456789\MYPROJECT.Services\Concrete\SettingsService.cs:line 40
   at MYPROJECT.Controllers.BaseController.OnActionExecuted(ActionExecutedContext filterContext) in c:\TeamCity\buildAgent\work\123456789\MYPROJECT\Controllers\BaseController.cs:line 37
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass25.<>c__DisplayClass2a.<BeginInvokeAction>b__20()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult)
   at System.Web.Mvc.Controller.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar)
   at System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar)
   at System.Web.Mvc.MvcHandler.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar)
   at System.Web.Mvc.HttpHandlerUtil.ServerExecuteHttpHandlerWrapper.<>c__DisplayClass4.<Wrap>b__3()
   at System.Web.Mvc.HttpHandlerUtil.ServerExecuteHttpHandlerWrapper.Wrap[TResult](Func`1 func)
   at System.Web.HttpServerUtility.ExecuteInternal(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage, VirtualPath path, VirtualPath filePath, String physPath, Exception error, String queryStringOverride)

中间的方法是我的代码,基本上是返回这个:

dc.Settings.SingleOrDefault(x => x.SettingID == settingID)

从谷歌搜索我读到检查数据库连接并确保 MSDTC 正在运行。是的,但这导致我检查服务器上的时间。SQL 和 Web 服务器之间的差异约为 5 秒。所以我认为这可能是相关的,但是在重新同步到域控制器后,我似乎仍然有这个问题!任何帮助将非常感激。

更新更多信息
值得一提的是我正在使用 NInject。所以数据上下文的处理方式如下:

Bind<IUnitOfWork>().To<UnitOfWork>().InRequestScope().WithConstructorArgument("connectionString", connectionString); 

然后我的所有服务都能够引用相同的数据上下文。

4

1 回答 1

0

时间狩猎是一场野鹅追逐。问题出在代码上。发生了 ChangeConflictException,随后的请求仍在同一数据上下文中执行。我不知道如何,但每当发生这种情况时,它会使一些请求在最简单的数据库调用中完全失败(如上所示)。取出有问题的代码完全解决了这个问题。

于 2013-05-14T09:08:32.753 回答