2

I am using sql server and sometimes my database goes down for maintenance. I have error handling code so the user don't see error page, instead they see a nice and friendly error message. My question is: When the database is down, It takes an awfully long time for the page to load and show the error message. I thought it had something to do with the timeout property, but changing it using

command.CommandTimeout = 5;

or

Connect Timeout=5; in web.config in connectionString doesn't seem to help and it still take a long time for the page to load (about 40 sec). Is there a way to minimize this time?

The exception that gets thrown and logged is

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 0 - A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.)

user1
  • 1,063
  • 1
  • 8
  • 28
  • Minimize which time? The time required to fail the connection attempt or make the connection attempt go faster? – Panagiotis Kanavos Jul 09 '13 at 14:38
  • 1
    The time for the code to throw the exception. – user1 Jul 09 '13 at 14:40
  • It takes about 35-40 sec to throw the exception when the line with stored proc call is hit. – user1 Jul 09 '13 at 14:41
  • You need to differentiate between a CONNECTION timeout and a COMMAND timeout. They are not the same. A better strategy is to have a "website offline" plan in effect...that doesn't need to try and talk to the database. – granadaCoder Jul 09 '13 at 14:51
  • @granadaCoder More than half of the content of my website is static which don't need database access. So it would be unwise to pull the entire website down and also, the code helps in error handling if any unforeseen error was to occur. Also, I mean to say, I tried both timeouts and none seem to help so I am asking you guys for a solution – user1 Jul 09 '13 at 14:57

2 Answers2

0

Ok then.

I would create a base class for all your "data access layer" classes.

public class DataBaseLayer ()
{

public DataBaseLayer()
{ /* check for appSetting here and throw a SqlServerMaintenanceModeException exception (custom exception) */}

}


public class EmployeeDataLayer :  DataBaseLayer ()
    {
public EmployeeDataLayer() : base ()
}

In the constructor, I would check an appSetting...for "true" or "false".........and throw a CustomException

public class SqlServerMaintenanceModeException : ApplicationException

{}

throw it in the constructor of the DataBaseLayer......then have everything else handle it.

That way...only "db driven" pages are affected.

The small reason I don't like checking for a "timeout" is that..

  1. It is a tad unpredictable.
  2. Sometimes that is a normal exception.............like a switch on your network goes by. How will you tell the difference?

I dislike ambiguous exceptions tremendously.

granadaCoder
  • 26,328
  • 10
  • 113
  • 146
  • I don't know if I am not getting your solution or you didn't understand my question, but how does this help the database driven pages load faster? – user1 Jul 09 '13 at 15:14
0

If your server connect timeout takes too long, check:

  • type of your connection string (.NET provider, OLEDB or other), they might have different names for same thing
  • if you really are using the connection string in question; you might be actually using completely different connection string
  • isolate your try/catch timeout block to wrap just conn.Open() - that way you really know that opening a connection takes too long; I'm not saying your exception is not indicative, just that I'm not 100% sure that all that time is spent only for conn.Open()
OzrenTkalcecKrznaric
  • 5,535
  • 4
  • 34
  • 57
  • Since I only have one database so I am sure that i am not using a different connection string also, since i always use `ConfigurationManager` class to pull out connection string, I am positive that I am not using differnet connection string. I used breakpoint and saw that it was just conn.open() that was taking biggest chunk of time. So this rules out your 2nd and 3rd bullet points. But not sure how to check the type of connection string. Could you please explain? – user1 Jul 10 '13 at 15:22