JpaItemWriter:没有事务正在进行中

时间:2014-03-19 14:47:36

标签: java spring jpa spring-batch

我想使用JpaItemWriter来批处理实体。但是当我使用下面的代码来坚持时,我被告知:

Hibernate: 
    select
        nextval ('hibernate_sequence')
[] 2014-03-19 15:46:02,237 ERROR : TransactionRequiredException: no transaction is in progress

如何启用以下事项:

@Bean
public ItemWriter<T> writer() {
    JpaItemWriter<T> itemWriter = new JpaItemWriter<>();
    itemWriter.setEntityManagerFactory(emf);
}

@Configuration
@EnableTransactionManagement
@EnableBatchProcessing 
class Config{ {
     @Bean
    public LocalContainerEntityManagerFactoryBean emf() {
        LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
        emf.setDataSource(dataSource());
        emf.setPackagesToScan("my.package");
        emf.setJpaVendorAdapter(jpaAdapter());
        emf.setJpaProperties(jpaProterties());
        return emf;
}

编辑:

@Bean
public Job airlineJob(JobBuilderFactory jobs, Step step) {
    return jobs.get("job")
            .start(step)
            .build();
}

//Reader is a `FlatFileItemReader`, writer is `CustomItemWriter`.
@Bean
public Step step(StepBuilderFactory steps,
        MultiResourceItemReader<T> rea,
        ItemProcessor<T, T> pro,
        ItemWriter<T> wr) {
    return steps.get("step")
            .reader(rea)
            .processor(proc)
            .writer(wr)
            .build();
}

//use same datasource and tx manager as in the full web application
@Bean
public JobLauncher launcher(TransactionManager tx, DataSource ds) throws Exception {
    SimpleJobLauncher launcher = new SimpleJobLauncher();

    JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
    factory.setDataSource(ds);
    factory.setTransactionManager(tx);

    jobLauncher.setJobRepository(factory.getJobRepository());
    return launcher;
}

编辑2作为对@Haim的回复:

@Bean
    public JpaItemWriter<T> jpaItemWriter(EntityManagerFactory emf) {
        JpaItemWriter<T> writer = new JpaItemWriter<T>();
        writer.setEntityManagerFactory(emf);
        return writer;
    }

3 个答案:

答案 0 :(得分:8)

我同意Michael Minella:Spring批处理作业存储库不喜欢与其他人共享其事务管理器。逻辑很简单,如果您在步骤失败时与步骤事务管理器共享作业事务管理器,它将回滚步骤和写入作业存储库的数据。这意味着您不会保留数据以重新启动作业。 要使用两个事务管理器,您需要:

删除@EnableTransactionManagement以防您使用用于@Transactional以上
定义其他事务管理器

@Bean
@Qualifier("jpaTrx")
public PlatformTransactionManager jpaTransactionManager() {
       return new JpaTransactionManager(emf());
}

将事务管理器设置为您的步骤

@Autowired
@Qualifier("jpaTrx")
PlatformTransactionManager jpaTransactionManager

 //Reader is a FlatFileItemReader, writer is CustomItemWriter.
    @Bean
    public Step step(StepBuilderFactory steps,
            MultiResourceItemReader<T> rea,
            ItemProcessor<T, T> pro,
            ItemWriter<T> wr) {
        return steps.get("step")
                //attach tx manager
                .transactionManager(jpaTransactionManager)
                .reader(rea)
                .processor(proc)
                .writer(wr)
                .build();
    }

答案 1 :(得分:0)

我解决了它创建自己的事务性JpaWriter:

@Component
public class CustomItemWriter<T> extends JpaItemWriter<T> {
    @Override
    @Transactional
    public void write(List<? extends T> items) {
        super.write(items);
    }
}

答案 2 :(得分:-1)

为您的数据源编写自己的 JpaTransactionManager 而不是 spring 事务

   @Autowired
   private DataSource dataSource;

   @Bean
   @Primary
   public JpaTransactionManager jpaTransactionManager() {
        final JpaTransactionManager tm = new JpaTransactionManager();
        tm.setDataSource(dataSource);
        return tm;
   }

如果这对您有用,请点击向上投票。