This sample application shows how to use Spring Data JDBC with Spanner GoogleSQL.
This sample shows:
- How to use Spring Data JDBC with Spanner GoogleSQL.
- How to use bit-reversed identity columns to automatically generate primary key values for entities.
- How to set the transaction isolation level that is used by the Spanner JDBC driver.
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.
The application by default runs on the Spanner Emulator.
- Modify the application.properties to point to an existing database. The database must use the GoogleSQL dialect.
- Run the application with
mvn spring-boot:run.
The main application components are:
- 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 file. The
DatabaseSeederclass loads this file into memory and executes it on the active database using standard JDBC APIs. - SpannerDialectProvider: 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:
Spring Data JDBC requires a dialect for the database, so it knows which features are supported,
and how to build clauses like
LIMITandFOR UPDATE. This class provides this information. It is based on the built-inAnsiDialectin Spring Data JDBC. - JdbcConfiguration.java:
This configuration file serves two purposes:
- Make sure
OpenTelemetryis initialized before any data sources. - Add a converter for
LocalDateproperties. Spring Data JDBC by default map these toTIMESTAMPcolumns, but a better fit in Spanner isDATE.
- Make sure
- 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 are considered a good choice for primary keys on Cloud Spanner.
- 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.