Mehdi Benmoha
Mehdi Benmoha

Reputation: 3935

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 --

Upvotes: 1

Views: 1836

Answers (2)

seb
seb

Reputation: 4096

Two things are required for inter-project Cloud SQL communication:

  • The project containing the Cloud SQL instance must have Cloud SQL Admin API enabled
  • The project wanting to access it must have an appropriate Cloud SQL IAM role on the project containing the Cloud SQL Instance (source):

App Engine and Cloud SQL in different projects

For App Engine applications and Cloud SQL instances in different projects, you must grant the default appengine service account ([PROJECT-ID]@appspot.gserviceaccount.com) one of the following IAM roles:

  • Cloud SQL Client
  • Cloud SQL Editor
  • Cloud SQL Admin

Upvotes: 0

Vadim
Vadim

Reputation: 5126

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.

Upvotes: 1

Related Questions