1

On my Spring Cloud Task project I'm using Spring Batch. I want to separate the metadata (BATCH_ and TASK_ tables) from the production data, so I configure two DataSource like this:

# DataSource: Production data
prod.datasource.jdbc-url=jdbc:sqlserver://localhost;databaseName=PROD
prod.datasource.data-source-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
prod.datasource.username=...
prod.datasource.password=...


# DataSource: Jobs and Tasks metadata
tasks.datasource.jdbc-url=jdbc:sqlserver://localhost;databaseName=TASKS
tasks.datasource.data-source-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
tasks.datasource.username=sa
tasks.datasource.password=...

+

public class DataSourceConfig {

    @Bean
    @Primary
    @ConfigurationProperties(prefix = "prod.datasource")
    public DataSource dataSourceProd() {
        return DataSourceBuilder.create().build();
    }


    @Bean
    @ConfigurationProperties(prefix = "tasks.datasource")
    public DataSource dataSourceTasks() {
        return DataSourceBuilder.create().build();
    }

}

+

@Configuration
@EnableTask
@EnableBatchProcessing
@Import(DataSourceConfig.class)
public class JobConfig {

    @Autowired
    private JobBuilderFactory jobs;

    @Autowired
    private StepBuilderFactory steps;


    @Bean
    public TaskConfigurer taskConfigurer(@Qualifier("dataSourceTasks") DataSource source) {
        return new DefaultTaskConfigurer(source);
    }


    @Bean
    public BatchConfigurer batchConfigurer(@Qualifier("dataSourceTasks") DataSource source) {
        return new DefaultBatchConfigurer(source);
    }

    //+jobs, steps...

When I run it, I'm getting Cannot cast com.microsoft.sqlserver.jdbc.SQLServerDriver to javax.sql.DataSource from taskConfigurer bean. Do I miss something?

Using Spring Boot starter Batch 2.0.0.RELEASE and Cloud starter Task 2.0.0.M3

anat0lius
  • 2,145
  • 6
  • 33
  • 60

2 Answers2

1

use prod.datasource.driverClassName

instead of

prod.datasource.data-source-class-name
Bhushan Uniyal
  • 5,575
  • 2
  • 22
  • 45
  • At first I was using `datasource.url` and it was yelling me to use `datasource.jdbc-url` which I found is from `database.hikari` properties. Then I explored these hikari properties and I saw there is `data-source-class-name` so I guessed I should use it to be "syncroned" with hikari properties. It's a bit confusing. – anat0lius Mar 27 '18 at 13:46
  • 1
    https://stackoverflow.com/questions/26490967/how-do-i-configure-hikaricp-in-my-spring-boot-app-in-my-application-properties-f – Bhushan Uniyal Mar 27 '18 at 13:49
  • it can't initialize the schema it seems: `Failed to start bean 'taskLifecycleListener';....Could not increment identity;....Invalid object name 'TASK_SEQ'` – anat0lius Mar 27 '18 at 15:29
  • is TASK_SEQ is exist in db – Bhushan Uniyal Mar 27 '18 at 16:07
  • No, there isn't, any TASK_ table nor BATCH_ – anat0lius Mar 28 '18 at 06:44
  • man i have never work around spring batching, by default spring.jpa.hibernate.ddl-auto is none means dev are responsible for creating db schema not hibernate – Bhushan Uniyal Mar 28 '18 at 06:47
  • It's SQLServer issue. I've created a 2nd data base (using default setting from Management Studio) and maybe it miss permissions. If the 2nd DataSource (Jobs) points to Prod data base, then it's ok – anat0lius Mar 28 '18 at 10:22
  • No, I don't know what permissions needs. Btw, spring batch genereates the schema on my 2nd data base, it's only spring task that doesn't do it. Very strange, why Spring Task would need special permissions but Spring Batch no? I've created the DB as 'sa' and gived the DataSource the 'sa' login – anat0lius Mar 28 '18 at 11:48
  • is there any error logs, or what kind of permission message you are getting – Bhushan Uniyal Mar 28 '18 at 12:53
  • I figured out that Spring Cloud Data Flow server and the TaskConfigurer Datasources must match. Then it works. – anat0lius Apr 03 '18 at 08:34
  • ohh interesting, may be you can write a blog on it – Bhushan Uniyal Apr 03 '18 at 09:13
1

com.microsoft.sqlserver.jdbc.SQLServerDriver does not implement javax.sql.DataSource it's just the driver.

Use a class that implements javax.sql.DataSource such as com.microsoft.sqlserver.jdbc.SQLServerDataSource.

Or you could even use an alternative DataSource such as a pooled DataSource like com.mchange.v2.c3p0.ComboPooledDataSource

xtratic
  • 4,600
  • 2
  • 14
  • 32