Spring Batch使用不同的EntityManagerFactories导致org.hibernate.StaleObjectStateException

环境:JDK9.0.4,带有Spring Batch 4.0.1的Spring Boot 2.0.2,通过Hibernate使用Postgres 9.5和JPA .

我的工作使用JpaPagingItemReader和JpaItemWriter,中间有一个处理器来更新实体上的值 .

我的问题是我的批处理作业在处理第一个项目并尝试读取下一页(块/页面大小1)后抛出一个org.hibernate.StaleObjectStateException

Caused by: org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [com.my.entity#1315]

从调试开始,Batch似乎尝试在JPAPagingItemReader doReadPage()中刷新JPA会话,但是它已经在write()期间通过(我认为)另一个以某种方式实例化的EMF被刷新,因此Hibernate检测到版本号已更改并抛出例外 .

Update

我发现了一些5年前的论坛帖子/ JIRA,这表明这是批量和各种变通方法的限制 . 我很想知道我是否正确地读这个 .

https://jira.spring.io/browse/BATCH-1110

https://jira.spring.io/browse/BATCH-1166

http://forum.spring.io/forum/spring-projects/batch/92792-jpapagingitemreader-stale-object-exception

如果有人能指出这方面的任何明显陷阱,我将不胜感激 . 我已经阅读了相当数量的Javadocs但很难理解这个问题 .

@Configuration
public class DBConfig {

    @Autowired
    private DataSource dataSource;

    @Bean
    public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(emf);

        return transactionManager;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {

        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSource);
        em.setPackagesToScan("com.my.ents", "com.my.other.ends");

        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);

        return em;
    }

}



@Bean
@StepScope
public JpaPagingItemReader<ScrapeHistorySingle> getItemReader(
        @Value("#{jobParameters['council']}") String councilShortName,
        @Value("#{jobParameters['scrapeType']}") String scrapeTypeString) {

    if (councilShortName == null) {
        throw new NullPointerException("councilShortName cannot be null");
    }

    if (scrapeTypeString == null) {
        throw new NullPointerException("scrapeType cannot be null");
    }

    JpaPagingItemReader<ScrapeHistorySingle> itemReader = new JpaPagingItemReader<>();

    itemReader.setEntityManagerFactory(entityManagerFactory);

...

@Bean
public JpaItemWriter<ScrapeHistorySingle> itemWriterScrapeHistorySingle() {

    JpaItemWriter<ScrapeHistorySingle> itemWriter = new JpaItemWriter<>();

    itemWriter.setEntityManagerFactory(entityManagerFactory);

    return itemWriter;
}


@Configuration
@EnableBatchProcessing
@Component
public class CustomBatchConfigurer extends DefaultBatchConfigurer {

    private static final Log logger = LogFactory.getLog(CustomBatchConfigurer.class);

    @Autowired
    private ThreadPoolTaskExecutor taskExecutor;

    @Autowired
    private JobRegistry jobRegistry;

    @Autowired
    private DataSource dataSource;

    @Qualifier("transactionManager")
    @Autowired
    private PlatformTransactionManager transactionManager;

回答(0)