I am going to create batch for GCP spanner am planning to use JdbcCursorItemReader that needs datasource, So i need to create datasource for my GCP Spanner instance, Can you please suggest me on this?
1 Answers
You need to add the Cloud Spanner JDBC driver to your build path like this:
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-spanner-jdbc</artifactId>
<version>1.9.0</version>
</dependency>
Then you can define a Spring data source in the normal way. Doing it programmatically would look like this:
@Bean
public DataSource spannerDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.google.cloud.spanner.jdbc.JdbcDriver");
dataSource.setUrl("jdbc:cloudspanner:/projects/<YOUR-PROJECT-ID>/instances/<YOUR-INSTANCE-ID>/databases/<YOUR-DATABASE-ID>?credentials=<PATH-TO-YOUR-SERVICE-CREDENTIALS>");
return dataSource;
}
---- Additional information after comment ----
Google Cloud Spanner is not a database that is supported by default by Spring Batch. You therefore need to explicitly set the database type to one of the supported database types. Have a look at this answer to see how that is done.
You need to select one of the supported databases, even though you are using another database. This might cause other compatibility problems, especially if you let Spring Batch automatically generate your data model. If you create the data model by hand and only use Spring Batch for reading data, it should be less of a problem. I would recommend to try to set the database type to POSTGRES and see if it works.

- 2,964
- 7
- 19
-
1able to get Datasource but getting below exception, when try to use the datasource Exception: "DatabaseType not found for product name: [Google Cloud Spanner]" – wild Nov 07 '19 at 11:32
-
Finally it worked for me by extending the DefaultBatchConfigurer :) – wild Nov 09 '19 at 04:01
-
is it equivalent to AWS aurora? – GvSharma Jan 07 '22 at 10:02