简体   繁体   English

无法建立随机SQL连接问题

[英]random sql connection can't be established issue

Our code is simple 我们的代码很简单

using (var context = dbNewsEntities.CreateContext())
{
            CheckClient(clientId, context);

            var articles = context.GetArticlesForRSS(clientId, 0, ArchiveStartDays, false);
}

Randomly we get sql connection issues. 随机我们得到sql连接问题。 I used windbg to check the SqlConnection, and indeed there are 100 (the default) connections in the pool. 我使用windbg检查SqlConnection,实际上池中有100个(默认)连接。

However, when I use the method described here to check those connections, they are all closed. 但是,当我使用此处介绍的方法检查那些连接时,它们都已关闭。 (as the _innerConnection object is System.Data.ProviderBase.DbConnectionClosedNeverOpened, not some methods that we were doing). (因为_innerConnection对象是System.Data.ProviderBase.DbConnectionClosedNeverOpened,而不是我们正在使用的某些方法)。

What going to happen when there are 100 closed connections in the pool? 当池中有100个关闭的连接时会发生什么? I assume c# code will just pick one other than create one. 我假设C#代码只会选择一个,而不是创建一个。 Am I right? 我对吗?

Many Thanks 非常感谢

Sorry, now I realised that our code is not as simple as I put there. 抱歉,现在我意识到我们的代码并不像我放在那里那样简单。

Turned out that there was some code hidden in another function, which wasn't closed properly. 原来,有一些代码隐藏在另一个函数中,没有正确关闭。 If helps anyone, you can use: 如果有帮助,您可以使用:

SELECT 
  DBID, 
  DB_NAME(dbid) as DBName, 
  COUNT(dbid) as NumberOfConnections,
  loginame as LoginName
FROM
    sys.sysprocesses
WHERE 
  dbid > 0 
GROUP BY 
   dbid, loginame

To see how how many connections in database, if your live system's exception is not properly catched, Then in windbg, you can use 要查看数据库中有多少个连接(如果未正确捕获实时系统的异常),则可以在windbg中使用

.foreach (ex {!dumpheap -type System.Data.EntityException -short}){.echo "********************************";!pe ${ex} }

This will print you the exceptions and stack traces you need. 这将为您打印所需的异常和堆栈跟踪。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM