0

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
Phil Haigh
  • 4,522
  • 1
  • 25
  • 29
  • 1
    Would you mind sharing your contents of application.properties? I am not able to reproduce your issue, and I noticed that you seem to also set spring.datasource.url in the application.properties. That should not be necessary if you are setting the environment variable SPANNER_EMULATOR_HOST. – Knut Olav Løite Sep 23 '20 at 16:45
  • 1
    Also, spring-data-jpa-sample-project uses spring-cloud-gcp-data-spanner, and not the JDBC driver to connect to Spanner. So there should be no connection pool at all here. Could it be that you have added one or more additional dependencies to your project? – Knut Olav Løite Sep 23 '20 at 17:03
  • Apologies, I linked to the wrong sample project. Fixed the link now and also added the contents of application.properties – Phil Haigh Sep 24 '20 at 07:46
  • @KnutOlavLoite this repository - https://github.com/surfsoft/spring-boot-spanner-demo - is a minimalist Spring Boot app with JPA/Hibernate/JDBC that demonstrates this issue perfectly when run against the emulator. – Phil Haigh Sep 24 '20 at 12:21

1 Answers1

1

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.

Phil Haigh
  • 4,522
  • 1
  • 25
  • 29
  • 1
    That's good to know. I'll still look into the error message, because that one is quite cryptic for what basically seems to be 'Database not found'. – Knut Olav Løite Sep 24 '20 at 18:21