Cloud Developer How To: Connect Your Web App to a Cloud Database and Avoid Disaster

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...
March 28, 2012

When you create a dynamic website, the cloud database becomes the coal bin that supplies the engine with fuel to burn. Most programmers make the same common mistake at least once in their career: accidentally uploading the database connection file that connects to the testing environment instead of the production environment. The result is a website that crashes with a database error or the data entered by customers is sent to the wrong database.

The issue is disastrous for webmasters and the developer responsible for the mistake.  The answer is to dynamically detect the IP address of the cloud host server and connect to the database associated with the host IP address. The solution protects you from crashing the website after accidentally uploading the wrong database connection file. Here is how you can resolve the issue in your code for both the .NET and PHP languages.

 

Dynamically Connect to Your Cloud Production Environment in PHP

It’s simpler to code the dynamic connection in the PHP language than .NET. You can use any text-based editor to change the code. For instance, PHP programmers like to use Notepad++, which is a free text editor, but you can also use Windows Notepad for a quick change on a computer that does not have a PHP editor installed. The code to dynamically connect to the cloud versus the local test environment:

 

$ip = $_SERVER[‘SERVER_ADDR’];

If ($ip == ‘cloudip’)

{

$servername = ‘cloud_db_name’;

} else

{

$servername = ‘test_db’;

}

$con = mysql_connect($servername,”user”,”pass”);

 

This script detects the IP of the server on which the script runs to detect which host is running the database connection. If the server’s IP is the IP of your cloud host, the database server’s name ($servername) is set to the production server. All other IPs are set to the test connection. The connection defaults to the test server and not the production server. This protects you from changing servers and then accidentally forgetting to change the connection file. The default connection is set to the testing environment, so any mistakes are made in the test environment and not your production environment.

 

Connecting Dynamically in .NET’s web.config File

C# and VB.NET work differently than PHP’s include files. The web.config file contains all the settings for your website’s code. You can use this file along with your data connection file or data layer namespace to dynamically connect to the cloud server. You’ll need Visual Studio to edit and test the new code changes in the debugger, so open your project and the web.config file in the Visual Studio software.

 

In .NET 3.0 and up, the web.config file contains a section called “connectionStrings” in the XML. If you don’t already have the section, you can add it to the file. In this example, a connection to a Microsoft SQL Server is made, because most .NET websites work with SQL Server. To create a dynamic connection, you create two entries: one that connects to the test database and one that connects to the cloud production database. The following code shows you an example of two connection strings:

 

<connectionStrings>

<add

name=”production”

connectionString=”Data Source=serverName;Initial

Catalog=yourdatabasename;Persist Security Info=True;User

ID=user;Password=password”

providerName=”System.Data.SqlClient”

/>

<add

name=”test”

connectionString=”Data Source=serverName;Initial

Catalog=yourdatabasename;Persist Security Info=True;User

ID=user;Password=password”

providerName=”System.Data.SqlClient”

/>

</connectionStrings>

Now that you set up the web.config, you can approach the connection logic two ways. You can use the same method that was used in the PHP sample and detect the server’s IP address in your code, or you can set up a flag in the web.config file. The flag sets the config file as “test” or “production,” and your code reads the flag and applies the appropriate connection. In this example, the .NET code detects the IP of the server, because it protects you in case you accidentally upload any connection files by mistake. The following code shows you how to dynamically set the connection in C# and in VB.NET:

 

C#

stringip = Request.ServerVariables[“LOCAL_ADDR”];

stringdb = string.Empty;

if (ip == “cloudip”)

{

db = “production”;

} else

{

db = “test”;

}

System.Configuration.ConnectionStringSettings conn;

conn =

ConfigurationManager.ConnectionStrings[db].ConnectionString

 

VB.NET

Dim ip As String

Dim db As String

ip = Request.ServerVariables(“LOCAL_ADDR”)

If ip = “cloudip” Then

db = “production”

Else

db = “test”

End If

Dim conn As System.Configuration.ConnectionStringSettings

conn =

ConfigurationManager.ConnectionStrings(db).ConnectionString

 

After you create the code, click the “Run” button in the Visual Studio toolbar to execute it in the debugger. The connection will automatically connect to your testing environment. After you upload it to the production server, the connection will dynamically set and connect to production. If you accidentally upload the file in the future, the code still dynamically sets, so you will not crash your production website.

 

You may also like...

Post a comment