0
votes

I have a springbatch application where I will be connecting to a networked database for reading. Most of the examples I find have the database embedded for the purposes of testing stand-alone. I don't want to do that. I am not certain if the configuration would be similar or not. Here are the most germaine portions of what I have (with some obfiscation):

@Configuration
@EnableBatchProcessing
public class BatchConfig {


    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;


    private static final String GET_DATA =
        "SELECT " +
                "stuffA, " +
                "stuffB, " +
                "FROM STUFF_TABLE " +
                "ORDER BY stuffA ASC";

    @Bean
    public ItemReader<StuffDto> itemReader(DataSource dataSource) {
        return new JdbcCursorItemReaderBuilder<StuffDto>()
            .name("cursorItemReader")
            .dataSource(dataSource)
            .sql(GET_DATA)
            .rowMapper(new BeanPropertyRowMapper<>(StuffDto.class))
            .build();
}

....

, and then starting it up from application:

@SpringBootApplication
@EnableBatchProcessing
public class BatchApplication {

    public static void main(String[] args) {
        SpringApplication.run(BatchApplication.class, args);
    }
}

I don't have the datasource configured. I am not sure if, since mine is essentially just a client connection to a remote, networked database, it would configured differently than if I have the embedded set up of the various examples I find. So naturally I get error (below). I am simply looking for a simple configuration to instantiate datasource that is a connection to my remote database.

Thanks for any response

. . .

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2020-08-21 13:18:09.773 ERROR 1220 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :


APPLICATION FAILED TO START


Description:

Parameter 0 of method itemReader in java.main.configuration.configuration.BatchConfig required a bean of type 'javax.sql.DataSource' that could not be found.

The following candidates were found but could not be injected: - Bean method 'dataSource' in 'JndiDataSourceAutoConfiguration' not loaded because @ConditionalOnProperty (spring.datasource.jndi-name) did not find property 'jndi-name' - Bean method 'dataSource' in 'XADataSourceAutoConfiguration' not loaded because @ConditionalOnClass did not find required class 'javax.transaction.TransactionManager'

Action:

Consider revisiting the entries above or defining a bean of type 'javax.sql.DataSource' in your configuration.

1

1 Answers

0
votes

A javax.sql.DataSource is an abstraction over a remote/local/embedded data source. So it's a matter of configuration where you can set the JDBC connection string to a remote database host. Here is an example for a remote MySQL datasource:

@Bean
public DataSource dataSource() {
    MysqlDataSource datasource = new MysqlDataSource();
    // config values could be injected from properties/environment variables
    datasource.setURL("jdbc:mysql://REMOTE_HOST:PORT/schema");
    datasource.setUser("username");
    datasource.setPassword("passord");
    return datasource;
}

Spring Batch works with a DataSource object and does not (and should not) know if it's local, remote or embedded.