简体   繁体   English

如何确保我们的.NET Core API清理数据库连接?

[英]How do I ensure our .NET Core APIs clean up database connections?

We are running .NET Core 1.1 APIs in Docker containers on Ubuntu 14.04. 我们在Ubuntu 14.04的Docker容器中运行.NET Core 1.1 API。 The API's interact with various SQL Server databases through EF Core, and what we are seeing is that databases connections are not being cleaned up, nor are they being reused. 该API通过EF Core与各种SQL Server数据库进行交互,我们看到的是数据库连接没有被清理,也没有被重用。 We are quickly seeing thousands of connections on the database through the sp_who2 query. 通过sp_who2查询,我们很快看到数据库上有数千个连接。

  1. All connections are created with the same connection string, so connection pooling should be able to work. 所有连接都使用相同的连接字符串创建,因此连接池应该可以正常工作。
  2. The LastBatch time on the connections does not update, so they do not appear to be being reused, as you'd expect if they were pooled. 连接上的LastBatch时间不会更新,因此它们似乎没有被重用,就像您希望它们被池化一样。
  3. Objects calling the database are defined with Scoped lifecycle, so should be cleaned up after each session. 调用数据库的对象是使用Scoped生命周期定义的,因此应在每次会话后清除。
  4. The Hibernating Rhinos EF Profiler appears to show DbContexts being cleaned up consistently within EF. 休眠Rhinos EF探查器似乎显示在EF中一致地清理了DbContext。
  5. The connection count does not increase every time the database is hit, so it may be specific code - we are still trying to nail that down. 每次命中数据库时,连接计数不会增加,因此它可能是特定的代码-我们仍在努力确定。
  6. Our testing shows that this does not happen when running on Windows under IIS or IIS Express, so it appears to be specific to Docker or Ubuntu. 我们的测试表明,在Windows上以IIS或IIS Express运行时不会发生这种情况,因此它似乎特定于Docker或Ubuntu。

Could this be by design in .NET Core? 可以是.NET Core中的设计吗? Or are we missing a trick? 还是我们错过了一个把戏? How could we force EF to do some connection cleanup? 我们如何强制EF进行一些连接清理?

We are now pretty sure that this is caused by a bug in the corefx System.Data.SqlClient which was fixed in version 4.3.1. 现在,我们非常确定这是由于corefx System.Data.SqlClient中的一个错误引起的,该错误已在4.3.1版中修复。 We were running 4.3.0 and have now updated to 4.3.1, and initial testing indicates that the problem is resolved. 我们正在运行4.3.0,现在已更新为4.3.1,初步测试表明问题已解决。

You can read more about the issue in the dotnet/corefx Github repository Issue #13422 . 您可以在dotnet / corefx Github存储库Issue#13422中阅读有关该问题的更多信息。

In summary, it appears to be a Linux-specific bug where the connections were not being disposed. 总而言之,这似乎是特定于Linux的错误,其中没有释放连接。

I will confirm/mark as the answer when we have proved this solves the problem to my satisfaction. 当我们证明这可以使我满意地解决问题时,我将确认/标记为答案。

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

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