-1

I got an windows service application which dynamically loads some other modules in other new appdomain. The problem is they are all using the same static database connections. I can dispose the static connections in the main AppDomain when I shutdown the service. But how can I immediately dispose the other static connections in other AppDomain. The problem is since the other connections still exist, the service application still runs in the Task Manager even I fully stop it.

Thanks

Mango
  • 11
  • 3

1 Answers1

1

The problem is they are all using the same static database connections.

Yes, that is definitely a problem. Don't do that. Connections are pooled by .NET and are not expensive to create, so the proper pattern is to create them when you need them, use them, and dispose of them when you're done. An effective way of doing that is with using statements.

in general, whatever creates a disposable object is responsible for disposing of it. since your disposable objects are static, there is no way to know what is responsible for disposing of it. So you need to have logic to see if it's already been disposed, if it's open, if it's null, etc. It's much cleaner to just keep all of the creation and disposal logic in one place.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • Hi Stanley, Thanks very much for your reply. The scenario is I am using rabbitMq to sending like millions of messages. Should I create the connection and dispose it for each single message? Sounds like I need to create an connections pool as well like .net does? – Mango May 27 '16 at 01:20