How do I address sudden loss of connection to SQL Azure in my Azure role?

前端 未结 2 1869
庸人自扰
庸人自扰 2020-12-29 09:05

My Azure role grabs stuff to process from a database - it holds an instance of System.Data.SqlClient.SqlConnection and periodically creates an SqlCommand<

相关标签:
2条回答
  • 2020-12-29 09:33

    We use TransientFaultHandling and it doesn't handle all of the strange exceptions.

    For example, this one popped up yesterday:

    The service has encountered an error processing your request. Please try again. Error code 40143. A severe error occurred on the current command. The results, if any, should be discarded. , stacktrace at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, . . .

    The reasonable approach that will work even with this:

    1. Identify a coarse-grained pseudo-transaction where the call happens.
    2. Wrap this block in a try-catch.
    3. on exception, 'roll back' the pseudo-transaction.

    Example of a typical workflow:

    • A get Azure queue message
    • B query data from SQL Azure
    • C process data,
    • D upload results
    • E delete message.

    Wrap B through C together in a try-catch. If something happens during 'harmless' SQL Azure call, simply bail out without deleting the message, it will simply pop up again after visibility timeout expires.

    Actually, this is very common approach: organize into transaction-like blocks, wrap block into try-catch, neatly roll back on exception. And never, never assume that some calls do not fail. All call fail from time to time.

    0 讨论(0)
  • 2020-12-29 09:34

    I would strongly recommend you have a look at the Transient Fault Handling Framework for SQL Azure

    This will help you handle retry logic for both connection and query attempts, I am using this in production and it works great. There is also a nice article on technet that might be of some use.

    [EDIT: 17 Oct 2013]

    It looks like this has been picked up by the patterns and practices team at The Transient Fault Handling Application Block

    0 讨论(0)
提交回复
热议问题