频繁的TransactionException用于简单查询

时间:2013-05-13 10:31:23

标签: .net linq-to-sql

在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。所以datacontext的处理方式如下:

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

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

1 个答案:

答案 0 :(得分:0)

时间的追捕是一场疯狂的追逐。问题在于代码。发生了ChangeConflictException,然后后续请求仍在同一个datacontext中执行。我不知道如何但是无论何时发生这种情况都会使一些请求在最简单的数据库调用中完全失败(如上所示)。取出违规代码完全解决了问题。