【发布时间】:2011-07-05 20:14:04
【问题描述】:
我有一个 MS SQL 2008 数据库,可通过 LINQ 访问以进行数据更新/检索。
WCF 服务以 PerCall 实例化模式访问我的 linq,以用于繁重的应用程序。该应用程序有多个线程调用服务,并且多个应用程序同时运行。
我经常发生一些 EntityException:
System.Data.EntityException 被捕获 Message=在提供程序连接上启动事务时发生错误。有关详细信息,请参阅内部异常。 源=系统.数据.实体 堆栈跟踪: 在 System.Data.EntityClient.EntityConnection.BeginDbTransaction(IsolationLevel 隔离级别) 在 System.Data.EntityClient.EntityConnection.BeginTransaction() 在 System.Data.Objects.ObjectContext.SaveChanges(SaveOptions 选项) 在 D:\Workspace\XYZWASDF\DataServer\DataServer.cs:line 123 中的 Infoteam.GfK.TOMServer.DataServer.DataServer.SaveChanges() 内部异常:System.Data.SqlClient.SqlException Message=Une nouvelle transaction n'est pas autorisée parce que d'autres threads Sont en cours d'execution dans la session。 Source=.Net SqlClient 数据提供者 错误代码=-2146232060 班级=16 行号=1 数量=3988 程序="" 服务器=ift-srv114 状态=1 堆栈跟踪: 在 System.Data.SqlClient.SqlConnection.OnError(SqlException 异常,布尔 breakConnection) 在 System.Data.SqlClient.SqlInternalConnection.OnError(SqlException 异常,布尔 breakConnection) 在 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() 在 System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior,SqlCommand cmdHandler,SqlDataReader dataStream,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj) 在 System.Data.SqlClient.TdsParser.TdsExecuteTransactionManagerRequest(字节 [] 缓冲区,TransactionManagerRequestType 请求,字符串 transactionName,TransactionManagerIsolationLevel isoLevel,Int32 超时,SqlInternalTransaction 事务,TdsParserStateObject stateObj,布尔 isDelegateControlRequest) 在 System.Data.SqlClient.SqlInternalConnectionTds.ExecuteTransactionYukon(TransactionRequest transactionRequest,字符串 transactionName,IsolationLevel iso,SqlInternalTransaction internalTransaction,布尔 isDelegateControlRequest) 在 System.Data.SqlClient.SqlInternalConnectionTds.ExecuteTransaction(TransactionRequest transactionRequest,字符串名称,IsolationLevel iso,SqlInternalTransaction internalTransaction,布尔 isDelegateControlRequest) 在 System.Data.SqlClient.SqlInternalConnection.BeginSqlTransaction(IsolationLevel iso,字符串 transactionName) 在 System.Data.SqlClient.SqlInternalConnection.BeginTransaction(IsolationLevel iso) 在 System.Data.SqlClient.SqlConnection.BeginDbTransaction(IsolationLevel 隔离级别) 在 System.Data.Common.DbConnection.BeginTransaction(IsolationLevel 隔离级别) 在 System.Data.EntityClient.EntityConnection.BeginDbTransaction(IsolationLevel 隔离级别) 内部异常:
(对不起,它不是很可读)。 (内部异常消息的意思是“A new transaction is not allowed because there are other threads running in the session.”
我已经检查过了,我不是在循环中,它出现这个异常时纯粹是随机的,我不知道如何避免这种情况。
任何帮助将不胜感激:)
谢谢!
编辑:这是我有时遇到此异常的示例
//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;
}
【问题讨论】:
-
你能显示导致这个错误的代码吗?
标签: sql multithreading wcf entity-framework concurrency