Intégrer Spanner à Spring Data JPA (dialecte PostgreSQL)

Spring Data JPA, qui fait partie de la famille étendue Spring Data, facilite la implémenter des dépôts basés sur JPA. Spring Data JPA est compatible avec PostgreSQL et un large éventail d'autres systèmes de base de données. Il ajoute une couche d'abstraction entre votre application et votre base de données, ce qui facilite le portage de votre application d'un système de base de données à un autre.

Configurer Spring Data JPA pour les bases de données de dialecte PostgreSQL dans Spanner

Vous pouvez intégrer les bases de données de dialecte PostgreSQL de Spanner à Spring Data JPA. en utilisant le dialecte hibernate PostgreSQL standard et PGAdapter :

Pour voir un exemple, reportez-vous aux application exemple fonctionnelle sur GitHub.

Dépendances

Dans votre projet, ajoutez des dépendances Apache Maven pour Spring Data JPA. le pilote JDBC PostgreSQL et PGAdapter.

  <dependencies>
    <!-- Spring Data JPA -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <!-- Add the PostgreSQL JDBC driver -->
    <dependency>
      <groupId>org.postgresql</groupId>
      <artifactId>postgresql</artifactId>
    </dependency>
    <!-- Add PGAdapter as a dependency, so we can start it in-process -->
    <dependency>
      <groupId>com.google.cloud</groupId>
      <artifactId>google-cloud-spanner-pgadapter</artifactId>
    </dependency>
  </dependencies>

Démarrer PGAdapter en cours

Ajoutez la méthode suivante à votre application pour démarrer PGAdapter directement depuis votre application Java. PGAdapter s'exécute dans la même JVM que votre application, qui se connecte à PGAdapter localhost:port

  /** Starts PGAdapter in-process and returns a reference to the server. */
  static ProxyServer startPGAdapter() {
    // Start PGAdapter using the default credentials of the runtime environment on port 9432.
    OptionsMetadata options = OptionsMetadata.newBuilder().setPort(9432).build();
    ProxyServer server = new ProxyServer(options);
    server.startServer();
    server.awaitRunning();

    return server;
  }

Configuration

Configurer application.properties pour utiliser PostgreSQL Hibernate Dialecte et pilote JDBC PostgreSQL. Configurer PostgreSQL Pilote JDBC pour se connecter à une base de données de dialecte PostgreSQL via PGAdapter.

# The example uses the standard PostgreSQL Hibernate dialect.
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect

# Defining these properties here makes it a bit easier to build the connection string.
# Change these to match your Cloud Spanner PostgreSQL-dialect database.
spanner.project=my-project
spanner.instance=my-instance
spanner.database=my-database
# This setting ensures that PGAdapter automatically commits the current transaction if it encounters
# a DDL statement in a read/write transaction, and then executes the DDL statements as a single DDL
# batch. 
spanner.ddl_transaction_mode=options=-c%20spanner.ddl_transaction_mode=AutocommitExplicitTransaction

# This is the connection string to PGAdapter running in-process.
spring.datasource.url=jdbc:postgresql://localhost:9432/projects%2F${spanner.project}%2Finstances%2F${spanner.instance}%2Fdatabases%2F${spanner.database}?${spanner.ddl_transaction_mode}

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

# Enable JDBC batching.
spring.jpa.properties.hibernate.jdbc.batch_size=100
spring.jpa.properties.hibernate.order_inserts=true

Exemple d'application complet

Un exemple d'application fonctionnel disponible sur GitHub.

Étape suivante