This application uses the Google Cloud SQL JDBC Socket Factory and is compatible with Native Image compilation.
The application runs some simple Cloud SQL queries to demonstrate compatibility.
-
Follow the GCP Project and Native Image Setup Instructions.
-
Cloud SQL database setup:
-
Follow these instructions to create a Cloud SQL instance. Choose MySQL as the database provider because this sample is designed for Cloud SQL with MySQL.
-
Navigate to your Cloud SQL instance from the instances view page and find the instance connection name.
It should be of the form:
{PROJECT_ID}:{REGION}:{SQL_INSTANCE_NAME}
This string will be used to connect your instance.
-
Follow these instructions to create a database. For this example, name the database
test_db
.
-
-
Navigate to this directory and compile the application with the Native Image compiler.
mvn package -P native
-
Run the application. Set the
-Dinstance
property to the instance connection name referenced above../target/cloud-sql-sample -Dinstance=<INSTANCE_CONNECTION_NAME>
Additional Properties
By default the application connects with the
root
user and an empty password to thetest_db
database. You may override the settings by passing additional arguments with the-D
prefix to the executable:Property Description -Dusername
Set the username to use. (Defaults to root) -Dpassword
Set the password to use. (Defaults to "") -Dinstance
Specify the instance connection string. -DunixSocketPath
Specify the unix domain socket path. (Defaults to null) -Ddatabase
Specify the database to connect to. -
The application will run through some basic operations to create a table and read records from it.
[main] INFO com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Start completed. Jun 04, 2021 2:28:13 PM com.google.cloud.sql.core.CoreSocketFactory connect INFO: Connecting to Cloud SQL instance [<your-instance-here>] via SSL socket. Books in database: a965870d-c148-4159-9280-81ea507af1f8, The Book
The Cloud SQL connector library uses the CP1252 charset when logging error messages from the server.
Therefore, you will need to pass -H:+AddAllCharsets
to the Native Image compiler.
This is specified in the buildArgs
section of the native-maven-plugin
in the pom.xml of this sample.