Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure website sometimes can't connect to SQL Azure database

I've been testing a website in Azure for about a month (Free trial), and now I've subscribed to pay-as-you-go. When I was on the free trial, I had absolutely no connectivity issues. Now I've deployed again to a fresh website created on the pay-as-you-go subscription with the same settings (scaling mode etc.) as the previous one. However, now when I navigate to my site, it sometimes fails to connect to my database:

Server Error in '/' Application.
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
Description: An unhandled exception occurred during the execution of the current web
request. Please review the stack trace for more information about the error and where
it originated in the code.

Exception Details: System.ComponentModel.Win32Exception: 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

Source Error:

An unhandled exception was generated during the execution of the current web request.
Information regarding the origin and location of the exception can be identified using
the exception stack trace below.

Stack Trace:


[Win32Exception (0x80004005): 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]

[SqlException (0x80131904): 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.)

I try to refresh the page and it connects. After a few minutes, when I try again, I sometimes get the same error again, but sometimes not. I never had a single connectivity issue in the free trial mode. Why does it happend and how can I prevent this problem?

like image 713
Can Poyrazoğlu Avatar asked Oct 01 '22 22:10

Can Poyrazoğlu


1 Answers

Transient errors are expected in a cloud environment. Let's say that a database call fails, if you retry a few milliseconds later, that same call would now work. To shield you from these errors, you need to implement a retry logic. If you use Entity Framework 6, you're in luck since it's baked in: http://msdn.microsoft.com/en-US/data/dn456835 Here's a quick video on how to implement this: http://www.azurerocks.com/0_oaw-merMw

You can also use the Enterprise Library Transient Fault Handling Application Block: http://msdn.microsoft.com/en-us/library/hh680934(v=pandp.50).aspx

like image 178
CSharpRocks Avatar answered Oct 18 '22 01:10

CSharpRocks