0

Followed the tutorial and my CloudSqlServlet.java init() function:

   try {
        ApiProxy.Environment env = ApiProxy.getCurrentEnvironment();
        Map<String, Object> attr = env.getAttributes();
    String hostname = (String) attr.get("com.google.appengine.runtime.default_version_hostname");

    String url = hostname.contains("localhost:")
            ? System.getProperty("cloudsql-local") : System.getProperty("cloudsql");
    log("connecting to: " + url);
    try {
        conn = DriverManager.getConnection(url);
    } catch (SQLException e) {
        throw new ServletException("Unable to connect to Cloud SQL", e);
    }

This is my appegnine-web.xml:

<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
  <version>1</version>
  <threadsafe>true</threadsafe>
  <runtime>java8</runtime>

  <use-google-connector-j>true</use-google-connector-j>

  <service>cloudsql</service>

  <system-properties>
    <property name="cloudsql" value="jdbc:google:mysql://${INSTANCE_CONNECTION_NAME}/${database}?user=${user}&amp;password=${password}" />
    <property name="cloudsql-local" value="jdbc:mysql://google/${database}?useSSL=false&amp;cloudSqlInstance=${INSTANCE_CONNECTION_NAME}&amp;socketFactory=com.google.cloud.sql.mysql.SocketFactory&amp;user=${user}&amp;password=${password}" />
  </system-properties>
</appengine-web-app>

The app compiles with maven. I then run the appengine server in one terminal:

mvn appengine:run

And in another terminal I'll run the sql proxy:

./cloud_sql_proxy -instances=skeleton-182342:europe-west1:trial1131=tcp:3306

Then when I access the server locally: localhost:8080/cloudsql The app throws an exception:

Caused by: java.lang.IllegalArgumentException: Invalid Cloud SQL instance [${INSTANCE_CONNECTION_NAME}], expected value in form [project:region:name].

And in the logs I can see that the properties are not inserted into the url:

log: CloudSQL: connecting to: jdbc:mysql://google/${database}?useSSL=false&cloudSqlInstance=${INSTANCE_CONNECTION_NAME}&socketFactory=com.google.cloud.sql.mysql.SocketFactory&user=${user}&password=${password}
JY2k
  • 2,879
  • 1
  • 31
  • 60

1 Answers1

0

As for your code, everything looks fine except for the ending in the line saying </system-properties>mv, where you may have a typo in mv.

Maybe this is the tutorial you were talking about, but in any case I recommend you to have a look at the official documentation for connecting a Java App Engine application to Cloud SQL.

For what I see, I think you may not have changed the app properties for your application with the appropriate values, and maybe you have the default ones. You can change them in the pom.xml file, which should look like the following (additional information here too):

<properties>
    <INSTANCE_CONNECTION_NAME>PROJECT:REGION:INSTANCE</INSTANCE_CONNECTION_NAME>
    <user>USER</user>
    <password>PASSWORD</password>
    <database>DB_NAME</database>

    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.source>1.8</maven.compiler.source>
  </properties>

Your application will be retrieving the properties from that file, so make sure to update your properties with the corresponding values.

dsesto
  • 7,864
  • 2
  • 33
  • 50