|
1 | | -# Spring Data JDBC Sample Application with Cloud Spanner PostgreSQL |
| 1 | +# Spring Data JDBC |
2 | 2 |
|
3 | | -This sample application shows how to develop portable applications using Spring Data JDBC in |
4 | | -combination with Cloud Spanner PostgreSQL. This application can be configured to run on either a |
5 | | -[Cloud Spanner PostgreSQL](https://cloud.google.com/spanner/docs/postgresql-interface) database or |
6 | | -an open-source PostgreSQL database. The only change that is needed to switch between the two is |
7 | | -changing the active Spring profile that is used by the application. |
8 | | - |
9 | | -The application uses the Cloud Spanner JDBC driver to connect to Cloud Spanner PostgreSQL, and it |
10 | | -uses the PostgreSQL JDBC driver to connect to open-source PostgreSQL. Spring Data JDBC works with |
11 | | -both drivers and offers a single consistent API to the application developer, regardless of the |
12 | | -actual database or JDBC driver being used. |
13 | | - |
14 | | -This sample shows: |
15 | | - |
16 | | -1. How to use Spring Data JDBC with Cloud Spanner PostgreSQL. |
17 | | -2. How to develop a portable application that runs on both Google Cloud Spanner PostgreSQL and |
18 | | - open-source PostgreSQL with the same code base. |
19 | | -3. How to use bit-reversed sequences to automatically generate primary key values for entities. |
20 | | - |
21 | | -__NOTE__: This application does __not require PGAdapter__. Instead, it connects to Cloud Spanner |
22 | | -PostgreSQL using the Cloud Spanner JDBC driver. |
23 | | - |
24 | | -## Cloud Spanner PostgreSQL |
25 | | - |
26 | | -Cloud Spanner PostgreSQL provides language support by expressing Spanner database functionality |
27 | | -through a subset of open-source PostgreSQL language constructs, with extensions added to support |
28 | | -Spanner functionality like interleaved tables and hinting. |
29 | | - |
30 | | -The PostgreSQL interface makes the capabilities of Spanner —__fully managed, unlimited scale, strong |
31 | | -consistency, high performance, and up to 99.999% global availability__— accessible using the |
32 | | -PostgreSQL dialect. Unlike other services that manage actual PostgreSQL database instances, Spanner |
33 | | -uses PostgreSQL-compatible syntax to expose its existing scale-out capabilities. This provides |
34 | | -familiarity for developers and portability for applications, but not 100% PostgreSQL compatibility. |
35 | | -The SQL syntax that Spanner supports is semantically equivalent PostgreSQL, meaning schemas |
36 | | -and queries written against the PostgreSQL interface can be easily ported to another PostgreSQL |
37 | | -environment. |
38 | | - |
39 | | -This sample showcases this portability with an application that works on both Cloud Spanner PostgreSQL |
40 | | -and open-source PostgreSQL with the same code base. |
41 | | - |
42 | | -## Spring Data JDBC |
| 3 | +This directory contains two sample applications for using Spring Data JDBC |
| 4 | +with the Spanner JDBC driver. |
43 | 5 |
|
44 | 6 | [Spring Data JDBC](https://spring.io/projects/spring-data-jdbc) is part of the larger Spring Data |
45 | | -family. It makes it easy to implement JDBC based repositories. This module deals with enhanced |
46 | | -support for JDBC based data access layers. |
| 7 | +family. It makes it easy to implement JDBC based repositories. |
| 8 | +This module deals with enhanced support for JDBC based data access layers. |
47 | 9 |
|
48 | 10 | Spring Data JDBC aims at being conceptually easy. In order to achieve this it does NOT offer caching, |
49 | 11 | lazy loading, write behind or many other features of JPA. This makes Spring Data JDBC a simple, |
50 | 12 | limited, opinionated ORM. |
51 | 13 |
|
52 | | -## Sample Application |
53 | | - |
54 | | -This sample shows how to create a portable application using Spring Data JDBC and the Cloud Spanner |
55 | | -PostgreSQL dialect. The application works on both Cloud Spanner PostgreSQL and open-source |
56 | | -PostgreSQL. You can switch between the two by changing the active Spring profile: |
57 | | -* Profile `cs` runs the application on Cloud Spanner PostgreSQL. |
58 | | -* Profile `pg` runs the application on open-source PostgreSQL. |
59 | | - |
60 | | -The default profile is `cs`. You can change the default profile by modifying the |
61 | | -[application.properties](src/main/resources/application.properties) file. |
62 | | - |
63 | | -### Running the Application |
64 | | - |
65 | | -1. Choose the database system that you want to use by choosing a profile. The default profile is |
66 | | - `cs`, which runs the application on Cloud Spanner PostgreSQL. Modify the default profile in the |
67 | | - [application.properties](src/main/resources/application.properties) file. |
68 | | -2. Modify either [application-cs.properties](src/main/resources/application-cs.properties) or |
69 | | - [application-pg.properties](src/main/resources/application-pg.properties) to point to an existing |
70 | | - database. If you use Cloud Spanner, the database that the configuration file references must be a |
71 | | - database that uses the PostgreSQL dialect. |
72 | | -3. Run the application with `mvn spring-boot:run`. |
73 | | - |
74 | | -### Main Application Components |
75 | | - |
76 | | -The main application components are: |
77 | | -* [DatabaseSeeder.java](src/main/java/com/google/cloud/spanner/sample/DatabaseSeeder.java): This |
78 | | - class is responsible for creating the database schema and inserting some initial test data. The |
79 | | - schema is created from the [create_schema.sql](src/main/resources/create_schema.sql) file. The |
80 | | - `DatabaseSeeder` class loads this file into memory and executes it on the active database using |
81 | | - standard JDBC APIs. The class also removes Cloud Spanner-specific extensions to the PostgreSQL |
82 | | - dialect when the application runs on open-source PostgreSQL. |
83 | | -* [JdbcConfiguration.java](src/main/java/com/google/cloud/spanner/sample/JdbcConfiguration.java): |
84 | | - Spring Data JDBC by default detects the database dialect based on the JDBC driver that is used. |
85 | | - This class overrides this default and instructs Spring Data JDBC to also use the PostgreSQL |
86 | | - dialect for Cloud Spanner PostgreSQL. |
87 | | -* [AbstractEntity.java](src/main/java/com/google/cloud/spanner/sample/entities/AbstractEntity.java): |
88 | | - This is the shared base class for all entities in this sample application. It defines a number of |
89 | | - standard attributes, such as the identifier (primary key). The primary key is automatically |
90 | | - generated using a (bit-reversed) sequence. [Bit-reversed sequential values](https://cloud.google.com/spanner/docs/schema-design#bit_reverse_primary_key) |
91 | | - are considered a good choice for primary keys on Cloud Spanner. |
92 | | -* [Application.java](src/main/java/com/google/cloud/spanner/sample/Application.java): The starter |
93 | | - class of the application. It contains a command-line runner that executes a selection of queries |
94 | | - and updates on the database. |
95 | | - |
| 14 | +- [GoogleSQL](googlesql): This sample uses the Spanner GoogleSQL dialect. |
| 15 | +- [PostgreSQL](postgresql): This sample uses the Spanner PostgreSQL dialect and the Spanner JDBC |
| 16 | + driver. It does not use PGAdapter. The sample application can also be configured to run on open |
| 17 | + source PostgreSQL, and shows how a portable application be developed using this setup. |
0 commit comments