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

后端 未结 3 1406
借酒劲吻你
借酒劲吻你 2021-01-05 23:42

I\'ve got a c# WINDOWS Application that is multi-threaded. It is my understanding that in a web environment, connections are pooled automatically. It is also my understandin

3条回答
  •  广开言路
    2021-01-06 00:06

    It is my understanding that in a web environment, connections are pooled automatically. It is also my understanding that in a Windows app, this is not the case.

    No, this is wrong, as m3rLinEz pointed out. 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.

    You could keep a connection open for the duration of the application in a monolithic WinForms app. 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.

    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. But as noted above, you should prefer the standard pattern for data access.

提交回复
热议问题