C# .NET/SQL Server 中的连接池?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-07-31 17:04:26  来源:igfitidea点击:

Connection Pooling in .NET/SQL Server?

提问by pix0r

Is it necessary or advantageous to write custom connection pooling code when developing applications in .NET with an SQL Server database? I know that ADO.NET gives you the option to enable/disable connection pooling -- does that mean that it's built into the framework and I don't need to worry about it? Why do people talk about writing their own connection pooling software and how is this different than what's built into ADO.NET?

使用 SQL Server 数据库在 .NET 中开发应用程序时,编写自定义连接池代码是否必要或有利?我知道 ADO.NET 为您提供了启用/禁用连接池的选项——这是否意味着它已内置到框架中而我无需担心?为什么人们谈论编写自己的连接池软件,这与 ADO.NET 中内置的软件有何不同?

采纳答案by Sean Carpenter

The connection pooling built-in to ADO.Net is robust and mature. I would recommend against attempting to write your own version.

ADO.Net 内置的连接池功能强大且成熟。我建议不要尝试编写自己的版本。

回答by Rob Cooper

My understanding is that the connection pooling is automatically handled for you when using the SqlConnection object. This is purposefully designed to work with MSSQL and will ensure connections are pooled efficiently. You just need to be sure you close them when you are finished with them (and ensure they are disposed of).

我的理解是在使用 SqlConnection 对象时会自动为您处理连接池。这是专门为与 MSSQL 一起使用而设计的,并将确保有效地汇集连接。您只需要确保在完成它们后关闭它们(并确保它们被处理掉)。

I have never heard of people needing to roll their own myself. But I admit my experience is kind of limited there.

我从来没有听说过人们需要自己动手。但我承认我在那里的经验有限。

回答by Vaibhav

Well, it is going to go away as the answer to all these questions will be LINQ. Incidentally, we have never needed custom connection pooling for any of our applications, so I am not sure what all the noise is about.

好吧,它会消失,因为所有这些问题的答案都将是 LINQ。顺便说一句,我们从来不需要为我们的任何应用程序定制连接池,所以我不确定所有的噪音是关于什么的。

回答by Chris Roberts

I'm no realexpert on this matter, but I know ADO.NET has its own connection pooling system, and as long as I've been using it it's been faultless.

我不是这方面的真正专家,但我知道 ADO.NET 有自己的连接池系统,而且只要我一直在使用它,它就完美无缺。

My reaction would be that there's no point in reinventing the wheel... Just make sure you close your connections when you're finished with them and everything will be fine!

我的反应是重新发明轮子没有意义……只要确保在完成连接后关闭连接,一切都会好起来的!

I hope someone else can give you some more firm anwers!

我希望别人能给你一些更坚定的答案!

回答by BinaryMisfit

With the advent of ADO.Net and the newer version of SQL connection pooling is handled on two layers, first through ADO.Net itself and secondly by SQL Server 2005/2008 directly, eliminating the need for custom connection pooling.

随着 ADO.Net 的出现和更新版本的 SQL 连接池在两层上处理,首先通过 ADO.Net 本身,其次直接通过 SQL Server 2005/2008,消除了对自定义连接池的需要。

I have been informed that similar support are being planned or have been implemented in Oracle and MySQL out of interest.

我被告知出于兴趣,正在计划或已在 Oracle 和 MySQL 中实施类似的支持。