Question

I am writing my first .NET MVC application and I am using the Code-First approach. I have recently learned how to configure two SQL Servers installations for High Availability using a Mirror Database and a Witness (not to be confused with Failover Clusters) to do the failover process. I think this would be a great time to practice both things by mounting my web app into a highly-available DB.

Now, for what I learned (correct me if I'm wrong) in the mirror configuration you have the witness failover to the secondary DB if the first one goes down... but your application will also need to change the connection string to reference the secondary server.

What is the best approach to have both addresses in the Web.config (or somewhere else) and choosing the right connection string?

Was it helpful?

Solution

I have zero experience with connecting to Mirrored databases, so this is all heresy! :)

The short of it may be you may not have to do anything special, as long as you pass along the FailoverPartner attribute in your connection string. The long of it is you may need additional error handling to attempt a new connection so the data provide will actually use the FailoverPartner name in the new connection.

There seems to be some good information with Connecting Clients to a Database Mirroring Session to get started. Have you had a chance to check that out?

If not, its there with Making the Initial Connection where they introduce the FailoverPartner attribute of the ConnectionString property attributes.

Reconnecting to a Database Mirroring Session suggests that on any client disconnect due to failover, the client will need to trap this exception and be prepared to reconnect:

The application must become aware of the error. Then, the application needs to close the failed connection and open a new connection using the same connection string attributes.

If the FailoverPartner attribute is available, this process should be relatively transparent to the client.

If the above doesn't work, then you might need to actually introduce some logic at the application tier to track who is the primary node, the failover node, and connection strings for each, and be prepared to persist that information somewhere - much like the data access provider should be doing for us (eyes wide open).

There is also this ServerFault post on database mirroring with Sql Server that might be of interest from an operational viewpoint that has additional reference information.

Hopefully someone with actual experience will back up any of this!

OTHER TIPS

This may be totally off base, but what if you had a load balancer between your web server and the database servers?

The Load Balancer would have both databases in it's pool, using basic health check techniques (e.g ping, etc).

Your configuration would then only need to point to the IP of the Load Balancer, and wouldn't need to change.

This is what these network devices are good for. It's not the job of the programming framework (ASP.NET) to make decisions on the health of servers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top