我有一个MS SQL 2008数据库,可以通过LINQ进行数据更新/回溯.
对于繁重的应用程序,我的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:WorkspaceXYZWASDFDataServerDataServer.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;
}
解决方法
没有看到你的代码,简短的回答是EntityFramework不是线程安全的.当尝试访问您的ObjectContext时,当两个线程重叠时,您会以看似随机的模式获得此错误.我假设你已经将你的上下文填充到一个静态变量中.将上下文设置为局部变量或围绕对ObjectContext的访问进行写锁定.
如果您想要更具体的答案,您的代码将有所帮助.
编辑
您的问题是两个线程正在尝试同时使用上下文,或者一个线程正在打开一个事务,然后第二个线程尝试使用您的单例上下文.您的代码片段为我提出了比以前更多的问题.
>在您的代码示例中_lockObject是一个静态变量?>为什么在SaveChanges中显示锁定,然后解释正在抛出错误来自InsertOrUpdateDetachedObject?我们能看到InsertOrUpdateDetachedObject的代码吗?> SaveChanges是否使用与直接访问上下文的所有其他方法相同的_lockObject?>您对_context.SaveChanges的调用是您保存到数据库的唯一方式,还是您有自己打开事务上下文的其他区域?>您使用单例,以便在多个调用之间共享您的上下文.最好为每个WFC调用实例化一个新的新上下文. (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|