How to java-configure separate datasources for spring batch data and business data? Should I even do it?

后端 未结 7 1559
感情败类
感情败类 2020-11-29 22:33

My main job does only read operations and the other one does some writing but on MyISAM engine which ignores transactions, so I wouldn\'t require necessarily tr

7条回答
  •  抹茶落季
    2020-11-29 22:57

    Add @BatchDataSource to the batch data source if your spring boot's version is 2.2.0 or later.

    Details of this annotation is as follows:

    /**
     * Qualifier annotation for a DataSource to be injected into Batch auto-configuration. Can
     * be used on a secondary data source, if there is another one marked as
     * {@link Primary @Primary}.
     *
     * @author Dmytro Nosan
     * @since 2.2.0
     */
    @Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE, ElementType.ANNOTATION_TYPE })
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Qualifier
    public @interface BatchDataSource {
    
    }
    

    for example:

    @BatchDataSource
    @Bean("batchDataSource")
    public DataSource batchDataSource(@Qualifier("batchDataSourceProperties") DataSourceProperties dataSourceProperties) {
            return dataSourceProperties
                    .initializeDataSourceBuilder()
                    .type(HikariDataSource.class)
                    .build();
    }
    

提交回复
热议问题