如何设置两名交易经理?

时间:2018-09-04 06:58:48

标签: spring jpa spring-batch

我正在处理已经设置了事务管理器的Spring应用程序。

在配置类中,它已经设置了一个从persistence.xml读取的实体管理器,然后设置了JpaTransactionManager。

我需要创建一个Spring Batch实现,问题是,正如我从不同的帖子中发现的那样,当使用@EnableBatchProcessing批注时,似乎第二个事务管理器已注册,并且我无法保留数据在我的小任务中。

是否可以使用两个事务管理器或以可以持久保存数据的方式配置应用程序?

能给我提供示例代码吗?

谢谢。

编辑:

这是应用程序配置类,已经存在于应用程序中:

@Configuration
@ComponentScan({
    ...
})
@EnableJpaRepositories("...")
@EnableTransactionManagement
@EnableJpaAuditing
@Import({SecurityConfig.class})
@PropertySource("classpath:application.properties")
public class ApplicationConfig {

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {

        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setPersistenceXmlLocation("classpath:/META-INF/persistence.xml");
        return factory;
    }

    @Bean
    public PlatformTransactionManager transactionManager() {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
        return transactionManager;
    }

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}

这是我的批处理配置:

@Configuration
@EnableBatchProcessing
public class BatchConfig {

    @Autowired
    private JobBuilderFactory jobs;

    @Autowired
    private StepBuilderFactory steps;

    @Autowired
    @Qualifier("entityManagerFactory")
    private LocalEntityManagerFactoryBean batchEntityManagerFactory;


}

我从中得到:

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.orm.jpa.LocalEntityManagerFactoryBean com.xxx.xxx.xxx.configuration.BatchConfig.batchEntityManagerFactory; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.orm.jpa.LocalEntityManagerFactoryBean] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.sp
ringframework.beans.factory.annotation.Qualifier(value=entityManagerFactory)}

编辑2: 这就是我所做的:

@EnableJpaRepositories("xxx")
@Configuration
@EnableBatchProcessing
@PropertySource("classpath:application.properties")
public class BatchConfig {

    @Autowired
    private JobBuilderFactory jobs;

    @Autowired
    private StepBuilderFactory steps;

    @Autowired
    private ReportReaderProcessor reportReaderProcessor;

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {

        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setPersistenceXmlLocation("classpath:/META-INF/persistence.xml");
        return factory;
    }

    @Bean
    public BatchConfigurer batchConfigurer() {
        return new DefaultBatchConfigurer() {
            @Override
            public PlatformTransactionManager getTransactionManager() {
                JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
                jpaTransactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
                return jpaTransactionManager;
            }
        };
    }
    @Bean
    public JobRepository jobRepository() throws Exception {
        MapJobRepositoryFactoryBean factory = new MapJobRepositoryFactoryBean();
        factory.setTransactionManager(batchConfigurer().getTransactionManager());
        return (JobRepository) factory.getObject();
    }

    @Bean
    public SimpleJobLauncher simpleJobLauncher() throws Exception {
        SimpleJobLauncher simpleJobLauncher = new SimpleJobLauncher();
        simpleJobLauncher.setJobRepository(jobRepository());
        return simpleJobLauncher;
    }

    @Bean
    public Step readReports() {
        return steps
                .get("readReports")
                .tasklet(reportReaderProcessor)
                .build();
    }

    @Bean
    public Job reportJob() {
        return jobs
                .get("submitReportJob")
                .start(readReports())
                .build();
    }
}

但是现在我遇到另一个错误:

15:47:23,657 ERROR [stderr] (pool-36-thread-1) org.springframework.transaction.InvalidIsolationLevelException: DefaultJpaDialect does not support custom isolation levels due to limitations in standard JPA. Specific arrangements may be implemented in custom JpaDialect variants.

1 个答案:

答案 0 :(得分:2)

在这种情况下,这里存在一个未解决的问题:https://jira.spring.io/browse/BATCH-2294在4.1.0.M3版本中已修复。要使用自定义事务管理器,您需要在应用程序上下文中提供BatchConfigurer,例如:

@Bean
public BatchConfigurer batchConfigurer() {
    return new DefaultBatchConfigurer() {
        @Override
        public PlatformTransactionManager getTransactionManager() {
           return new MyTransactionManager();
        }
    };
}