This sample application demonstrates using Hibernate with Google Cloud Spanner.
It provides examples of how to access the Spanner JDBC Driver to perform stale reads, read-only transactions, and other Spanner-specific operations from Hibernate.
See this Cloud Spanner Spring Data JPA with Hibernate Sample for a sample application that shows all available Spanner features that can be used with Hibernate.
-
Create a Google Cloud Platform Project
-
Create a service account with Cloud Spanner permission. Furnish a new JSON key and then set the credentials using the
GOOGLE_APPLICATION_CREDENTIALS
environment variable.Alternatively, have the Google Cloud SDK installed and initialized and logged in with application default credentials.
-
Enable the Cloud Spanner API.
-
Create a Cloud Spanner instance and database in your project and save those details for the next step.
First construct the JDBC URL string based on the Spanner instance and database you created.
Replace the PROJECT_ID
, INSTANCE_ID
, and DATABASE_ID
parameters in the command to specify the Spanner database you will use for the samples.
jdbc:cloudspanner:/projects/{YOUR_PROJECT_ID}/instances/{YOUR_INSTANCE_ID}/databases/{YOUR_DATABASE_ID}
Stale reads are a feature in Cloud Spanner which allows you to read records from the database at a previous point in time.
The StaleReadsDemo.java
provides a demonstration of how to perform a Stale read in Hibernate.
The sample creates a new row in the database which is successfully read by a strong-read, but is not present in the stale read.
To run the sample on command line:
mvn exec:java -Dexec.mainClass="com.example.StaleReadsDemo" -Dhibernate.connection.url="jdbc:cloudspanner:/projects/{YOUR_PROJECT_ID}/instances/{YOUR_INSTANCE_ID}/databases/{YOUR_DATABASE_ID}"
Spanner offers read-only transactions which can offer performance benefits for read-only queries.
The TransactionTypeDemo.java
offers an example of how to perform a read-only transaction.
To run the sample on command line:
mvn exec:java -Dexec.mainClass="com.example.TransactionTypeDemo" -Dhibernate.connection.url="jdbc:cloudspanner:/projects/{YOUR_PROJECT_ID}/instances/{YOUR_INSTANCE_ID}/databases/{YOUR_DATABASE_ID}"
Many advanced features of Cloud Spanner can be accessed through the JDBC Driver by unwrapping the underlying JDBC connection from the session:
session.doWork(conn -> {
conn.setReadOnly(false);
conn.createStatement().execute("SET READ_ONLY_STALENESS = 'STRONG'");
});
Note
|
It is important to remember that JDBC Connections are pooled and shared among Hibernate sessions. If you modify a JDBC connection in one session (such as setting it to read-only), that change will affect the next session using the connection. Therefore, it is recommended to wrap session creation in a helper class (such as SessionHelper.java in this sample) to help manage JDBC driver settings. You can reset a
JDBC connection to its original state with the SQL statement RESET ALL .
|
To see the full list of JDBC features you can access, please consult the Cloud Spanner JDBC documentation.