Search code examples
springspring-bootspring-batchspring-cloud-task

Getting Exception 'java.lang.IllegalStateException:Expected one datasource and found 2' when more than one datasource defined


When i define more than one datasource in my spring cloud task application , it throws an exception. This is how i have defined the datasources

@Primary
@Bean(name="datasource1")
@ConfigurationProperties(prefix="spring.datasource")
public javax.sql.DataSource primaryDataSource() {
    return  DataSourceBuilder.create().build();
}

@Bean(name="datasource2")
@ConfigurationProperties(prefix="spring.datasource1")
public javax.sql.DataSource primaryDataSource1() {
    return  DataSourceBuilder.create().build();
}

@Bean
public TaskConfigurer taskConfigurer() {
    return new DefaultTaskConfigurer(primaryDataSource());
}

I have seen suggestions to put @Primary , defining TaskConfigurer like above, but none of them is working.Has any one faced this kind of problem ?

Thanks, Neel


Solution

  • You're going to need to override the listener. It, like the other autoconfig around tasks, doesn't know what datasource to use when you've defined more than one. I've created an issue to address this in a future version: https://github.com/spring-cloud/spring-cloud-task/issues/252