Search code examples
javagoogle-app-enginegoogle-cloud-platformgoogle-cloud-sqlapp-engine-flexible

Unable to connect to Cloud SQL from different project on App Engine flex Java


Here's the exception that's thrown:

javax.servlet.ServletException: javax.servlet.ServletException: org.glassfish.jersey.server.ContainerException: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.

I also get this message later on the stack trace:

Caused by: java.lang.RuntimeException: The Google Cloud SQL API is not enabled for project [xxxxxx]. Please use the Google Developers Console to enable it: https://console.cloud.google.com/apis/api/sqladmin/overview?project=xxxxxx

When I click on the console link, I can see that the Cloud SQL Api is enabled and the code was working perfectly when the database was on the same project. I also added the AppEngine flexible service account that has the App Engine flexible environment Service Agent role in the project containing the Cloud SQL database with the role Cloud SQL Client.

What should I try next ? Thanks

UPDATE

pom.xml:

-- update2 there's no more need for that file --


Solution

  • When accessing a Cloud SQL instance in a different project, it's important that the Cloud SQL Admin API is enabled in the source project. Copy the link in the error message and replace the project id with the other project id. The current message is misleading as it currently assumes the instance is in the same project.