对于繁重的应用程序,我的linq在PerCall实例化模式下由WCF服务访问.这个应用程序有几个调用服务的线程,并且有几个应用程序同时运行.
我经常发生一些EntityException:
System.Data.EntityException was caught
Message=An error occurred while starting a transaction on the provider connection. See the inner exception for details.
Source=System.Data.Entity
StackTrace:
at System.Data.EntityClient.EntityConnection.BeginDbTransaction(IsolationLevel isolationLevel)
at System.Data.EntityClient.EntityConnection.BeginTransaction()
at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
at Infoteam.GfK.TOMServer.DataServer.DataServer.SaveChanges() in D:\Workspace\XYZWASDF\DataServer\DataServer.cs:line 123
InnerException: System.Data.sqlClient.sqlException
Message=Une nouvelle transaction n’est pas autorisée parce que d’autres threads sont en cours d’exécution dans la session.
Source=.Net sqlClient Data Provider
ErrorCode=-2146232060
Class=16
LineNumber=1
Number=3988
Procedure=””
Server=ift-srv114
State=1
StackTrace:
at System.Data.sqlClient.sqlConnection.OnError(sqlException exception,Boolean breakConnection)
at System.Data.sqlClient.sqlInternalConnection.OnError(sqlException exception,Boolean breakConnection)
at System.Data.sqlClient.TdsParser.ThrowExceptionAndWarning()
at System.Data.sqlClient.TdsParser.Run(RunBehavior runBehavior,sqlCommand cmdHandler,sqlDataReader dataStream,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj)
at System.Data.sqlClient.TdsParser.TdsExecuteTransactionManagerRequest(Byte[] buffer,TransactionManagerRequestType request,String transactionName,TransactionManagerIsolationLevel isoLevel,Int32 timeout,sqlInternalTransaction transaction,TdsParserStateObject stateObj,Boolean isDelegateControlRequest)
at System.Data.sqlClient.sqlInternalConnectionTds.ExecuteTransactionYukon(TransactionRequest transactionRequest,IsolationLevel iso,sqlInternalTransaction internalTransaction,Boolean isDelegateControlRequest)
at System.Data.sqlClient.sqlInternalConnectionTds.ExecuteTransaction(TransactionRequest transactionRequest,String name,Boolean isDelegateControlRequest)
at System.Data.sqlClient.sqlInternalConnection.BeginsqlTransaction(IsolationLevel iso,String transactionName)
at System.Data.sqlClient.sqlInternalConnection.BeginTransaction(IsolationLevel iso)
at System.Data.sqlClient.sqlConnection.BeginDbTransaction(IsolationLevel isolationLevel)
at System.Data.Common.DbConnection.BeginTransaction(IsolationLevel isolationLevel)
at System.Data.EntityClient.EntityConnection.BeginDbTransaction(IsolationLevel isolationLevel)
InnerException:
(对不起,它不太可读). (内部异常的消息表示“由于会话中正在运行其他线程,因此不允许新事务.”
我已经检查过,我不是在一个循环中,当它出现这种异常时它是纯粹随机的,我不知道如何避免这种情况.
任何帮助将非常感激:)
谢谢!
编辑:这是我得到这个例外的例子有时候
//My DataServer method,which is a singleton [MethodImpl(MethodImplOptions.Synchronized)] public void SaveChanges() { lock (_lockObject) { try { _context.SaveChanges(SaveOptions.AcceptAllChangesAfterSave); _changeListener.ManageIDAfterInsert(); } catch (Exception ex) { Logger.Instance.Error("[DataServer:SaveChanges] Got an error when trying to save an object",ex); //HERE I've this error } } } //One example where I can have exception sometimes,this is called through a WCF service,so I have a method which attach the object and then save it private OrderStatus AddOrderStatus(OrderStatus orderStatus) { DataServer.DataServer.Instance.InsertOrUpdateDetachedObject(orderStatus); return orderStatus; }
解决方法
如果您想要更具体的答案,您的代码将有所帮助.
编辑
您的问题是两个线程正在尝试同时使用上下文,或者一个线程正在打开一个事务,然后第二个线程尝试使用您的单例上下文.您的代码片段为我提出了比以前更多的问题.
>在您的代码示例中_lockObject是一个静态变量?>为什么在SaveChanges中显示锁定,然后解释正在抛出错误来自InsertOrUpdateDetachedObject?我们能看到InsertOrUpdateDetachedObject的代码吗?> SaveChanges是否使用与直接访问上下文的所有其他方法相同的_lockObject?>您对_context.SaveChanges的调用是您保存到数据库的唯一方式,还是您有自己打开事务上下文的其他区域?>您使用单例,以便在多个调用之间共享您的上下文.最好为每个WFC调用实例化一个新的新上下文.