Disable (politely) website when sql server is down

I work in college and am developing an ASP.NET site with a lot of student reports, attendance statistics ... The basis of the data is the server database MSSQL, which is the server part of our student management system. On Thursday mornings, regular maintenance is performed for an unknown period of time (depending on what needs to be done).

Most employees are aware of this, but less regular users seem to always call me. What is the easiest way to turn off the site during maintenance, obviously, I can just try the database query to check if it works, but I'm not sure, for example, the best way to redirect all users to the message "The site is down for maintenance" , bearing in mind that they could start a session before the website crashed.

I hope something can be implemented globally, and not on the page.

+3
source share
8 answers

I would suggest doing this in Application_PreRequestHandlerExecute, and not after the error. As a general rule, it is better not to introduce normal processing if you know that your database is not available. I usually use something like below

void Application_PreRequestHandlerExecute(Object sender, EventArgs e)
{
 string sPage = Request.ServerVariables["SCRIPT_NAME"];
 if (!sPage.EndsWith("Maintenance.aspx", StringComparison.OrdinalIgnoreCase))
 {
  //test the database connection
  //if it fails then redirect the user to Maintenance.aspx
  string connStr = ConfigurationManager.ConnectionString["ConnectionString"].ConnectionString;
  SqlConnection conn = new SqlConnection(connStr);
  try
  {
   conn.Open();
  }
  catch(Exception ex)
  {
   Session["DBException"] = ex;
   Response.Redirect("Maintenance.aspx");
  }
  finally
  {
   conn.Close();
  }
 }
}
+1

html "app_offline.htm" . .

.

+12

, , , " ", , xxx . -, , ( ) , , , .

+1

"offline.html" , , / .

, , - (IP- () ), . - , "" - "" -. , "" -.

- "" , ( /css)

, - "" 404, "- ".

+1

, , - ? ADO.NET , , " "?

"Global.asax", "Application_Error". , -. , #:

protected void Application_Error(object sender, EventArgs e)
{
    Exception e = Server.GetLastError().GetBaseException();
    if(e is SqlException)
    {    
        Server.ClearError();
        Server.Transfer("~/offline.aspx");
    }
} 

Number , , , . , , SQL , , , .

EDIT: , , petebob.

+1

, , , , IIS. , , , , .

, - , . - global.asax, , , :

, Application_BeginRequest SQL, :

HttpContext context = HttpContext.Current;
     if (!isOnline())
     {
        context.Response.ClearContent();
        context.Response.Write("<script language='javascript'>" + 
"top.location='" + Request.ApplicationPath + "/public/Offline.aspx';</scr" + "ipt>");
     } 

- , , . .

0

Global.asax , .

- , , .

EDIT: , , . , , :)

0

, , :

try
{
   conn.Open();
}
catch(Exception ex)
{
   Session["DBException"] = ex;
   Response.Redirect("Maintenance.aspx");
}
finally
{
   conn.Close();
}
0

Source: https://habr.com/ru/post/1697084/


All Articles