Google Cloud Platform

Using Google Cloud SQL

Python |Java |PHP |Go

This document describes how to use Google Cloud SQL instances with the App Engine Java SDK.

  1. Creating a Cloud SQL instance
  2. Build a starter application and database
  3. Connect to your database
  4. Using a local MySQL instance during development
  5. Size and access limits
  6. Using Persistence APIs with Cloud SQL

To learn more about Google Cloud SQL, see the Google Cloud SQL documentation.

If you haven't already created a Google Cloud SQL instance, the first thing you need to do is create one.

Creating a Cloud SQL Instance

A Cloud SQL instance is equivalent to a server. A single Cloud SQL instance can contain multiple databases. Follow these steps to create a Google Cloud SQL instance:

  1. Sign into the Google Cloud Platform Console.
  2. Create a new project, or open an existing project.
  3. From within a project, select Cloud SQL to open the Cloud SQL control panel for that project.
  4. Click New Instance to create a new Cloud SQL instance in your project, and configure your size, billing and replication options. You'll notice that the App Engine application associated with your current project is already authorized to access this new instance. For more information on app authorization see the Access Control topic in the Cloud SQL docs.

That's it! You can now connect to your Google Cloud SQL instance from within your app, or any of these other methods.

MySQL case sensitivity

When you are creating or using databases and tables, keep in mind that all identifiers in Google Cloud SQL are case-sensitive. This means that all tables and databases are stored with the same name and case that was specified at creation time. When you try to access your databases and tables, make sure that you are using the exact database or table name.

For example, if you create a database named PersonsDatabase, you will not be able to reference the database using any other variation of that name, such as personsDatabase or personsdatabase. For more information about identifier case sensitivity, see the MySQL documentation.

Build a starter application and database

The easiest way to build an App Engine application that accesses Google Cloud SQL is to create a starter application then modify it. This section leads you through the steps of building an application that displays a web form that lets users read and write entries to a guestbook database. The sample application demonstrates how to read and write to a Google Cloud SQL instance.

Step 1: Create your App Engine sample application

Follow the instructions in Creating a Project to create a simple App Engine application.

Step 2: Grant your App Engine application access to the Google Cloud SQL instance

You can grant individual Google App Engine applications access to a Google Cloud SQL instance. One application can be granted access to multiple instances, and multiple applications can be granted access to a particular instance. To grant access to a Google App Engine application, you need its application ID which can be found at the Google App Engine administration console under the Applications column.

Note: An App Engine application must be in the same region (either EU or US) as a Google Cloud SQL instance to be authorized to access that Google Cloud SQL instance.

To grant an App Engine application access to a Google Cloud SQL instance:

  1. In the Google Cloud Platform Console, select a project.
  2. Click the name of the instance to which you want to grant access.
  3. In the instance dashboard, click Edit.
  4. In the Instance settings window, enter your Google App Engine application ID in the Authorized App Engine applications section. You can grant access to multiple applications, by entering them one at a time.
  5. Click Confirm to apply your changes.

After you have added authorized applications to your Google Cloud SQL instance, you can view a list of these applications in the instance dashboard, in the section titled Applications.

Step 3: Create your database and table

