javagoogle-cloud-spannerspring-boot-jpagoogle-cloud-spanner-emulator

Why does the Spanner spring demo running against the Spanner emulator fail to create pooled database connections?


I'm unable to get Google's spring-data-jpa-sample project running against the Spanner emulator. I'm using a pre-installed JDK 11 (AdoptOpenJDK build 11.0.3+7) on MacOS Mojave.

I've authenticated against my GCP account and gone through the emulator set-up as documented here, following the instructions through to starting up the emulator, creating an instance successfully, and have set the SPANNER_EMULATOR_HOST environment variable to localhost:9010.

When the Spring application runs, it fails with the following error:

java.sql.SQLTransientConnectionException: HikariPool-1 - Connection is not available, request timed out after 30088ms.

There are two warnings prior to this. HHH000342: Could not obtain connection to query metadata) and SQL Error: 0, SQLState: null.

If I update spring.datasource.url to add usePlainText=true on the end, which I've seen as a suggested solution here, the result is the same.

Application code and configuration is exactly as shown in the spring-data-jpa-sample project which is why I've not posted it here - except for the value of spring.datasource.url in application.properties (see below). To replicate this issue, follow the emulator set-up instructions here, clone the repository, set the project, instance and database variables and run the code sample.

Here's the contents of the application.properties file:

# Application configuration to use Cloud Spanner with Spring Data JPA

# Spanner connection URL.
# - ${PROJECT_ID} Replace with your GCP project ID
# - ${INSTANCE_ID} Replace with your Spanner instance ID
# - ${DATABASE_NAME} Replace with your Spanner database name within your Spanner instance
spring.datasource.url=jdbc:cloudspanner:/projects/test-project/instances/test-instance/databases/spring-demo?usePlainText=true

# Specify the Spanner JDBC driver.
spring.datasource.driver-class-name=com.google.cloud.spanner.jdbc.JdbcDriver

# Specify the Spanner Hibernate dialect.
spring.jpa.properties.hibernate.dialect=com.google.cloud.spanner.hibernate.SpannerDialect

spring.jpa.hibernate.ddl-auto=update

# Settings to enable batching statements for efficiency
spring.jpa.properties.hibernate.jdbc.batch_size=100

# You may display SQL statements and stats for debugging if needed.
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.format_sql=true

Solution

  • Despite careful checking we'd missed an important step setting up the database:

    gcloud spanner databases create spring-demo --instance=test-instance 
    

    Everything's good now.