简体   繁体   中英

C#: Monitoring Connections

I am writing an application which has an SslStream / NetworkStream / TcpClient as well as multiple SqlConnection instances. I am sending and receiving data over them constantly, but as this is an application which accesses multiple servers both locally and remotely, I need to monitor the connections for failure and somehow attempt to restart them should they fail. Is there a Microsoft supported convention for how I should monitor these classes? The documentaton doesn't really show me anything. I've tried CanRead / CanWrite for the SslStream and I do, of course, have the ability to monitor timeouts. Is there a recommended time for timing these out? Is monitoring/counting timeouts the only way to monitor for connection loss?

Managed SqlConnection instances should be short lived. Only create them when you need them and then dispose them. Do not keep them hanging around in your application for when you want them, that is a very bad practice. The reason is that Sql Server (as well as other databases) implement connection pooling so creating the managed connection is relatively cheap as the underlying connection will be reused once its released back to the pool.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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