0

In my case, I am using spring boot and MySQL DB. I am having two database users.

  1. Admin user is used by liquibase to create tables. It is having its own data source.
  2. Dev User is used to read/write data to the tables. It is having another data source.
@Bean
public DataSource devDataSource() {
        HikariDataSource dataSource = new HikariDataSource();
        ...
        ...
        return dataSource;
        
        }
@Bean   
public DataSource liquibaseDataSource() {
        HikariDataSource dataSource = new HikariDataSource();
        ...
        ...
        return dataSource;
        
        }

Now I am trying to add the Spring batch to the service. I am getting a duplicate bean exception. It is getting fixed when I am adding @Primary to liquibase datasource, but we dont want to use this for read/write operations for batch.

If I add @Primary in the Dev User data source the batch meta-data tables are not getting created. As far as I understood, Dev User is not having the privilege to create the table. So when Dev data source is configured for batch, the tables are not getting created.

I want to create batch meta-data table via spring config(automatically) and for all read, and write to all tables(including batch meta table) via Dev user if possible.

Not sure what I am missing here. any help is appreciated

sub
  • 527
  • 1
  • 7
  • 24

1 Answers1

0
@Bean("hikaridev") 
public DataSource devDataSource() {
        HikariDataSource dataSource = new HikariDataSource();
        ...
        ...
        return dataSource;
        
        }

@Bean("liquibase") 
public DataSource liquibaseDataSource() {
        HikariDataSource dataSource = new HikariDataSource();
        ...
        ...
        return dataSource;
        
        }

AnnotationConfigApplicationContext context;
        context = new AnnotationConfigApplicationContext();

DataSource dev = (DataSource) context.getBean("hikaridev");
DataSource liquibase = (DataSource) context.getBean("liquibase");
Oussama ZAGHDOUD
  • 1,767
  • 5
  • 15
  • This works for the normal spring boot app using two data sources, but in the case of the spring batch, it's not working. – sub Apr 05 '22 at 09:56