IllegalTransactionStateException配置多个数据源时发现预绑定JDBC连接

时间:2018-11-04 07:14:42

标签: java spring spring-boot spring-data-jpa spring-batch

我有一个Spring Batch项目,其中我从数据源读取数据,处理数据并写入另一个主数据源。我将CrudRepository扩展到dao操作。

我正在尝试为我的springbatch + spring boot应用程序配置多个数据源,以下是软件包结构:

myproject
   ---com
   ---batch
        ---config
                 ---firstDsConfig.java
                 ---secondDsConfig.java
        ---firstrepository
                 ---firstCrudRepository.java
        ---secondRepository
                 ---SecondCrudRepository.java
        ---firstEntity
                 ---firstDBEntity.java
        ---secondEntity
                 ---secondDBEntity.java
        ----main
                 ---MyMainClass.java

firstDsConfig.java的代码:

@Configuration
@EnableJpaRepositories(
        entityManagerFactoryRef = "firstEntityManagerFactory",
        transactionManagerRef = "firstTransactionManager",
        basePackages = "com.batch.firstrepository"
)
@EnableTransactionManagement
public class FirstDbConfig {

    @Primary
    @Bean(name = "firstEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean firstEntityManagerFactory(final EntityManagerFactoryBuilder builder,
                                                                            final @Qualifier("firstDs") DataSource dataSource) {
        return builder
                .dataSource(dataSource)
                .packages("com.batch.firstEntity")
                .persistenceUnit("abc")
                .build();
    }


    @Primary
    @Bean(name = "firstTransactionManager")
    public PlatformTransactionManager firstTransactionManager(@Qualifier("firstEntityManagerFactory")
                                                              EntityManagerFactory firstEntityManagerFactory) {
        return new JpaTransactionManager(firstEntityManagerFactory);
    }


    @Primary
    @Bean(name = "firstDs")
    @ConfigurationProperties(prefix = "spring.datasource.first")
    public DataSource firstDataSource() {
        return DataSourceBuilder.create().build();
    }
}

secondDsConfig的代码:

@Configuration
@EnableJpaRepositories(
        entityManagerFactoryRef = "secondEntityManagerFactory",
        transactionManagerRef = "secondTransactionManager",
        basePackages = "com.batch.secondrepository"
)
@EnableTransactionManagement
public class FirstDbConfig {

    @Primary
    @Bean(name = "secondEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean secondEntityManagerFactory(final EntityManagerFactoryBuilder builder,
                                                                            final @Qualifier("secondDs") DataSource dataSource) {
        return builder
                .dataSource(dataSource)
                .packages("com.batch.secondEntity")
                .persistenceUnit("xyz")
                .build();
    }


    @Primary
    @Bean(name = "secondTransactionManager")
    public PlatformTransactionManager secondTransactionManager(@Qualifier("secondEntityManagerFactory")
                                                              EntityManagerFactory firstEntityManagerFactory) {
        return new JpaTransactionManager(secondEntityManagerFactory);
    }


    @Primary
    @Bean(name = "secondDs")
    @ConfigurationProperties(prefix = "spring.datasource.second")
    public DataSource secondDataSource() {
        return DataSourceBuilder.create().build();
    }
}

这是我的主班

@EnableScheduling
@EnableBatchProcessing
@SpringBootApplication(scanBasePackages = { "com.batch" })
public class MyMainClass {

    @Autowired
    private JobLauncher jobLauncher;

    @Autowired
    private JobRepository jobRepository;

    @Autowired
    @Qualifier(firstDs)
    private DataSource dataSource;

    @Autowired
    @Qualifier("myJob")
    private Job job;

    public static void main(String[] args) throws Exception {
        SpringApplication.run(MyMainClass.class, args);
    }

    @EventListener(ApplicationReadyEvent.class)
    private void start() throws Exception {

        jobLauncher.run(job, new JobParameters());
    }


    @Bean(name="jobService")
    public JobService jobService() throws Exception {
        SimpleJobServiceFactoryBean factoryBean = new SimpleJobServiceFactoryBean();
        factoryBean.setDataSource(dataSource);
        factoryBean.setJobRepository(jobRepository);
        factoryBean.setJobLocator(new MapJobRegistry());
        factoryBean.setJobLauncher(jobLauncher);
        factoryBean.afterPropertiesSet();
        return factoryBean.getObject();
    }
}

这是原始存储库:

第一个凝乳回购

public interface FirstCrudRepository extends CrudRepository<FirstDbEntity, Integer> {

    List<FirstDbEntity> findByOId(String oId);

}

第二个凝乳回购

public interface SecondCrudRepository extends CrudRepository<SecondDbEntity, Integer> {

    List<SecondDbEntity> findByPid(String pid);

}

运行应用程序时,我在使用FirstCrudRepository保存记录时看到以下错误:

 org.springframework.transaction.IllegalTransactionStateException: Pre-bound JDBC Connection found! JpaTransactionManager does not support running within DataSourceTransactionManager if told to manage the DataSource itself. It is recommended to use a single JpaTransactionManager for all transactions on a single DataSource, no matter whether JPA or JDBC access.

注意:我能够从SecondCrudRepository成功获取详细信息

2 个答案:

答案 0 :(得分:1)

默认情况下,如果提供DataSource,Spring Batch将使用DataSourceTransactionManager,它对JPA配置一无所知。您需要告诉Spring Batch使用您的JpaTransactionManager。对此进行了解释:

答案 1 :(得分:0)

我怀疑有2个不同的交易管理器会引起问题。用@Transactional注释并指定事务管理器会有所帮助

来源:

Spring - Is it possible to use multiple transaction managers in the same application?