Entities not persisting. Are RepositoryItemWriter & SimpleJpaWriter thread-safe?

前端 未结 1 1088
生来不讨喜
生来不讨喜 2021-01-22 19:04

I have encountered an odd issue with a RepositoryItemWriter, where it does not appear to be persisting entities correctly through my configured Spring Data JPA repository to the

相关标签:
1条回答
  • 2021-01-22 19:47

    Looks like I fixed it by adding a PlatformTransactionManager. See changes below. Hope this helps someone, as this is one that I've been fighting for a couple of weeks now. What I don't understand is why Spring Boot is able to provide a JtaTransactionManager w/o Bitronix or Atomikos in my pom.xml.

    @SuppressWarnings("SpringJavaAutowiringInspection")
    @Bean
    public Step orderStep(StepBuilderFactory stepBuilderFactory, ItemReader<OrderEncounter> orderEncounterReader, ItemWriter<List<Order>> orderWriter,
                          ItemProcessor<OrderEncounter, List<Order>> orderProcessor, TaskExecutor taskExecutor, PlatformTransactionManager platformTransactionManager) {
        return stepBuilderFactory.get("orderStep")
                .<OrderEncounter, List<Order>> chunk(10)
                .reader(orderEncounterReader)
                .processor(orderProcessor)
                .writer(orderWriter)
                .taskExecutor(taskExecutor)
                .transactionManager(platformTransactionManager)
                .build();
    }
    

    And the configuration class:

    @Configuration
    public class JTOpenDataSourceConfiguration {
    
    @Bean(name="distillerDataSource")
    @Primary
    @ConfigurationProperties(prefix="spring.datasource.distiller")
    public DataSource distillerDataSource() {
        return DataSourceBuilder.create().build();
    }
    
    @Bean
    @ConfigurationProperties(prefix="spring.datasource.target")
    public DataSource targetDataSource() {
        return DataSourceBuilder.create().build();
    }
    
    @Bean
    public PlatformTransactionManager platformTransactionManager(@Qualifier("targetDataSource") DataSource targetDataSource) {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setDataSource(targetDataSource);
        return transactionManager;
    }
    
    0 讨论(0)
提交回复
热议问题