For example, you can use MySQL Client to run the following commands:

  1. Create a new database called guestbook using the following SQL statement:

    CREATE DATABASE guestbook;
  2. Inside the guestbook database create a table called entries with columns for the guest name, the message content, and a random ID, using the following statement:

    CREATE TABLE guestbook.entries (
      guestName VARCHAR(255),
      content VARCHAR(255),
      PRIMARY KEY(entryID)

After you have set up a bare-bones application, you can modify it and deploy it.

Connect to your database

One way to connect your Java App Engine application to a Cloud SQL databases is to use the Google Plugin for Eclipse. The following steps assume that you created an application following the Creating a New Web Application instructions. For the sample we are buiding here, we don't need the Google Web Toolkit (GWT) so you can create your web application without that SDK.

Note that the code below assumes that you created a web application project with a Project name specified as "Guestbook" and a Package specified as "guestbook". If you choose different values, you will have to make the appropriate changes in the servlet and web form.

  1. Enable MySQL Connector/J
  2. Connect and Post to Your Database
  3. Create Your Webform
  4. Map Your Servlet

Enable MySQL Connector/J

The MySQL Connector/J is available in Google App Engine, but it's not included in an app unless the app explicitly asks for it. To do that, add a <use-google-connector-j> element as a child of the <appengine-web-app> element in the app's appengine-web.xml file as shown in the following example:

<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="">

Connect and post to your database

Replace doGet() in GuestbookServlet with the following code. Replacing your-instance-name with your Google Cloud SQL instance name and your-project-id with the literal project ID. This code performs the following actions:

  • Initiates the connection by calling getConnection()
  • Collects the contents from a web form and posting them to the server
  • Redirects the user to a file called guestbook.jsp (we will create it in a later step)
import java.sql.*;
import javax.servlet.http.*;

public class GuestbookServlet extends HttpServlet {
  public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    String url = null;
    try {
      if (SystemProperty.environment.value() ==
          SystemProperty.Environment.Value.Production) {
        // Load the class that provides the new "jdbc:google:mysql://" prefix.
        url = "jdbc:google:mysql://your-project-id:your-instance-name/guestbook?user=root";
      } else {
        // Local MySQL instance to use during development.
        url = "jdbc:mysql://";

        // Alternatively, connect to a Google Cloud SQL instance using:
        // jdbc:mysql://ip-address-of-google-cloud-sql-instance:3306/guestbook?user=root
    } catch (Exception e) {

    PrintWriter out = resp.getWriter();
    try {
      Connection conn = DriverManager.getConnection(url);
      try {
        String fname = req.getParameter("fname");
        String content = req.getParameter("content");
        if (fname == "" || content == "") {
              "<html><head></head><body>You are missing either a message or a name! Try again! " +
              "Redirecting in 3 seconds...</body></html>");
        } else {
          String statement = "INSERT INTO entries (guestName, content) VALUES( ? , ? )";
          PreparedStatement stmt = conn.prepareStatement(statement);
          stmt.setString(1, fname);
          stmt.setString(2, content);
          int success = 2;
          success = stmt.executeUpdate();
          if (success == 1) {
                "<html><head></head><body>Success! Redirecting in 3 seconds...</body></html>");
          } else if (success == 0) {
                "<html><head></head><body>Failure! Please try again! " +
                "Redirecting in 3 seconds...</body></html>");
      } finally {
    } catch (SQLException e) {
    resp.setHeader("Refresh", "3; url=/guestbook.jsp");

Although the above example connects to the Google Cloud SQL instance as the root user without any password, you can also connect to the instance indicating one:

Connection conn = DriverManager.getConnection(
    "user", "password");

For information about creating MySQL users, see Adding Users in the MySQL documentation.

Managing connections

An App Engine application is made up of one or more modules. Each module consists of source code and configuration files. An instance instantiates the code which is included in an App Engine module, and a particular version of module will have one or more instances running. The number of instances running depends on the number of incoming requests. You can configure App Engine to scale the number of instances automatically in response to processing volume (see Instance scaling and class).

When App Engine instances talk to Google Cloud SQL, each App Engine instance cannot have more than 12 concurrent connections to a Cloud SQL instance. Always close any established connections before finishing processing a request. Not closing a connection will cause it to leak and may eventually cause new connections to fail. You can exit this state by shutting down the affected App Engine instance.

You should also keep in mind that there is also a maximum number of concurrent connections and queries for each Cloud SQL instance, depending on the tier (see Cloud SQL pricing). For guidance on managing connections, see How should I manage connections? in the "Google Cloud SQL FAQ" document.

Create your webform

Next, we'll create the front-facing part of the sample application, which list the entries of your entries table and provides a simple form to post new entries.

In your war/ directory, create a new file called guestbook.jsp with the following code, replacing instance_name with your instance name:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.List" %>
<%@ page import="java.sql.*" %>
<%@ page import="" %>


String url = null;
if (SystemProperty.environment.value() ==
    SystemProperty.Environment.Value.Production) {
  // Load the class that provides the new "jdbc:google:mysql://" prefix.
  url = "jdbc:google:mysql://your-project-id:your-instance-name/guestbook?user=root";
} else {
  // Local MySQL instance to use during development.
  url = "jdbc:mysql://";

Connection conn = DriverManager.getConnection(url);
ResultSet rs = conn.createStatement().executeQuery(
    "SELECT guestName, content, entryID FROM entries");

<table style="border: 1px solid black">
<th width="35%" style="background-color: #CCFFCC; margin: 5px">Name</th>
<th style="background-color: #CCFFCC; margin: 5px">Message</th>
<th style="background-color: #CCFFCC; margin: 5px">ID</th>

while ( {
    String guestName = rs.getString("guestName");
    String content = rs.getString("content");
    int id = rs.getInt("entryID");
<td><%= guestName %></td>
<td><%= content %></td>
<td><%= id %></td>

<br />
No more messages!
<p><strong>Sign the guestbook!</strong></p>
<form action="/sign" method="post">
    <div>First Name: <input type="text" name="fname"></input></div>
    <br /><textarea name="content" rows="3" cols="60"></textarea>
    <div><input type="submit" value="Post Greeting" /></div>
    <input type="hidden" name="guestbookName" />

Map your servlet

Finally, override your web.xml with the following code to map your servlet correctly:

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi=""
xsi:schemaLocation="" version="2.5">

That's it! Now you can deploy your application and try it out!

Using a local MySQL instance during development

The Guestbook example above shows how your application can connect to a Cloud SQL instance when the code runs in App Engine and connect to a local MySQL server when the code runs in the Development Server. We encourage this pattern to minimize confusion and maximize flexibility.

Size and access limits

The following limits apply to Google Cloud SQL:

Instance Connections

  • Each tier allows for maximum concurrent connections and queries. For more information, see the pricing page.
  • There is a limit of 100 pending connections independent of tier.
  • Establishing a connection takes about 1.25 ms on the server side. Given the 100 pending connection limit, this means a maximum of 800 connections per second. If more than 100 clients try to connect simultaneously, some of them will fail.

These limits are in place to protect against accidents and abuse. For questions about increasing these values, contact the team.

Instance Size

The size of all instances is limited to 250GB by default. Note that you only pay for the storage that you use, so you don’t need to reserve this storage in advance. If you require more storage, up to 500GB, then it is possible to increase limits for individual instances for customers with a Silver or higher Google Cloud support package.

Google App Engine Limits

Requests from Google App Engine applications to Google Cloud SQL are subject to the following time and connection limits:

  • All database requests must finish within the HTTP request timer, around 60 seconds.
  • Offline requests like cron tasks have a time limit of 10 minutes.
  • Requests from App Engine modules to Google Cloud SQL are subject to the type of module scaling and instance residence time in memory.
  • Each App Engine instance cannot have more than 12 concurrent connections to a Google Cloud SQL instance.

Google App Engine applications are also subject to additional Google App Engine quotas and limits as discussed on the Quotas page.

Using Persistence APIs with Cloud SQL

There are several ways to connect to MySQL using Java. The most basic one is via JDBC as shown above. You can also connect using Java Persistence API (JPA) and Java Data Objects (JDO). Some JPAs, such as Hibernate, DataNucleus, and EclipseLink, have their own custom additions.

To use persistence providers to connect to Cloud SQL, make sure you get the right JARs from the upstream distros, configure the javax.persistence.jdbc.driver to use the com.mysql.jdbc.GoogleDriver, and point the javax.persistence.jdbc.url to the desired Cloud SQL instance using a jdbc:google:mysql:// prefix in the connection string. To allow the same JPA code to run in both production and locally with dev_appserver, use the following pattern:

import java.util.Map;
import java.util.HashMap;

    // Set the persistence driver and url based on environment, production or local.
    Map<String, String> properties = new HashMap();
    if (SystemProperty.environment.value() ==
          SystemProperty.Environment.Value.Production) {
    } else {

    // Create a EntityManager which will perform operations on the database.
    EntityManagerFactory emf = Persistence.createEntityManagerFactory(
        "persistence-unit-name", propertiesMap);

For JDO, create a PersistenceManagerFactory instead an EntityManagerFactory:

PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(propertiesMap, "persistence-unit-name");

Complete examples are available at for: