How and when to deal with database that is down? [closed]
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Close开发者_JS百科d 4 years ago.
Improve this questionMy problem is that I have to deal somehow with cases when database is down in my MVC application. So, should I like try{} catch{}
to open connection in every controller? Or what is the best practice?
Note: For sample database access is isolated in few "manager" classes that work repository kind of way.
Why are you opening a connection in every controller. This should be isolated within it's own reusable class.
You may want to check out the repository pattern to implement your data access.
If you don't have a database connection, in a production database-driven web app, typically this means something bad happened well beyond the scope of your application. About the only thing you can do is give a friendly error message to the user and tell someone responsible for fixing things. Which is a task for your global error handling, not each controller.
Well, first off, if you have a separate database handling routine in every controller, I would start with combining that into a base class or a reusable member.
After that, you should be able to use ASP.NET MVC's [HandleError]
attribute to specify how to handle errors from your database.
精彩评论