How to connect to Google Cloud SQL (MySQL) with jasync-sql?

97 Views Asked by At

I'm having a spring application with maven. I want to communicate with a cloud SQL MySQL database I have in GCP asynchronously.

I tried following the getting started code and wrote this:

    Connection connection = MySQLConnectionBuilder
      .createConnectionPool(
       "jdbc:mysql://google/$DB_NAME
  ?socketFactory=com.google.cloud.sql.mysql.SocketFactory
  &cloudSqlInstance=$PROJECT_ID:$PROJECT_LOCATION:$INSTANCE_NAME"
      );

    CompletableFuture<QueryResult> future = connection.sendPreparedStatement("select * from TABLE_NAME");
    return future;

However,I keep getting this error:

io.netty.channel.ConnectTimeoutException: connection timed out

Can someone please help me out here?

1

There are 1 best solutions below

0
Diksha On

I figured out the solution to this issue. Posting it so it might be useful to someone in future.

The reason why it wasn't connecting to the cloudSQL instance is because I was trying to connect to the cloudSQL instance directly. Using a cloud SQL proxy solved the issue for me.