Knut Olav Løite
Knut Olav Løite

Reputation: 3512

Error when trying to open a JDBC connection to Google Cloud Spanner

I am trying to open a JDBC connection to a Google Cloud Spanner database, but I get the following error message:

java.lang.IllegalArgumentException: A project ID is required for this service but could not be determined from the builder or the environment. Please set a project ID using the builder.

My JDBC URL is as follows:

jdbc:cloudspanner://localhost;Project=project-id;Instance=instance-id;Database=database-name;PvtKeyPath=path-to-key-file

If I remove the Project property from the URL, I get the following exception:

java.sql.SQLNonTransientConnectionException: [Simba]JDBC Connection Refused: [Simba]JDBC Required Connection Key(s): Project; [Simba]JDBC Optional Connection Key(s): Language, Mode

So it seems that the driver does pick up my Project ID, but somehow does not accept it. I have checked and double checked that my project id does equal the project id that I created on Google, I have also tried to change the value to the project name instead of the project id, but to no avail.

Does anyone have a URL example that works?

EDIT: It appears to be related to the reference to the private key file. If I make an environment variable GOOGLE_APPLICATION_CREDENTIALS pointing to my private key file, the connection can successfully be made. If I remove this environment variable, I get the above exception.

Upvotes: 1

Views: 2563

Answers (2)

Knut Olav Løite
Knut Olav Løite

Reputation: 3512

As the JDBC Driver supplied by Google is severely limited (does not support DML and DDL statemetns), I have written my own JDBC Driver. The driver is designed to work with JPA/Hibernate-enabled applications. The driver can be found here: https://github.com/olavloite/spanner-jdbc

This driver supports the same kind of URL's as the driver supplied by Google, including the PvtKeyPath property.

Upvotes: 0

Vikas Kedia
Vikas Kedia

Reputation: 648

Which version of the driver are you using? In the latest version, if you are specifying the path to the credentials file in the URL then you need not set GOOGLE_APPLICATION_CREDENTIALS.

Upvotes: 1

Related Questions