1

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

Mehdi Benmoha
  • 3,694
  • 3
  • 23
  • 43
  • I also added the default app engine service account (standard) with Cloud SQL Client but in vain. – Mehdi Benmoha Oct 11 '18 at 10:02
  • Have you looked at [this article][https://cloud.google.com/appengine/docs/flexible/java/using-cloud-sql#running_sql_name_short_and_gae_name_short_in_separate_projects] explaining how to connect to Cloud SQL in a different project? – Philipp Sh Oct 11 '18 at 11:04
  • Yes I tried with the default service account and the flexible service account but both don't work. – Mehdi Benmoha Oct 11 '18 at 12:02
  • Can you provide your full pom.xml and config.properties file contents? I would recommend omitting the personal information such as passwords. – Philipp Sh Oct 12 '18 at 14:16
  • I updated the question with the pom.xml file. PS: I am not a Java developer, I am coming from the JavaScript, Cloud and devOps world and trying to deploy this project that someone else developed. There's no config.properties file. – Mehdi Benmoha Oct 12 '18 at 14:22
  • 1
    Please check if the API is enabled on the **other** project. – Vadim Oct 12 '18 at 21:49
  • @Vadim Thank you so much, it's now working correctly. I enabled the Cloud Sql Admin API on the project that doesn't contain the database and it worked ! However, I think that the log message can be enhanced, or at least, it should be specified in the documentation. – Mehdi Benmoha Oct 15 '18 at 10:15
  • @Vadim can you post your solution as an answer for the benefit of the community? – Philipp Sh Oct 15 '18 at 10:34
  • Does this answer your question? [Accessing existing cloud SQL instance from another project ID](https://stackoverflow.com/questions/23518780/accessing-existing-cloud-sql-instance-from-another-project-id) – Carles Capellas Apr 07 '21 at 06:48
  • well not really your link points to an IAM issue while this is an API activation issue – Mehdi Benmoha Apr 07 '21 at 15:39

2 Answers2

1

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.

Vadim
  • 4,996
  • 1
  • 26
  • 30
  • Both the `Cloud SQL API` and the `Cloud SQL Admin API` need to be enabled in both of the projects. – Mehdi Benmoha Oct 16 '18 at 09:43
  • I think the Cloud SQL Admin API is sufficient, I don't believe you need Cloud SQL API on the project that doesn't have Cloud SQL. – Vadim Oct 16 '18 at 17:21
0

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
seb
  • 3,646
  • 26
  • 21