Home > Enterprise >  SpringBoot App with Hibernate deployed on App Engine failed connecting to Cloud SQL
SpringBoot App with Hibernate deployed on App Engine failed connecting to Cloud SQL

Time:11-12

I have a SpringBoot application that I have deployed on App Engine my database is MySQL 8. For my local testing I could connect to Cloud DB using public IP and connection works fine.

But when this application is deployed on App Engine I am getting Error:

2021-11-09 01:44:50 default[1]  2021-11-09 01:44:50.874  INFO 10 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.25.Final
2021-11-09 01:44:51 default[1]  2021-11-09 01:44:51.092  INFO 10 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2021-11-09 01:46:58 default[1]  2021-11-09 01:46:58.343  WARN 10 --- [           main] o.h.e.j.e.i.JdbcEnvironmentInitiator     : HHH000342: Could not obtain connection to query metadata
2021-11-09 01:46:58 default[1]
2021-11-09 01:46:58 default[1]  com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
2021-11-09 01:46:58 default[1]
2021-11-09 01:46:58 default[1]  The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
2021-11-09 01:46:58 default[1]      at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-java-8.0.26.jar!/:8.0.26]
2021-11-09 01:46:58 default[1]      at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-java-8.0.26.jar!/:8.0.26]

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

Caused by: java.net.ConnectException: Connection timed out (Connection timed out)

I have followed the steps as mentioned in document : https://cloud.google.com/sql/docs/mysql/connect-app-engine-standard

app.yaml

runtime: java11
instance_class: F2
entrypoint: java -noverify -jar strategy-0.0.1-SNAPSHOT.war

application.properties

#spring.datasource.url=jdbc:mysql://35.345.11.132:3306/fin_strat
spring.datasource.url=jdbc:mysql://localhost:3306/fin_strat
spring.datasource.username=<database-user-name>
spring.datasource.password=<database-password>
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

I am not sure what am I missing out while connecting from App Engine. Please assist.

CodePudding user response:

When you use the Cloud SQL feature in APp ENgine (or Cloud Functions or Cloud Run) to connect your Cloud SQL database, the service open a socket to connect the database. You don't have to use the public IP but the opened soket.

It's similar to a Cloud SQL Proxy in socket mode. You have documentation with a Java example here

  • Related