2

I have a spring batch project wherein I read data from a datasource , process the data and write into another primary data source. I am extending CrudRepository for dao operations.

I am trying to configure multiple datasources for my springbatch + spring boot application below is the package structure :

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

Code for 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();
    }
}

Code for 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();
    }
}

Here is my main class

@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();
    }
}

Here are the crud repo:

First curd repo

public interface FirstCrudRepository extends CrudRepository<FirstDbEntity, Integer> {

    List<FirstDbEntity> findByOId(String oId);

}

Second curd repo

public interface SecondCrudRepository extends CrudRepository<SecondDbEntity, Integer> {

    List<SecondDbEntity> findByPid(String pid);

}

When I run my application I see following error while saving record using 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.

Note: I am able to fetch details successfully from SecondCrudRepository

Manas Saxena
  • 2,171
  • 6
  • 39
  • 58

2 Answers2

1

By default, if you provide a DataSource, Spring Batch will use a DataSourceTransactionManager which knows nothing about your JPA configuration. You need to tell Spring Batch to use your JpaTransactionManager. This is explained in the:

Mahmoud Ben Hassine
  • 28,519
  • 3
  • 32
  • 50
  • thanks , it works now I extended one of the config class with DefaultBatchConfigurer in my code provided in the question – Manas Saxena Nov 09 '18 at 09:54
0

I suspect there are 2 different transaction managers present which would be causing issue. Annotate with @Transactional and specifying the transaction manager would help

Source:

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

greengreyblue
  • 389
  • 3
  • 12