简体   繁体   English

C#Windows应用程序 - 许多线程使用相同的连接?

[英]C# Windows Application - Many threads using the same connection?

I've got ac# WINDOWS Application that is multi-threaded. 我有一个多线程的ac#WINDOWS应用程序。 It is my understanding that in a web environment, connections are pooled automatically. 我的理解是,在Web环境中,连接会自动汇集。 It is also my understanding that in a Windows app, this is not the case. 我的理解是,在Windows应用程序中,情况并非如此。 Therefore, for a Windows app, the same connection should be used and not closed after each call, but instead closed when the app shuts down. 因此,对于Windows应用程序,应该使用相同的连接,而不是在每次调用后关闭,而是在应用程序关闭时关闭。

I'm curious though - is my correct? 我很好奇 - 我是对的吗? If it is, can two threads use the same connection to get a dataset from the DB at the same time or is that functionality queued up? 如果是,两个线程是否可以使用相同的连接同时从数据库获取数据集,或者该功能是否排队?

Thanks 谢谢

The Connection Pooling is one feature of ADO.NET. 连接池是ADO.NET的一个功能。 Therefore the connections are already pooled. 因此,连接已经合并。 Not only in the web environment. 不仅在网络环境中。

http://www.ondotnet.com/pub/a/dotnet/2004/02/09/connpool.html http://www.ondotnet.com/pub/a/dotnet/2004/02/09/connpool.html

It is my understanding that in a web environment, connections are pooled automatically. 我的理解是,在Web环境中,连接会自动汇集。 It is also my understanding that in a Windows app, this is not the case. 我的理解是,在Windows应用程序中,情况并非如此。

No, this is wrong, as m3rLinEz pointed out. 不,这是错的,正如m3rLinEz指出的那样。 Connections are always pooled. 连接总是汇集在一起​​。

Therefore, for a Windows app, the same connection should be used and not closed after each call, but instead closed when the app shuts down. 因此,对于Windows应用程序,应该使用相同的连接,而不是在每次调用后关闭,而是在应用程序关闭时关闭。

You could keep a connection open for the duration of the application in a monolithic WinForms app. 可以在单个WinForms应用程序中保持连接在应用程序期间保持打开状态。 But it's better to use the standard pattern of opening/closing connections whenever you need them. 但最好在需要时使用打开/关闭连接的标准模式。 Connection pooling means you won't notice a performance difference. 连接池意味着您不会注意到性能差异。 And your data access code will be compatible with server applications such as ASP.NET. 并且您的数据访问代码将与ASP.NET等服务器应用程序兼容。

If it is, can two threads use the same connection to get a dataset from the DB at the same time or is that functionality queued up? 如果是,两个线程是否可以使用相同的连接同时从数据库获取数据集,或者该功能是否排队?

No. The ADO.NET classes (connection, command etc) are not thread-safe and should not be shared between threads without synchronisation. 不可以.ADO.NET类(连接,命令等)不是线程安全的,不应在没有同步的线程之间共享。 But as noted above, you should prefer the standard pattern for data access. 但如上所述,您应该更喜欢数据访问的标准模式。

ok - so this assumption of mine was brought on by observation: When I tried a win app setup in the typical pool fashion, I always experience a 3-5 second delay while a real connection is established to the remote server. 好吧 - 所以我的这个假设是通过观察引起的:当我尝试以典型的游泳池方式设置win应用程序时,我总是遇到3-5秒的延迟,同时建立了与远程服务器的真实连接。 Even when I did an open, then a close, the next query would always have this delay. 即使我做了一个打开,然后关闭,下一个查询总是有这个延迟。

When the server connects, it obviously doesn't establish a connection for each connection in the pool. 当服务器连接时,它显然不会为池中的每个连接建立连接。 Also, is the pooling mechanism smart enough to grab a connection that it knows is already open or is it possible for it to simply grab any random connection? 此外,池化机制是否足够智能以获取它知道已经打开的连接,或者它是否可以简单地抓取任何随机连接?

What is the default max connections in the pool? 池中的默认最大连接数是多少?

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

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