4

I open a database connection in a try block. If I step into the try block, and then stop debugging, would the finally block be executed? In other words, does the connection remain open?

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Xiaowei.Jia
  • 415
  • 1
  • 4
  • 13
  • If the code was executed you could still debug the finally-block, which you cannot apparently because the app was already closed. – MakePeaceGreatAgain Apr 20 '16 at 10:32
  • one option I see for you to check in database server how many connections are active. This way you can test it. If you are using SQL server refer this link http://stackoverflow.com/questions/216007/how-to-determine-total-number-of-open-active-connections-in-ms-sql-server-2005 – kudlatiger Apr 20 '16 at 10:38

3 Answers3

4

No, if you terminate the app from your debugger, the finally block is not executed. The entire process is killed. Indeed, this may lead to undisposed references. (If you just detach, it keeps running)

The connection will not remain open though. The server will disconnect sooner or later.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • 1
    "Stopping debugging" doesn't necessarily mean that the process is terminated though. Could simply be that the connection visual studio has with the process is stopped. – Liam Apr 20 '16 at 10:33
  • I guess I'm thinking web here. If it's a web app pressing stop in visual studio doesn't terminate the process but in windows (been a while since I've done any windows development so I think I'm correct in saying...) it does. – Liam Apr 20 '16 at 10:37
  • I see what your saying. I guess the question isn't clear – Liam Apr 20 '16 at 10:39
  • we can see in database server for active connections - just for testing. – kudlatiger Apr 20 '16 at 10:40
4

If by "stop debugging" you mean stopping the application of course not (for example stopping the current debug session pressing the stop button inside Visual Studio). This is the typical case for a console application launched by pressing F5 button.

If by "stopping" you mean "detaching" from currently running application launched outside VS (for example the one you attached with Ctrl+Alt+P), the program will run to it's end and will do everything is programmed for. It's also true for any web application which is running inside IIS (but not the IIS express).

Luca Ghersi
  • 3,261
  • 18
  • 32
2

It depends on the application; if it is web application the rest of code will execute, since it can be accessed from the generated .dll files. if it is windows application then the finally block will not be executed

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88