How can I temporarily disable a LocalDB database for connection failure testing?

Viewed 636

For developer testing, I run a LocalDB version of my SQLServer database from Visual Studio 2013. I want to test what happens when this database becomes unreachable to my C# application running on the same machine - ideally debugging from VS. Specifically, becomes unreachable while the application is running, so I can "turn it on/of" without re-starting my application and check it handles this OK.

I can't see an obvious "stop DB" option within VS. I also don't know how the LocalDB instance is managed... if it only exists while VS is running for instance?

What way(s) are there to do what I want, with both DB and application running on the same machine and the DB hosted as LocalDB not through SQLServer (which I don't have installed)?

2 Answers

LocalDB isn't ideal for testing of this scenario. Better test on the real thing.

However, if you are just prototyping something, you could:

  1. Start by testing how the application responds if you kill its database connection. The app will get error code 596 (or perhaps 233).
  2. Test how the app responds to sqllocaldb stop <instancename> -k (it will probably be seeing an error code 904 or 109; it's a poor test as the localdb will be automatically restarted nearly immediately).
  3. Make sure, perhaps by source code inspection, that the application is similarly impervious to error code -2, -1, or 2, which is what you will probably be seeing in regular disconnections or when recovering from those.

While I have listed some error codes I have seen, because the app you are testing (or its library) might be doing something like that, I don't really recommend coding against individual error codes. A disconnection is better detected as an error with severity 20 or more.

Don't forget to do the final testing in conditions similar to your production conditions.

Related