Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9156645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:49:11+00:00 2026-06-17T12:49:11+00:00

We’re migrating SQL to Azure. Our DAL is Entity Framework 4.x based. We’re wanting

  • 0

We’re migrating SQL to Azure. Our DAL is Entity Framework 4.x based. We’re wanting to use the Transient Fault Handling Block to add retry logic for SQL Azure.

Overall, we’re looking for the best 80/20 rule (or maybe more of a 95/5 but you get the point) – we’re not looking to spend weeks refactoring/rewriting code (there’s a LOT of it). I’m fine re-implementing our DAL’s framework but not all of the code written and generated against it anymore than we have to since this is already here only to address a minority case. Mitigation >>> elimination of this edge case for us.

Looking at the possible options explained here at MSDN, it seems Case #3 there is the “quickest” to implement, but only at first glance. Upon pondering this solution a bit, it struck me that we might have problems with connection management since this circumvent’s Entity Framework’s built-in processes for managing connections (i.e. always closing them). It seems to me that the “solution” is to make sure 100% of our Contexts that we instantiate use Using blocks, but with our architecture, this would be difficult.

So my question: Going with Case #3 from that link, are hanging connections a problem or is there some magic somewhere that’s going on that I don’t know about?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:49:12+00:00Added an answer on June 17, 2026 at 12:49 pm

    I’ve done some experimenting and it turns out that this brings us back to the old “managing connections” situation we’re used to from the past, only this time the connections are abstracted away from us a bit and we must now “manage Contexts” similarly.

    Let’s say we have the following OnContextCreated implementation:

    private void OnContextCreated()
    {
        const int maxRetries = 4;
        const int initialDelayInMilliseconds = 100;
        const int maxDelayInMilliseconds = 5000;
        const int deltaBackoffInMilliseconds = initialDelayInMilliseconds;
    
        var policy = new RetryPolicy<SqlAzureTransientErrorDetectionStrategy>(maxRetries,
                                                                                TimeSpan.FromMilliseconds(initialDelayInMilliseconds),
                                                                                TimeSpan.FromMilliseconds(maxDelayInMilliseconds),
                                                                                TimeSpan.FromMilliseconds(deltaBackoffInMilliseconds));
        policy.ExecuteAction(() =>
                {
                    try
                    {
                        Connection.Open();
                        var storeConnection = (SqlConnection) ((EntityConnection) Connection).StoreConnection;
                        new SqlCommand("declare @i int", storeConnection).ExecuteNonQuery();
                        //Connection.Close();
                        // throw new ApplicationException("Test only");
                    }
                    catch (Exception e)
                    {
                        Connection.Close();
    
                        Trace.TraceWarning("Attempted to open connection but failed: " + e.Message);
    
                        throw;
                    }
                }
            );
    }
    

    In this scenario, we forcibly open the Connection (which was the goal here). Because of this, the Context keeps it open across many calls. Because of that, we must tell the Context when to close the connection. Our primary mechanism for doing that is calling the Dispose method on the Context. So if we just allow garbage collection to clean up our contexts, then we allow connections to remain hanging open.

    I tested this by toggling the comments on the Connection.Close() in the try block and running a bunch of unit tests against our database. Without calling Close, we jumped up to ~275-300 active connections (from SQL Server’s perspective). By calling Close, that number hovered at ~12. I then reproduced with a small number of unit tests both with and without a using block for the Context and reproduced the same result (different numbers – I forget what they were).

    I was using the following query to count my connections:

    SELECT s.session_id, s.login_name, e.connection_id,
          s.last_request_end_time, s.cpu_time, 
          e.connect_time
    FROM sys.dm_exec_sessions AS s
    INNER JOIN sys.dm_exec_connections AS e
    ON s.session_id = e.session_id
    WHERE login_name='myuser'
    ORDER BY s.login_name
    

    Conclusion: If you call Connection.Open() with this work-around to enable the Transient Fault Handling Block, then you MUST use using blocks for all contexts you work with, otherwise you will have problems (that with SQL Azure, will cause your database to be “throttled” and ultimately taken offline for hours!).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am confused How to use looping for Json response Array in another Array.
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I am trying to understand how to use SyndicationItem to display feed which is
I have a small JavaScript validation script that validates inputs based on Regex. I
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.