Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Cheatsheet: Relational Database Management Systems ComparisonEstimating Mean Uptime for Team-based uptime measurements »
    comments

    In the previous article, "Estimating Mean Uptime for Team-based uptime measurements" uptime planning was discussed in some detail.  The calculation you could use to set team goals was provided.  All of these goals in general are for helping gauge how we are doing and where we have growth areas, but how do we even use something like database mirroring with our applications?

    In most .NET-driven development, and when a SQL Server database is behind the application, a connection string is involved.  In particular, ADO.NET connection strings are commonly used to connect to SQL Server.  One thing that the ADO.NET connection string offers an application is the Failover Partner property.  The Failover Partner property provides an application or service the ability to automatically decide to look to a secondary source if the primary is unavailable.  In conjunction with SQL Server’s database mirroring, this can be very powerful and provide the ability to help us meet our projected uptime goals.

    Failover Partner

    The Failover Partner property is used with the ADO.NET or SQL Native Client connections.  The connection string would be formed as follows.

    Data Source=PrincipalServer;Failover Partner=MirrorServer;Initial Catalog=AdventureWorks;Integrated Security=True;

    The Failover Partner property is available in SQL Server 2005, 2008, 2008 R2 and, as of this article’s publish date, SQL Server 2012 RC0.

    This can be seen in detail on http://www.connectionstrings.com/sql-server-2008.

    The Failover Partner property is all that is needed for the connection string to be well-formed to take into account a mirrored SQL Server instance.  To see this in action and provide testing on both normal operations and a failover event, we’ll use a .NET Windows Form Application.

    The application’s interface consists of a grid view, a button to load the grid view, and an option to show the connection string using the Failover Partner property.

    The design of this form allows a connection string with the Failover Partner setting and without.  If the “User Mirror Connection String?” check box is checked, the connection string will use the Failover Partner property.  The code to determine this is as follows.

    1. string strconn;
    2.             switch (checkUseMirrorString.Checked)
    3.             {
    4.                 case true:
    5.                     strconn = @"Data Source=ONPNT\RC0;Failover Partner=ONPNT\RC0_Mirror;Initial Catalog=AdventureWorks;Integrated Security=True;";
    6.                     break;
    7.                 default:
    8.                     strconn = @"Data Source=ONPNT\RC0;Initial Catalog=AdventureWorks;Integrated Security=True;";
    9.                     break;
    10.             }

    The remaining code in the form handles loading the grid and providing a notification, message box, of the current connection string being used.

    1. SqlConnection conn = new SqlConnection(strconn);
    2.             SqlDataAdapter da = new SqlDataAdapter("select TOP 100 * from Sales.SalesOrderHeader", strconn);
    3.             SqlCommandBuilder cmd = new SqlCommandBuilder(da);
    4.             DataTable dt = new DataTable();
    5.  
    6.             try
    7.             {
    8.                 da.Fill(dt);
    9.                 conn.Open();
    10.                 Salesgrid.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
    11.                 Salesgrid.ReadOnly = true;
    12.                 Salesgrid.DataSource = dt;
    13.             }
    14.             catch (Exception ex)
    15.             {
    16.                 MessageBox.Show(ex.Message.ToString());
    17.             }
    18.             finally
    19.             {
    20.                 conn.Close();
    21.                 dt.Dispose();
    22.                 da.Dispose();
    23.             }

    This code is not complex and better methods may exist.  For demonstration purposes, this code will help us see the Failover Partner usage in action.

    The database AdventureWorks has been configured for mirroring to a secondary instance acting as the mirror.  If you need help setting up mirroring, refer to, “Mirroring Hands On with Developer Edition”.

    There are a few tests to run.

    1. Normal operations without the Failover Partner
    2. Test when the principal database is offline and no Failover Partner is used
    3. Test normal operation with the Failover Partner and the mirror is offline
    4. Test normal operations with the Failover Partner and the mirror is online
    5. Test normal operations and the mirrored partnership has failed to the mirrored database
    6. Test normal operations and the mirrored database has failed back to the principal

    The results

    1. Grid loading successful
    2. Grid loading failure with connection error
    3. Grid loading successful.  SQL connection showing on Principal instance for confirmation
    4. Grid loading successful.  SQL connection showing on Principal instance for confirmation
    5. Grid loading successful.  SQL connection showing on Mirror instance for confirmation
    6. Grid loading successful.  SQL connection showing on Principal instance for confirmation

    A last step is performed for mirroring being removed from the principal database.  In this test case, the grid successfully loads even with mirroring not being configured on the database. However, taking the database offline does not check for the second instance in this case.

    This confirms that the connection string, even with the Failover Partner configured, is database mirroring aware.  If the mirror is not configured, even with the Failover Partner set, the connection will persist to look to the primary data source.

    Results and Conclusions

    The Failover Partner configuration in an ADO.NET or SQL Native Client connection can be invaluable when Database Mirroring is configured.  The tests run in this article show that the connection is stable on several events that are present in the mirroring partnership and also exposes what happens if that partnership is broken.  Keep this in mind if taking your mirror configuration down for any reason.  It is more reliable to use the Pause Mirror commands when the need for maintenance on a mirroring session is needed.  You can read more about pausing database mirroring in this article, “Stop Mirroring for server reboot”.

    Even if you have an application in development, design, or an existing application, requesting this change to a connection string is a critical planning step.  If the application in question is external and database mirroring is configured locally, contact the vendor to request a change to their connection string, or request the steps that you and the vendor may take to work together to add the functionality.  In most cases, the vendor will work with you and even put this option into their applications and systems as a beneficial option to other customers.

    About the Author

    Ted Krueger is a SQL Server MVP and has been working in development and database administration for 13+ years. Specialties range from High Availability and Disaster / Recovery setup and testing methods down to custom assembly development for SQL Server Reporting Services. Ted blogs and is also one of the founders of LessThanDot.com technology community. Some of the articles focused on are Backup / Recovery, Security, SSIS and working on SQL Server and using all of the SQL Server features available to create stable and scalable database services. @onpnt
    Social SitingsTwitterLinkedInLTD RSS Feed
    654 views
    Instapaper

    No feedback yet

    Leave a comment


    Your email address will not be revealed on this site.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)