Spring Data JPA, חלק מהמשפחה הגדולה יותר של Spring Data, מקל על הטמעה של מאגרי מידע מבוססי JPA. Spring Data JPA תומך ב-PostgreSQL ובמגוון רחב של מערכות מסדי נתונים אחרות. היא מוסיפה שכבת הפשטה בין האפליקציה לבין מסד הנתונים, וכך מקלה על העברת האפליקציה ממערכת מסד נתונים אחת לאחרת.
הגדרה של Spring Data JPA למסדי נתונים של Spanner עם ניב PostgreSQL
אפשר לשלב מסדי נתונים של Spanner עם ניב PostgreSQL עם Spring Data JPA באמצעות ניב PostgreSQL Hibernate סטנדרטי ו-PGAdapter.
כדי לראות דוגמה, אפשר לעיין באפליקציה לדוגמה שעובדת ב-GitHub.
תלויות
בפרויקט, מוסיפים תלות ב-Apache Maven עבור Spring Data JPA, PostgreSQL JDBC driver ו-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>
הפעלת PGAdapter בתוך התהליך
כדי להפעיל את PGAdapter ישירות מאפליקציית Java, מוסיפים את השיטה הבאה לאפליקציה: PGAdapter פועל באותה מכונה וירטואלית של Java (JVM) כמו האפליקציה שלכם, והאפליקציה מתחברת ל-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;
}
הגדרות אישיות
מגדירים את application.properties לשימוש ב-PostgreSQL Hibernate
Dialect וב-PostgreSQL JDBC Driver. מגדירים את מנהל ההתקן של PostgreSQL JDBC כדי להתחבר למסד נתונים של ניב PostgreSQL דרך 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
אפליקציה לדוגמה מלאה
אפליקציה לדוגמה זמינה ב-GitHub.
המאמרים הבאים
- מידע נוסף על Spring Data JPA
- מידע נוסף על Hibernate ORM
- אפשר לראות את המאגר של PGAdapter ב-GitHub.
- אפשר לפתוח בקשה בקשר לבעיה ב-GitHub כדי לדווח על באג או לשאול שאלה לגבי PGAdapter.
- שילוב של Spanner עם Spring Data JPA (ניב GoogleSQL)