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

huangapple go评论77阅读模式
英文:

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

问题

我无法使 Google 的 [spring-data-jpa-sample 项目](https://github.com/GoogleCloudPlatform/google-cloud-spanner-hibernate/tree/master/google-cloud-spanner-hibernate-samples/spring-data-jpa-sample) 在 Spanner 模拟器上运行起来。我在 MacOS Mojave 上使用了预安装的 JDK 11(AdoptOpenJDK 构建 11.0.3+7)。

我已经针对我的 GCP 帐户进行了身份验证,并按照[这里](https://cloud.google.com/spanner/docs/emulator)的文档进行了模拟器设置,按照说明启动了模拟器,成功地创建了一个实例,并将 `SPANNER_EMULATOR_HOST` 环境变量设置为 `localhost:9010`。

当 Spring 应用程序运行时,它失败并显示以下错误:

java.sql.SQLTransientConnectionException: HikariPool-1 - 经过 30088 毫秒,连接不可用,请求超时。


在此之前有两个警告。HHH000342: 无法获取连接以查询元数据)和 SQL 错误: 0,SQLState: null。

如果我将 spring.datasource.url 更新为在末尾添加 `usePlainText=true`,就像我在[这里](https://stackoverflow.com/questions/62971749)看到的建议解决方法一样,结果是相同的。

应用程序的代码和配置与 [spring-data-jpa-sample 项目](https://github.com/GoogleCloudPlatform/google-cloud-spanner-hibernate/tree/master/google-cloud-spanner-hibernate-samples/spring-data-jpa-sample) 中显示的完全相同,这就是为什么我没有在这里发布它的原因,除了在 application.properties 中的 `spring.datasource.url` 的值(请参见下文)。要复制此问题,请按照[这里](https://cloud.google.com/spanner/docs/emulator)的模拟器设置说明,克隆存储库,设置项目、实例和数据库变量,然后运行代码示例。

这是 `application.properties` 文件的内容:

使用 Spring Data JPA 配置应用程序以使用 Cloud Spanner

Spanner 连接 URL。

- ${PROJECT_ID} 替换为您的 GCP 项目 ID

- ${INSTANCE_ID} 替换为您的 Spanner 实例 ID

- ${DATABASE_NAME} 替换为您在 Spanner 实例中的 Spanner 数据库名称

spring.datasource.url=jdbc:cloudspanner:/projects/test-project/instances/test-instance/databases/spring-demo?usePlainText=true

指定 Spanner JDBC 驱动程序。

spring.datasource.driver-class-name=com.google.cloud.spanner.jdbc.JdbcDriver

指定 Spanner Hibernate 方言。

spring.jpa.properties.hibernate.dialect=com.google.cloud.spanner.hibernate.SpannerDialect

spring.jpa.hibernate.ddl-auto=update

用于启用批处理语句以提高效率的设置

spring.jpa.properties.hibernate.jdbc.batch_size=100

如果需要,您可以显示 SQL 语句和统计信息进行调试。

spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.format_sql=true

英文:

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

答案1

得分: 1

尽管进行了仔细检查,但我们错过了设置数据库的重要步骤:

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

现在一切都正常。

英文:

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.

huangapple
  • 本文由 发表于 2020年9月23日 23:37:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/64031492.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定