As an enterprise application developer you have to face lot of challenges. You are responsible for designing and developing highly scalable and fault tolerant systems. The systems that you develop are life line for your enterprise any downtime in it can cause a rippling effects and sometimes even loss of revenue. Your worries get more when you come to know that the application that you are working on shall be deployed on shared hosting environment. Though you have the SLA with the hosting company of 99.9 % uptime but servers do get crash and they backup or failover servers gets replaced so that your users get minimum downtime. I recently overcome this challenge and wants to share my experience with the community. The application that I developed was deployed on shared hosting environment with SQL Server on a separate box. The hosting company had a failover mechanism in place where by which if the server on which my database was running had some problems it will be taken over by the failover server. Now the question over here how will my application know that the main server is down and I have to switch over to the failover server because the connection string was already hardcoded in the web.config file which was pointing to the main server.This application was developed using Silverlight and WCF Ria Services. I took up this challenge and in the end came out with the elegant solution. So keep reading.
I need to device a solution by which my app knows that which sql server it needs to point out before retrieving or submitting the data. For the readers information both the database servers were always sync with the data , there was bi-directional synchronization mechanism already in place by the hosting company. So I was assured that my users would never get any stale data. After doing a bit of head scratching I found the way out. As I was using Entity Framework ,Visual Studio generates code based on your logical model when the solution was build. This generated code can be found under your .edmx file. It has the extension .designer.cs . Refer to the figure below.
If you open this file you would find one partial class which inherits from the ObjectContext. As this class was partial I can always extend it in other class and use its existing partial methods. In my case I used the partial method OnContextCreated . The code of this this extended partial class is shown below.
The above code is no teaser. In the OnContextCreated method I check for the main server connection is working or not by calling the function CheckIfSqlServerisRunning() . If this function returns me false then I call another function which gets me the alternate connection for the failover sql server. CheckIfSqlServerisRunning function will return me true if I am able to open the connection successfully else it will return false. But the real potatoes and meat is in GetAlternativeConnection function. This function builds up the dynamic Sql connection string using SqlConnectionStringBuilder class and then I use the EntityConnectionStringBuilder class to make the Entity connection string.
The OnContextCreated method will be called whenever your domain service methods wants to perform any database operations and that is the catch. You always ensure that you give a valid sql connection to the service so it doesn’t fails and your user can get data seamlessly without knowing from which database server the data is coming from. You change the connection on the fly if the main server is down.
As the saying goes “WITH GREAT POWER COMES GREAT RESPONSIBILITIES” , the same thing applies to a developer. You carry a loads of responsibilities on your shoulder for the enterprise to become successful. Any bad code written by you can not only ruin you but your organization too. So first think and then code.