Neel
Neel

Reputation: 33

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

Upvotes: 3

Views: 2638

Answers (2)

Dhruv Saksena
Dhruv Saksena

Reputation: 219

You need to override DefaultTaskConfigurer

@Configuration
public class BatchConfigurer extends DefaultTaskConfigurer
{

    public BatchConfigurer(@Qualifier("batchDataSource") DataSource dataSource) 
    {
        super(dataSource);
    }

}

Upvotes: 1

Michael Minella
Michael Minella

Reputation: 21453

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

Upvotes: 3

Related Questions