Skip to content

Latest commit

 

History

History
 
 

spring-cloud-gcp-starter-sql

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Google Cloud SQL Spring Boot Starter

Maven coordinates, using Spring Cloud GCP BOM:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-gcp-starter-sql</artifactId>
</dependency>

Gradle coordinates:

dependencies {
    compile group: 'org.springframework.cloud', name: 'spring-cloud-gcp-starter-sql', version: '1.0.0.M1'
}

This starter provides the configuration sources CloudSqlJdbcInfoProvider and DataSource.

It only allows connections to second-generation Google Cloud SQL instances.

Before using the starter, ensure that the Google Cloud SQL API is enabled in your GCP project. To do that, go to the API library page of the Google Cloud Console, search for "Cloud SQL API", click the first result and enable the API.

Note
There are several similar "Cloud SQL" results. You must access the "Google Cloud SQL API" one and enable the API from there.

Based on your environment (e.g., Google App Engine) or database type (e.g., MySQL, PostgreSQL), the starter provides a CloudSqlJdbcInfoProvider object that returns the correct values for the JDBC URL and driver class name to be used.

The starter uses Spring Boot to configure a DataSource. The SQL username and password are configured through spring.datasource.username and spring.datasource.password. spring.datasource.url and spring.datasource.driver-class-name are automatically configured by the provided CloudSqlJdbcInfoProvider, unless they are explicitly configured.

The following properties are required:

spring.cloud.gcp.sql.database-name=[YOUR_CLOUD_SQL_DATABASE_NAME]
spring.cloud.gcp.sql.instance-connection-name=[YOUR_CLOUD_SQL_INSTANCE_CONNECTION_NAME]

spring.cloud.gcp.sql.instance-connection-name is your instance’s connection name.

The following properties are optional:

spring.cloud.gcp.sql.database-type=[mysql or postgresql]
spring.cloud.gcp.sql.credentials.location=[CREDENTIALS_FILESYSTEM_PATH]

spring.cloud.gcp.sql.database-type can either be mysql or postgresql. Any value that isn’t those two will cause the configuration to error. spring.cloud.gcp.sql.database-type determines if the resolved JDBC URL and driver class are for MySQL or PostgreSQL.

spring.cloud.gcp.sql.credentials.location can be used to specify credentials for a GCP project ID different than the one configured in the Core Starter.

By using this starter in conjunction with Spring JDBC, it’s possible to inject a fully configured JdbcTemplate object only from adding the required properties.

Google Cloud SQL Instance Connection Name

A Google Cloud SQL instance connection name is a string comprised of three elements:

[YOUR_GCP_PROJECT_ID]:[YOUR_INSTANCE_REGION]:[YOUR_INSTANCE_NAME]

You can find your instance’s connection name in its Google Cloud Console page.

Troubleshooting tips

If you’re not able to connect to a database and see an endless loop of Connecting to Cloud SQL instance […​] on IP […​], it’s likely that exceptions are being thrown and logged at a level lower than your logger’s level. This may be the case with HikariCP, if your logger is set to INFO or higher level.

To see what’s going on in the background, you should add a logback.xml file to your application resources folder, that looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <include resource="org/springframework/boot/logging/logback/base.xml"/>
  <logger name="com.zaxxer.hikari.pool" level="DEBUG"/>
</configuration>

Frequently Asked Questions

PostgreSQL: java.net.SocketException: already connected

We found this exception to be common if your Maven project’s parent is spring-boot version 1.5.x, or in any other circumstance that would cause the version of the org.postgresql:postgresql dependency to be an older one (e.g., 9.4.1212.jre7).

To fix this, re-declare the dependency in its correct version. For example, in Maven:

<dependency>
  <groupId>org.postgresql</groupId>
  <artifactId>postgresql</artifactId>
  <version>42.1.1</version>
</dependency>

I’m seeing a lot of log lines similar to "c.g.cloud.sql.core.SslSocketFactory : Re-throwing cached exception due to attempt to refresh instance information too soon after error." in a loop and I can’t connect to my database

This is usually a symptom that something isn’t right with the permissions of your credentials or the Google Cloud SQL API is not enabled. Verify that the Google Cloud SQL API is enabled in the Cloud Console and that your service account has the necessary IAM roles.

To find out what’s causing the issue, you can enable DEBUG logging level by adding the following logback.xml file to your application’s resources, or only add the relevant set log level line if you already have one logback.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <include resource="org/springframework/boot/logging/logback/base.xml"/>
  <logger name="com.zaxxer.hikari.pool" level="DEBUG"/>
</configuration>