Skip to content
Knut Olav Løite edited this page Feb 3, 2018 · 6 revisions

JDBC Driver for Google Cloud Spanner

An open source JDBC Driver for Google Cloud Spanner, the horizontally scalable, globally consistent, relational database service from Google. The JDBC Driver that is supplied by Google is seriously limited, as it does not allow any inserts, updates or deletes, nor does it allow DDL-statements. This open source driver does allow these operations, although there are some limitations because of the underlying limitations of Cloud Spanner. This main features of this JDBC driver are:

  • Support for both DML (INSERT, UPDATE, DELETE) and DDL (CREATE TABLE, ALTER TABLE, CREATE INDEX, DROP TABLE, ...) statements
  • Support for transactions
  • DML-statements are limited to 'one row at a time'. The driver will automatically translate bulk operations into a number of single operations. I.e. the statement UPDATE table SET COL1=COL1*1.1, COL2='Test' WHERE COL3<100 will automatically be translated into a number of update statements operating on one row at a time.
  • DML-statements operating on only one row are recognized and directly translated into the corresponding operation on Cloud Spanner without any extra round trips.
  • The driver is designed to work with applications using JPA/Hibernate. See https://github.com/olavloite/spanner-hibernate for a Hibernate Dialect implementation for Google Cloud Spanner that works together with this JDBC Driver. A simple example project using Spring Boot + JPA + Hibernate + this JDBC Driver can be found here: https://github.com/olavloite/spanner-jpa-example

The driver also supports DDL-statements, although the DDL syntax of Google Cloud Spanner is quite limited in comparison to most relational databases.

Example usage:
spring.datasource.driver-class-name=nl.topicus.jdbc.CloudSpannerDriver spring.datasource.url=jdbc:cloudspanner://localhost;Project=projectId;Instance=instanceId;Database=databaseName;SimulateProductName=PostgreSQL;PvtKeyPath=key_file

The last two properties (SimulateProductName and PvtKeyPath) are optional.

You either need to

  1. Create an environment variable GOOGLE_APPLICATION_CREDENTIALS that points to a credentials file for a Google Cloud Spanner project.
  2. OR Supply the parameter PvtKeyPath that points to a file containing the credentials to use.

The server name (in the example above: localhost) is ignored by the driver, but as it is a mandatory part of a JDBC URL it needs to be specified. The property 'SimulateProductName' indicates what database name should be returned by the method DatabaseMetaData.getDatabaseProductName().