How are SQL Server timeout exceptions handled in Ajax post backs? - c#

I have an Ajax postback within which I make a SQL Server query. The structure's a little too complex to post here. If I have customErrors=Off", then the error is returned to the client in the HTTP response, from whence I pull it out and display the appropriate things to the user:
114|error|500|Timeout expired. The timeout period elapsed prior to
completion of the operation or the server is not responding.|
I have other "application exceptions" which I also catch at the client in the same way. All this is free: asp.net puts the above stuff in the http response without my assistance.
I then switch customErrors=On, because in other circumstances I don't want to show a full stack dump to users. At this point all my exceptions as above are stripped of the message, so they become simply:
0|error|500||
Which is entirely reasonable. So I then add some exception handling within my Ajax postback code to trap the Application errors... and then I explicitly write the exception data I need back to the client:
39|error|202|You do not have access to this record. |
This all works well.
The problem then is with one particular type of exception - SQL Server database timeouts. These appear to be generic "unhandled exceptions", with the database timeout exception inside them. The catch is that this exception doesn't hit my handler at all - I can't find it and report what I want!
Looking at the exception traces, the timeout trace is of course longer than my "you have no access" trace, because it binds several things before it hits my (forced for test purposes) timeout. Looking at just the last bits of the two traces, I have (application eception):
System.Web.HttpUnhandledException (0x80004005): Exception of type System.Web.HttpUnhandledException' was thrown. ---> System.ApplicationException: You do not have access to this record.
at pages_***.HideEventLists(Boolean becauseNotAccessible) in *
at pages_**.AjaxTabLoad(Object sender, AjaxTabEventArgs e) in *
at userControls_tabs.panel_Load(Object sender, EventArgs e) in *
at System.Web.UI.Control.OnLoad(EventArgs e)
Versus (SQL Server timeout):
System.Web.HttpUnhandledException (0x80004005): Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.Data.SqlClient.SqlException (0x80131904): Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
So I'm wondering where precisely I should be trapping that SQL Server timeout exception? I don't really want to wrap every database access with it's own handler! Is there something I can do to get this exception passed the same way as my application exception?

Related

What would cause AmqpException in azure wcf relay?

I am logging the connection status events with the wcf relay, and I'm seeing something like this in the logs.
1/26 06:47:12 ERROR Service Bus ConnectionStatus: 'Reconnecting' Event. [(null)][42]
LastError: System.ServiceModel.CommunicationException: Exception of type 'System.ServiceModel.CommunicationException' was thrown. ---> Microsoft.ServiceBus.Messaging.Amqp.AmqpException: An AMQP error occurred (condition='amqp:unauthorized-access').
--- End of inner exception stack trace ---
This exception doesn't show up in the list on this microsoft page, and the only other post I can find anywhere related to this error message is here. However, that post does not have any recent comments or a resolution or workaround for the issue. Also, the exception doesn't have any stacktrace, so how am I supposed to troubleshoot this error?
I guess as a follow-up, I would ask whether this is anything to actually worry about if the wcf connection is never faulting.
Apparently, the token that the relay keeps refreshing to stay active requires the time on the server to match the azure service that it is connecting with, and if not, this type of error will show up. We were able to fix it by correcting the server time.

MySqlClient blacklisting server in ServerPool

