Accessing Google Cloud SQL From App Engine

Accessing Google Cloud SQL from an App Engine application is relatively straightforward. To start, an application must first specify a host name for the Cloud SQL servers.

For example, here’s the host name for PHP applications:

:/cloudsql/[Google-Cloud-Project-Name]:[Cloud-SQL-Instance-Name]

Java accesses Cloud SQL through a special JDBC driver. Here’s the proper host name for it:

jdbc:google:mysql://[Google-Cloud-SQL-Project-Name]:[Cloud-SQL-Instance-Name]/[Database-Name]

Secondly, Cloud SQL must whitelist incoming connections from permitted App Engine applications. To do this, open up the Google Cloud console and select the project you’re using. Then press the Cloud SQL option on the left hand navigation bar:

Click the New Instance button:

On the bottom of the form there’s an option to whitelist named App Engine applications. Type in the application ID of the App Engine application using the database:

Click the Confirm button to finish setting up the database.

App Engine Downtime Notices

App Engine downtime and maintenance notices are posted to the Google Group google-appengine-downtime-notify, located at https://groups.google.com/forum/#!forum/google-appengine-downtime-notify . It’s a good idea to monitor this list for any issues with the App Engine platform.

It’s especially important to subscribe to these notices if your application is still on the M/S datastore. The M/S datastore is occasionally moved into a read-only state for maintenance, and these maintenance periods are announced over the downtime list.

Subscribing to the google-appengine-downtime-notify list is easy: go to the above linked address and click on the button marked Join Group. As you can see from the below pictures, this list is extremely low-traffic (less than 1 email a day).

Using Javascript To Forward A User (And Keeping Track Of The Forward)

URL hashes (the part after the # sign) are useful for keeping track of application state. Here’s a short example of how they can be used.

The below code fragment forwards the user to a Google search for the value held in the variable q . The useful part is that this code also sets a hash to mark that the forward has taken place. This allows the page to keep track of the forwarding status, and to prevent reissuing the forward – for instance, if the user presses the back button after being forwarded.

if (location.hash != "#forwarder") {
    location.hash = "forwarder";
    var search_url = "http://www.google.com/search?q=" + escape(q);
    //Set the timeout to 10 seconds.
    setTimeout(function(){location.assign(search_url);},10000);
}

To see how this works, consider the below example. The first alert will show that there’s no hash recorded in the URL (the alert will be blank). The second line sets a hash of forwarder and the third line shows the value of the current hash (which will be the value we set on the second line). This will cause the if statement to evaluate to false, preventing the forwarding from taking place. The same process (recognizing the hash and blocking the forward) happens when the user clicks the back button on the above forwarding code.

alert(location.hash);
location.hash = "forwarder";
alert(location.hash);
if (location.hash != "#forwarder") {
    location.hash = "forwarder";
    var search_url = "http://www.google.com/search?q=" + escape(q);
    //Set the timeout to 10 seconds.
    setTimeout(function(){location.assign(search_url);},10000);
}