Is a SqlConnection automatically closed when an application is closed?
Asked Answered
A

3

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?

Animated answered 23/9, 2011 at 21:8 Comment(0)
S
9

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

Siddur answered 23/9, 2011 at 21:13 Comment(4)
@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).Siddur
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.Animated
@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?Siddur
@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.Siddur
W
0

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

Warehouseman answered 23/9, 2011 at 21:13 Comment(0)
P
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?

Playa answered 3/10, 2011 at 16:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.