如果我运行下面的代码,很多与数据库的连接仍然无所事事。您可以通过运行以下命令来检查打开的连接数:
SELECT COUNT(dbid) as TotalConnections FROM sys.sysprocesses WHERE dbid > 0
或者,如果需要更多详细信息,请运行以下命令。你会看到很多状态为"等待命令"的连接:
sp_who2
我本以为在线程退出并关闭连接后,Codefluent 持久性上下文将消失。如何强制 Codefluent 关闭池中的连接?
public void TestThreads()
{
for (var i = 0; i < 1000; i++)
{
var t = new Thread(() => StaticticThreadContainer.Test());
t.Start();
}
}
public static int Test()
{
var p = CwObject.LoadByEntityKey("baf04c09-7415-497d-b3cd-00004266f503");
return 1;
}
我发现了更多。如果我在线程中返回之前调用以下代码,则连接将正确关闭。这是要走的路吗?
CodeFluentContext.Get(Compareware.Constants.ApplicationStoreName).Persistence.ResetConnection();
CodeFluent Entities 使用 LocalDataStoreSlot
存储每个线程的上下文。当线程终止时,不会自动释放线程本地数据。因此,在终止线程之前,必须调用 Dispose
方法:
CodeFluent.Runtime.CodeFluentContext.GetExisting(Constants.MyStoreName)?.Persistence.Dispose(true);
> Meziantou回答了我的问题,但提到的重载不存在。以下代码应该可以解决问题:
CodeFluent.Runtime.CodeFluentContext.GetExisting(Constants.MyStoreName)?.Persistence.ResetConnection();
CodeFluent.Runtime.CodeFluentContext.GetExisting(Constants.MyStoreName)?.Persistence.Dispose();