Spring 如何设置两个事务管理器?

Spring 如何设置两个事务管理器?,spring,jpa,spring-batch,Spring,Jpa,Spring Batch,我正在开发一个已经设置了事务管理器的Spring应用程序 在一个配置类中,它已经设置了一个从persistence.xml读取的实体管理器,然后设置了一个JpaTransactionManager 我需要创建一个Spring批处理实现,问题是,正如我在不同的帖子中发现的,当使用@EnableBatchProcessing注释时,似乎注册了第二个事务管理器,我无法在我的Tasklet中持久化数据 是否可以使用两个事务管理器或以能够持久保存数据的方式配置我的应用程序 你能给我提供样本代码吗 提前谢谢

我正在开发一个已经设置了事务管理器的Spring应用程序

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

我需要创建一个Spring批处理实现,问题是,正如我在不同的帖子中发现的,当使用@EnableBatchProcessing注释时,似乎注册了第二个事务管理器,我无法在我的Tasklet中持久化数据

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

你能给我提供样本代码吗

提前谢谢

编辑:

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

@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.

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

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

您仍然需要提供一个BatchConfigurer并实现getTransactionManager以返回现有事务管理器。SpringBatch不会决定使用哪个事务管理器。如果您尝试使用版本4.1.0.M3并向我们提供反馈,那就太好了!非常感谢。这仍然是让SpringBatch在4.1.2中使用JpaTransactionManager的方法吗?我发现自己已经有了错误值[org.springframework.jdbc.datasource]。ConnectionHolder@32c3d9cf]当我尝试使用我的jpa回复时,我在下面的帖子中找到了你的答案。我已经成功地使用了这个解决方案,但我想确保它在当前版本的spring batch中是正确的。是的,自4.1.0.M3以来,这一点没有改变。