Skip to content

Using the Driver with an Emulator

Knut Olav Løite edited this page Aug 12, 2018 · 1 revision

Using the JDBC Driver with an Emulator

The JDBC driver can be used to connect to both a normal Cloud Spanner instance as well as an Emulator instance. Connecting to an emulator instead of a real Spanner instance can be very useful for running (integration) tests and creating and running small test projects. Creating and/or modifying a data model on a Spanner instance is very slow, and if you want to create tests that are completely independent of each other, you should create-and-drop your data and data model during each test run. Running these kinds of tests is a lot faster on an emulator instance than on a real Spanner instance.

Setting up an Emulator

Follow this tutorial to set up a Cloud Spanner Emulator: http://www.googlecloudspanner.com/2018/07/create-trial-account-for-cloud-spanner.html

Please note that this is a third-party emulator that is not developed, endorsed or in any way affiliated with Google.

Using the Emulator with JDBC

The JDBC URL for Cloud Spanner includes a host specification. The host is a mandatory part of a JDBC URL and is normally ignored by the driver, unless you explicitly instructs the driver to use a custom host. You do this by setting the property UseCustomHost to true. The host specified in the URL will then be used as the actual host that the driver will connect to. Example:

jdbc:cloudspanner:https://emulator.googlecloudspanner.com:8443;UseCustomHost=true;Project=static-test-project;Instance=static-test-instance;Database=spanner-jpa-example;PvtKeyPath=/path/to/emulator-key.json

Integration Tests

The integration tests of this JDBC driver are normally run against this emulator instead of against a real Spanner instance for performance reasons (and occasionally against Cloud Spanner to verify that the emulator and Cloud Spanner do not behave differently). Have a look at the integration tests to get an idea of what the capabilities of the emulator are.

Clone this wiki locally