将 Google Cloud Spanner 与 Spring Data JPA 集成

问题描述 投票:0回答:1

我正在将 springboot 应用程序从云 SQL (PostgresSQL) 迁移到 Cloud Spanner,并将使用 Google SQL 方言进行 Spanner 交互。在此迁移过程中,我遇到以下错误。

Caused by: java.lang.ExceptionInInitializerError: null
    at com.google.cloud.spanner.connection.ConnectionImpl.<clinit>(ConnectionImpl.java:135)
    at com.google.cloud.spanner.connection.ConnectionOptions.getConnection(ConnectionOptions.java:1073)
    at com.google.cloud.spanner.jdbc.AbstractJdbcConnection.<init>(AbstractJdbcConnection.java:65)
    at com.google.cloud.spanner.jdbc.JdbcConnection.<init>(JdbcConnection.java:56)
    at com.google.cloud.spanner.jdbc.JdbcDriver.connect(JdbcDriver.java:196)
    at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:121)
    at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:359)
    at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:201)
    at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:470)
    at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:561)
    at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:100)
    at com.zaxxer.hikari.HikariDataSource.<init>(HikariDataSource.java:81)
    at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:62)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:502)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:486)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:208)
    ... 37 common frames omitted
Caused by: com.google.cloud.spanner.SpannerException: INTERNAL: Could not instantiate statement parser for dialect GOOGLE_STANDARD_SQL
    at com.google.cloud.spanner.SpannerExceptionFactory.newSpannerExceptionPreformatted(SpannerExceptionFactory.java:319)
    at com.google.cloud.spanner.SpannerExceptionFactory.newSpannerExceptionPreformatted(SpannerExceptionFactory.java:325)
    at com.google.cloud.spanner.SpannerExceptionFactory.newSpannerException(SpannerExceptionFactory.java:62)
    at com.google.cloud.spanner.connection.AbstractStatementParser.getInstance(AbstractStatementParser.java:93)
    at com.google.cloud.spanner.connection.AbstractStatementParser.<clinit>(AbstractStatementParser.java:145)
    ... 55 common frames omitted
Caused by: java.lang.reflect.InvocationTargetException: null
    at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:74)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:502)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:486)
    at com.google.cloud.spanner.connection.AbstractStatementParser.getInstance(AbstractStatementParser.java:91)
    ... 56 common frames omitted
Caused by: com.google.cloud.spanner.connection.ClientSideStatementImpl$CompileException: Could not compile statement SHOW VARIABLE AUTOCOMMIT
    at com.google.cloud.spanner.connection.ClientSideStatementImpl.compile(ClientSideStatementImpl.java:159)
    at com.google.cloud.spanner.connection.ClientSideStatements.getCompiledStatements(ClientSideStatements.java:76)
    at com.google.cloud.spanner.connection.SpannerStatementParser.<init>(SpannerStatementParser.java:41)
    at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:62)
    ... 59 common frames omitted
Caused by: java.lang.reflect.InvocationTargetException: null
    at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:74)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:502)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:486)
    at com.google.cloud.spanner.connection.ClientSideStatementImpl.compile(ClientSideStatementImpl.java:156)
    ... 62 common frames omitted
Caused by: java.lang.NoClassDefFoundError: com/google/spanner/v1/DirectedReadOptions
    at java.base/java.lang.Class.getDeclaredMethods0(Native Method)
    at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3578)
    at java.base/java.lang.Class.getDeclaredMethod(Class.java:2846)
    at com.google.cloud.spanner.connection.ClientSideStatementNoParamExecutor.<init>(ClientSideStatementNoParamExecutor.java:39)
    at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:62)
    ... 65 common frames omitted
Caused by: java.lang.ClassNotFoundException: com.google.spanner.v1.DirectedReadOptions
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526)
    ... 70 common frames omitted

我的应用程序使用 Spring 3、Java 17 以及最新的 hibernate 和 JPA 库。下面为扳手添加了依赖项。我能够成功运行具有相同依赖项的简单应用程序。

<dependency>
            <groupId>com.google.cloud</groupId>
            <artifactId>google-cloud-spanner-hibernate-dialect</artifactId>
            <version>3.3.0</version>
        </dependency>
        <dependency>
            <groupId>com.google.cloud</groupId>
            <artifactId>google-cloud-spanner</artifactId>
            <version>6.80.1</version>
        </dependency>
        <dependency>
            <groupId>com.google.cloud</groupId>
            <artifactId>spring-cloud-gcp-data-spanner</artifactId>
            <version>5.8.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
            <version>3.3.5</version>
        </dependency>
        <dependency>
        <groupId>com.google.cloud</groupId>
        <artifactId>google-cloud-spanner-jdbc</artifactId>
        <version>2.8.0</version>
    </dependency>
spring-boot google-cloud-platform spring-data-jpa google-cloud-spanner google-cloud-spanner-emulator
1个回答
0
投票

该问题可能是由于您的项目中的依赖冲突引起的。您似乎同时添加了

google-cloud-spanner
google-cloud-spanner-jdbc
。然而,
google-cloud-spanner
google-cloud-spanner-jdbc
的传递依赖,所以这实际上并不是必要的。

此外,您添加的 Spanner JDBC 驱动程序版本 (2.8.0) 已有 2 年历史,而

google-cloud-spanner
的版本是几周前的。这两者的结合可能会导致您的问题。

我建议您以此示例应用程序为起点,特别是对于依赖项,并尝试从那里开始工作:https://github.com/GoogleCloudPlatform/google-cloud-spanner-hibernate/tree/master/ google-cloud-spanner-hibernate-samples/spring-data-jpa-full-sample

或者,尝试将您的依赖关系简化为以下内容:

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>${spring-boot-version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <!-- Hibernate Dialect and JDBC Driver dependencies-->
    <dependency>
      <groupId>com.google.cloud</groupId>
      <artifactId>google-cloud-spanner-hibernate-dialect</artifactId>
      <version>3.7.1</version>
    </dependency>
    <dependency>
      <groupId>com.google.cloud</groupId>
      <artifactId>google-cloud-spanner-jdbc</artifactId>
      <version>2.24.1</version>
    </dependency>
  </dependencies>
© www.soinside.com 2019 - 2024. All rights reserved.