Is there anything in the .NET MySqlClient (6.9.5.0) where when a MySQL server in the server pool is not responding (possibly due to temporary network issues), the server gets blacklisted or bypassed permanently? In our logging, we notice that an authentication error is thrown:
MySql.Data.MySqlClient.MySqlException (0x80004005): Authentication to host '<HOST>' for user '<USER>' using method 'mysql_native_password' failed with message: Reading from the stream has failed. ---> MySql.Data.MySqlClient.MySqlException (0x80004005): Reading from the stream has failed. ---> System.IO.EndOfStreamException: Attempted to read past the end of the stream.
Immediately after this error occurs, every attempt to write or read from the database fails, with this error message:
MySql.Data.MySqlClient.MySqlException (0x80004005): No available server found.
Any ideas? When the DB server was moved to be on the same host that our application is running on, the problem no longer occurs.
Two things strike me here:
The nature of your exception: System.IO.EndOfStreamException:
Attempted to read past the end of the stream.
And the fact that when the DB is located on the same machine as the
calling application, you do not encounter the issue.
To me, that's telling me that a transport or network related error is occurring, which is interrupting your stream. Once that occurs, your connection is reset, so subsequent calls to the DB are failing.
I would try to catch the EndOfStreamException, and attempt to gracefully close and reopen the connection. Something like below. If this allows subsequent calls to succeed, then you've found the issue... although keep in mind that in this structure the original call (within the try block) does not get called again. You will also need to properly need to recreate and open your connection on before making subsequent calls, since it is now always being disposed in the finally block.
try
{
//your work
}
catch (System.IO.EndOfStreamException ex)
{
System.Diagnostics.Debug.WriteLine("Stream exception caught: " + ex.Message.ToString());
}
finally
{
if(myConnection != null)
{
myConnection.Close();
myConnection.Dispose();
}
}

How to get rid of SqlException Timeout expired in WCF Service?

I'm working on asp.net web application, this application interacts with sql server Database through WCF Service. I get the following Exception sometimes but not every time.
System.Data.SqlClient.SqlException (0x80131904): Timeout expired.
The timeout period elapsed prior to completion of the operation or the server is not responding.
This failure occured while attempting to connect to the Principle server.
---> System.ComponentModel.Win32Exception (0x80004005):
The wait operation timed out
I know there are multiple reasons for this. There's a deadlock somewhere or The query is too complex.
At some place I have read that setting CommandTimeout of SqlCommand object can solve this issue.
So before applying this logic I want to know the strong reason of this issue and If there is any other workaround to be done to get rid of this issue please let me know. If I'm missing anything please let me know.

Connection Timeout Expired. The timeout period elapsed during the post-login phase

In my application, queries are working fine.
For a single select query, i am getting below error.
System.Data.EntityException: The underlying provider failed on Open.
---> System.Data.SqlClient.SqlException: Connection Timeout Expired. The timeout period elapsed during the post-login phase. The
connection could have timed out while waiting for server to complete
the login process and respond; Or it could have timed out while
attempting to create multiple active connections. The duration spent
while attempting to connect to this server was - [Pre-Login]
initialization=0; handshake=13914; [Login] initialization=0;
authentication=0; [Post-Login] complete=1062; --->
System.ComponentModel.Win32Exception: The wait operation timed out
I have ran sqlprofiler for detecting deadlocks but found none.
I am running simple query through entity framework
Any help would be appreciated
Here in stack overflow, there is a post called Help troubleshooting SqlException: Timeout expired on connection, in a non-load situation. It is a problem very similar to your problem. Please take a look.
I will quote (from that post) a comment as a possible cause of your problem:
"Not Enough Memory"
"You may have 'Auto Close' enabled"
"SQL Agent Jobs may be causing problems"
In that post you can see a very good explanation about the topic.
Maybe you forgot to .Close your connection before.
that's why: Or it could have timed out while attempting to create multiple active connections.
(I cant comment for now due to my reputation, dont take this as a full answer)
This looks like a problem with the Active Directory. Maybe a port is blocked (for example 135), so a timeout occurred.

What kind of exception is 'Connection Timed Out' - ASP.NET

I'm working on an asp.net application which communicates with web services. However quite often of late, the services have been down. I don't want to throw a generic Exception but rather a very specific one. What is the Exception name that handles 'connection timed out'. Thank you!
The correct one to throw is TimeoutException. The default message is The operation has timed-out
From MSDN:
The exception that is thrown when the time allotted for a process or operation has expired.

Categories