2

When my database server goes down, this causes IIS hosting MVC application on another server to crash.

All pages then show HTTP Error 503: Service Unavailable.

I need a solution, for example putting an error handler in global.asax such as Application_Error(), but this one doesn't fit my needs. How can I handle this case so that instead of crashing, my app shows specific error pages for each error.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
Festim Cahani
  • 317
  • 4
  • 18
  • This is a solved problem and is fairly straightforward to implement. Try using a fallover cluster and load balancer. the database would be mirrored on 2 (or more) database servers - if one of them goes down, the other takes the request. https://msdn.microsoft.com/en-us/library/ms189134.aspx – user1666620 Dec 22 '15 at 09:29
  • if you are using a backup database, and this reflects changes on the 1st database, aren't yo just using your own version of clustering - in which case why not just fully implement the existing solution? – user1666620 Dec 22 '15 at 09:33
  • @user1666620 Thank you. Im looking forward on implementing SqlAlwaysOn – Festim Cahani Dec 22 '15 at 09:35
  • Although, the point is making my iis mvc project run without databases, we are talking about application level handler, thats why I am in stackoverflow – Festim Cahani Dec 22 '15 at 10:08
  • You're solving the wrong error. See [What possibilities can cause “Service Unavailable 503” error? \[closed\]](http://stackoverflow.com/questions/4150952/what-possibilities-can-cause-service-unavailable-503-error): when your site shows a 503, your application is crashing too often. A failing database connection should not let your app crash. When the application pool is down, there is nothing in application code that you can do. – CodeCaster Dec 22 '15 at 15:41
  • @CodeCaster Yes, my app was crashing very often, i checked the logs. It was crashing because all connections i had to my database, not all of them had try/catches. Now, I am adding try/catch on every place, and something more i am adding as a feature, is making it work without databases, at least untill they come back, my page must work. – Festim Cahani Dec 22 '15 at 16:02
  • @CodeCaster so the real answer would be, to make sure what made it crash so often fix, so it wont crash anymore. The application code to not let the app crash should be added, and 503 is gone forever. – Festim Cahani Dec 22 '15 at 16:05
  • No, you can just handle exceptions without letting your application crash. – CodeCaster Dec 22 '15 at 16:06
  • Did I say something else? When you add try/catch application doesnt crash – Festim Cahani Dec 22 '15 at 16:08
  • 1
    You get the 503 when your app pool shuts down because your site is erroring a lot. Prevent the errors, prevent the 503. Whether you do that by adding try-catch everywhere or by handling them elsewhere is up to you. – CodeCaster Dec 22 '15 at 17:45
  • @CodeCaster you can write your full response as an answer, i will pick it as solved with yours. Thank you – Festim Cahani Dec 23 '15 at 13:12

1 Answers1

1

As explained in What possibilities can cause "Service Unavailable 503" error? and TechNet: 503-Service Unavailable (IIS 6.0), there are many possible reasons for a 503 error.

In your case it is the "rapid-fail protection" of IIS kicking in. Your application crashes your worker process, and that occurs so many times in a short timespan, that the server assumes something is inherently wrong with your application and shuts the application pool down.

Do note that an application error (such as an exception occurring for an unavailable database) should not kill the worker process. If this happens a lot, there's something else that's very wrong. Check the event log to find out the actual source of the error and fix that.

In the meantime, you may be able to prevent the crashes by adding try-catch statements at the appropriate places, or by adding a global exception handler in Application_Error.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272