Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .github/workflows/spring-data-jdbc-sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ jobs:
with:
distribution: temurin
java-version: 17
- name: Run tests
- name: Run tests on GoogleSQL
run: mvn test
working-directory: samples/spring-data-jdbc
working-directory: samples/spring-data-jdbc/googlesql
- name: Run tests on PostgreSQL
run: mvn test
working-directory: samples/spring-data-jdbc/postgresql
96 changes: 9 additions & 87 deletions samples/spring-data-jdbc/README.md
Original file line number Diff line number Diff line change
@@ -1,95 +1,17 @@
# Spring Data JDBC Sample Application with Cloud Spanner PostgreSQL
# Spring Data JDBC

This sample application shows how to develop portable applications using Spring Data JDBC in
combination with Cloud Spanner PostgreSQL. This application can be configured to run on either a
[Cloud Spanner PostgreSQL](https://cloud.google.com/spanner/docs/postgresql-interface) database or
an open-source PostgreSQL database. The only change that is needed to switch between the two is
changing the active Spring profile that is used by the application.

The application uses the Cloud Spanner JDBC driver to connect to Cloud Spanner PostgreSQL, and it
uses the PostgreSQL JDBC driver to connect to open-source PostgreSQL. Spring Data JDBC works with
both drivers and offers a single consistent API to the application developer, regardless of the
actual database or JDBC driver being used.

This sample shows:

1. How to use Spring Data JDBC with Cloud Spanner PostgreSQL.
2. How to develop a portable application that runs on both Google Cloud Spanner PostgreSQL and
open-source PostgreSQL with the same code base.
3. How to use bit-reversed sequences to automatically generate primary key values for entities.

__NOTE__: This application does __not require PGAdapter__. Instead, it connects to Cloud Spanner
PostgreSQL using the Cloud Spanner JDBC driver.

## Cloud Spanner PostgreSQL

Cloud Spanner PostgreSQL provides language support by expressing Spanner database functionality
through a subset of open-source PostgreSQL language constructs, with extensions added to support
Spanner functionality like interleaved tables and hinting.

The PostgreSQL interface makes the capabilities of Spanner —__fully managed, unlimited scale, strong
consistency, high performance, and up to 99.999% global availability__— accessible using the
PostgreSQL dialect. Unlike other services that manage actual PostgreSQL database instances, Spanner
uses PostgreSQL-compatible syntax to expose its existing scale-out capabilities. This provides
familiarity for developers and portability for applications, but not 100% PostgreSQL compatibility.
The SQL syntax that Spanner supports is semantically equivalent PostgreSQL, meaning schemas
and queries written against the PostgreSQL interface can be easily ported to another PostgreSQL
environment.

This sample showcases this portability with an application that works on both Cloud Spanner PostgreSQL
and open-source PostgreSQL with the same code base.

## Spring Data JDBC
This directory contains two sample applications for using Spring Data JDBC
with the Spanner JDBC driver.

[Spring Data JDBC](https://spring.io/projects/spring-data-jdbc) is part of the larger Spring Data
family. It makes it easy to implement JDBC based repositories. This module deals with enhanced
support for JDBC based data access layers.
family. It makes it easy to implement JDBC based repositories.
This module deals with enhanced support for JDBC based data access layers.

Spring Data JDBC aims at being conceptually easy. In order to achieve this it does NOT offer caching,
lazy loading, write behind or many other features of JPA. This makes Spring Data JDBC a simple,
limited, opinionated ORM.

## Sample Application

This sample shows how to create a portable application using Spring Data JDBC and the Cloud Spanner
PostgreSQL dialect. The application works on both Cloud Spanner PostgreSQL and open-source
PostgreSQL. You can switch between the two by changing the active Spring profile:
* Profile `cs` runs the application on Cloud Spanner PostgreSQL.
* Profile `pg` runs the application on open-source PostgreSQL.

The default profile is `cs`. You can change the default profile by modifying the
[application.properties](src/main/resources/application.properties) file.

### Running the Application

1. Choose the database system that you want to use by choosing a profile. The default profile is
`cs`, which runs the application on Cloud Spanner PostgreSQL. Modify the default profile in the
[application.properties](src/main/resources/application.properties) file.
2. Modify either [application-cs.properties](src/main/resources/application-cs.properties) or
[application-pg.properties](src/main/resources/application-pg.properties) to point to an existing
database. If you use Cloud Spanner, the database that the configuration file references must be a
database that uses the PostgreSQL dialect.
3. Run the application with `mvn spring-boot:run`.

### Main Application Components

The main application components are:
* [DatabaseSeeder.java](src/main/java/com/google/cloud/spanner/sample/DatabaseSeeder.java): This
class is responsible for creating the database schema and inserting some initial test data. The
schema is created from the [create_schema.sql](src/main/resources/create_schema.sql) file. The
`DatabaseSeeder` class loads this file into memory and executes it on the active database using
standard JDBC APIs. The class also removes Cloud Spanner-specific extensions to the PostgreSQL
dialect when the application runs on open-source PostgreSQL.
* [JdbcConfiguration.java](src/main/java/com/google/cloud/spanner/sample/JdbcConfiguration.java):
Spring Data JDBC by default detects the database dialect based on the JDBC driver that is used.
This class overrides this default and instructs Spring Data JDBC to also use the PostgreSQL
dialect for Cloud Spanner PostgreSQL.
* [AbstractEntity.java](src/main/java/com/google/cloud/spanner/sample/entities/AbstractEntity.java):
This is the shared base class for all entities in this sample application. It defines a number of
standard attributes, such as the identifier (primary key). The primary key is automatically
generated using a (bit-reversed) sequence. [Bit-reversed sequential values](https://cloud.google.com/spanner/docs/schema-design#bit_reverse_primary_key)
are considered a good choice for primary keys on Cloud Spanner.
* [Application.java](src/main/java/com/google/cloud/spanner/sample/Application.java): The starter
class of the application. It contains a command-line runner that executes a selection of queries
and updates on the database.

- [GoogleSQL](googlesql): This sample uses the Spanner GoogleSQL dialect.
- [PostgreSQL](postgresql): This sample uses the Spanner PostgreSQL dialect and the Spanner JDBC
driver. It does not use PGAdapter. The sample application can also be configured to run on open
source PostgreSQL, and shows how a portable application be developed using this setup.
57 changes: 57 additions & 0 deletions samples/spring-data-jdbc/googlesql/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Spring Data JDBC Sample Application with Spanner GoogleSQL

This sample application shows how to use Spring Data JDBC with Spanner GoogleSQL.

This sample shows:

1. How to use Spring Data JDBC with Spanner GoogleSQL.
2. How to use bit-reversed identity columns to automatically generate primary key values for entities.
3. How to set the transaction isolation level that is used by the Spanner JDBC driver.

## Spring Data JDBC

[Spring Data JDBC](https://spring.io/projects/spring-data-jdbc) is part of the larger Spring Data
family. It makes it easy to implement JDBC based repositories. This module deals with enhanced
support for JDBC based data access layers.

Spring Data JDBC aims at being conceptually easy. In order to achieve this it does NOT offer caching,
lazy loading, write behind or many other features of JPA. This makes Spring Data JDBC a simple,
limited, opinionated ORM.

### Running the Application

The application by default runs on the Spanner Emulator.

1. Modify the [application.properties](src/main/resources/application.properties) to point to an existing
database. The database must use the GoogleSQL dialect.
2. Run the application with `mvn spring-boot:run`.

### Main Application Components

The main application components are:
* [DatabaseSeeder.java](src/main/java/com/google/cloud/spanner/sample/DatabaseSeeder.java): This
class is responsible for creating the database schema and inserting some initial test data. The
schema is created from the [create_schema.sql](src/main/resources/create_schema.sql) file. The
`DatabaseSeeder` class loads this file into memory and executes it on the active database using
standard JDBC APIs.
* [SpannerDialectProvider](src/main/java/com/google/cloud/spanner/sample/SpannerDialectProvider.java):
Spring Data JDBC by default detects the database dialect based on the JDBC driver that is used.
Spanner GoogleSQL is not automatically recognized by Spring Data, so we add a dialect provider
for Spanner.
* [SpannerDialect](src/main/java/com/google/cloud/spanner/sample/SpannerDialect.java):
Spring Data JDBC requires a dialect for the database, so it knows which features are supported,
and how to build clauses like `LIMIT` and `FOR UPDATE`. This class provides this information. It
is based on the built-in `AnsiDialect` in Spring Data JDBC.
* [JdbcConfiguration.java](src/main/java/com/google/cloud/spanner/sample/JdbcConfiguration.java):
This configuration file serves two purposes:
1. Make sure `OpenTelemetry` is initialized before any data sources.
2. Add a converter for `LocalDate` properties. Spring Data JDBC by default map these to `TIMESTAMP`
columns, but a better fit in Spanner is `DATE`.
* [AbstractEntity.java](src/main/java/com/google/cloud/spanner/sample/entities/AbstractEntity.java):
This is the shared base class for all entities in this sample application. It defines a number of
standard attributes, such as the identifier (primary key). The primary key is automatically
generated using a (bit-reversed) sequence. [Bit-reversed sequential values](https://cloud.google.com/spanner/docs/schema-design#bit_reverse_primary_key)
are considered a good choice for primary keys on Cloud Spanner.
* [Application.java](src/main/java/com/google/cloud/spanner/sample/Application.java): The starter
class of the application. It contains a command-line runner that executes a selection of queries
and updates on the database.
138 changes: 138 additions & 0 deletions samples/spring-data-jdbc/googlesql/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>cloud-spanner-spring-data-jdbc-googlesql-example</artifactId>
<version>1.0-SNAPSHOT</version>
<description>
Sample application showing how to use Spring Data JDBC with Cloud Spanner GoogleSQL.
</description>

<properties>
<java.version>17</java.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-bom</artifactId>
<version>2024.1.5</version>
<scope>import</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-spanner-bom</artifactId>
<version>6.91.1</version>
<scope>import</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>libraries-bom</artifactId>
<version>26.59.0</version>
<scope>import</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-bom</artifactId>
<version>1.49.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
<version>3.4.5</version>
</dependency>

<!-- Add the Spanner JDBC driver. -->
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-spanner-jdbc</artifactId>
<exclusions>
<exclusion>
<groupId>com.google.api.grpc</groupId>
<artifactId>proto-google-cloud-spanner-executor-v1</artifactId>
</exclusion>
</exclusions>
</dependency>

<!-- Add OpenTelemetry for tracing and metrics. -->
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk</artifactId>
</dependency>
<dependency>
<groupId>com.google.cloud.opentelemetry</groupId>
<artifactId>exporter-trace</artifactId>
<version>0.34.0</version>
</dependency>
<dependency>
<groupId>com.google.cloud.opentelemetry</groupId>
<artifactId>exporter-metrics</artifactId>
<version>0.34.0</version>
</dependency>

<!-- Add testcontainers for running the Spanner emulator -->
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.21.0</version>
</dependency>

<dependency>
<groupId>com.google.collections</groupId>
<artifactId>google-collections</artifactId>
<version>1.0</version>
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-spanner</artifactId>
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.api</groupId>
<artifactId>gax-grpc</artifactId>
<classifier>testlib</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>com.spotify.fmt</groupId>
<artifactId>fmt-maven-plugin</artifactId>
<version>2.25</version>
<executions>
<execution>
<goals>
<goal>format</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Loading