9

I am querying the db in a separate thread.

If I close the application while the query is being executed, will the SqlConnection automatically close or will it remain open?

stivlo
  • 83,644
  • 31
  • 142
  • 199

3 Answers3

9

If the process is terminated, all the OS resources, including network connections, will be released. In other words - that's fine.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • @JoelCoehoorn: It's certainly not an excuse to avoid closing down connections promptly etc. But it's a reason not worry if your app goes down while doing something *reasonably* harmless. Dying half way through writing a new data file etc is slightly harder to cope with (although doable of course). – Jon Skeet Sep 23 '11 at 21:16
  • Becuase i have see in some cases that the application is closed, but when you query in sql-server to see the active connections, still shows up those connections. –  Sep 23 '11 at 21:18
  • @Vanilla: It's possible that the network connections are in time-wait or something similar - but I'd expect to see them drop off reasonably quickly. What sort of connection are you using to the database? – Jon Skeet Sep 23 '11 at 21:20
  • @Vanilla: No, I mean which network transport - TCP/IP, named pipes etc. It may well make a difference to how quickly the resource is cleaned up. – Jon Skeet Sep 23 '11 at 21:34
0

If the application ends, the connection gets closed, along with everything else that was opened.

LarsTech
  • 80,625
  • 14
  • 153
  • 225
0

A SqlConnection is a disposable object. In general it is always good practice to Dispose() of objects that implement IDisposable. I also noticed SqlConnection objects have a Close() method. Should you call that too? Well, I found this article with more info on this:

SqlConnection: To Close or To Dispose?

Steve Sheldon
  • 6,421
  • 3
  • 31
  • 35