0

We're breaking some functionality out of our app engine project and moving it to google cloud functions (GCF).

For the life of me, I can't get a db connection from the GCF. The exact same code is used in both the app engine app and GCF.

The db connection is initialized like this and this works in app engine:

String dbconn = "jdbc:mysql:///" + MYDBNAME + 
    "?cloudSqlInstance=" + PROJECTID + ":us-central1:" + INSTANCEID +
    "&socketFactory=com.google.cloud.sql.mysql.SocketFactory;

String uid = "xxx";
String pwd = "xxx";

config.setJdbcUrl( dbconn );
config.setUsername( uid );
config.setPassword( pwd );

ConnectionPool pool = new HikariDataSource( config );

This works perfectly in app engine.

However, the GCF shows this in the logs.

Failed to get driver instance for jdbcUrl=jdbc:mysql:///MYDBNAME?cloudSqlInstance=PROJECTID:us-central1:INSTANCEID&socketFactory=com.google.cloud.sql.mysql.SocketFactory

I've also configured a SQL connection in the GCF cloud console.

enter image description here

And the pom.xml file contains the sql classes.

<dependency>
  <groupId>com.google.cloud.sql</groupId>
  <artifactId>mysql-socket-factory-connector-j-8</artifactId>
  <version>1.13.1</version>
  <scope>runtime</scope>
</dependency>
<dependency>
  <groupId>com.mysql</groupId>
  <artifactId>mysql-connector-j</artifactId>
  <version>8.0.33</version>
  <scope>runtime</scope>
</dependency>
1
  • 1
    Make sure you're configuring your JDBC URL correctly. Have a look at this Link for the correct URL. Are you following this Documentation for connecting MySQL DB from Cloud Run Functions. Also confirm the Cloud SQL instances having public IP or private IP address? Make sure the google-cloud-sql-jdbc-socket-factory library is installed. Commented Sep 17, 2024 at 8:33

1 Answer 1

0

The problem was that we were not loading the JDBC driver. We were attempting to load this class, which we thought was the JDBC driver.

Class.forName( "com.google.cloud.sql.mysql.SocketFactory" );

When we added this instead (after several days of trial and error), it worked.

Class.forName( "com.mysql.cj.jdbc.Driver" );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.