Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify separate datasource for Activiti in a Spring Boot app

How would I use two separate dataSources in my Spring Boot application?

I would like one dataSource to be used by my application, to be used for persisting my models and a separate dataSource for use by the Activiti engine, so it can keep it's entities in a separate database.

As of now Activiti's tables and my app's tables are created in the same database.

[Edited]:

I know I can define two separate DataSource beans like:

@Bean
public DataSource appDataSource() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
    dataSource.setUrl("xxx");
    dataSource.setUsername("xxx");
    dataSource.setPassword("xxx");
    return dataSource;
}

@Bean
public DataSource activitiDataSource() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName("xxx");
    dataSource.setUrl("xxx");
    dataSource.setUsername("xxx");
    dataSource.setPassword("xxx");
    return dataSource;
}

But how do i inform Activiti to use the activitiDataSource?

I am using Activiti 5.16.4, btw...

Thanks!

like image 223
stratosgear Avatar asked Oct 27 '25 15:10

stratosgear


1 Answers

@andy-wilkinson gave the answer but here is an example on how to use it. As you suggested, create another DataSource and then wire it up to to a SpringProcessEngineConfiguration. Like so:

@Configuration
public class ActivitiConfiguration extends AbstractProcessEngineAutoConfiguration {

    @Bean
    @ConfigurationProperties(prefix = "datasource.activiti")
    public DataSource activitiDataSource() {
        return DataSourceBuilder
                .create()
                .url("jdbc:h2:mem:activiti")
                .username("activiti")
                .driverClassName("org.h2.Driver")
                .build();
    }

    @Bean
    public SpringProcessEngineConfiguration springProcessEngineConfiguration(
            PlatformTransactionManager transactionManager,
            SpringAsyncExecutor springAsyncExecutor) throws IOException {

        return baseSpringProcessEngineConfiguration(
                activitiDataSource(),
                transactionManager,
                springAsyncExecutor);
    }
}

Activiti will use activitiDataSource to create its tables and persist it's data.

Now you can create another DataSource to carry your apps tables and data. Here is a basic example based off of spring-boot-sample-basic. Basically it persists a customerId in a WaiterEntity/WaiterRepository (with Spring Data JPA - left out for brevity) and then passes that persisted value onto the Activiti basic2.bpmn process, which just prints it out to console.

@SpringBootApplication
public class Application {

    @Bean
    @Primary
    @ConfigurationProperties(prefix = "datasource.primary")
    public DataSource primaryDataSource() {
        return DataSourceBuilder
                .create()
                .url("jdbc:h2:mem:primary")
                .username("primary")
                .driverClassName("org.h2.Driver")
                .build();
    }

    @Bean
    CommandLineRunner basics(final RuntimeService runtimeService,
                             final WaiterRepository repository) {
        return new CommandLineRunner() {

            @Override
            public void run(String... strings) throws Exception {
                runtimeService.startProcessInstanceByKey(
                        "waiter2",
                        Collections.singletonMap(
                                "customerId",
                                (Object) repository.save(new WaiterEntity(123L)).getCustomerId()));
            }
        };
    }

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

note the @Primary on primaryDataSource. If you leave that out your WAITER_ENTITY table will be created in the activitiDataSource (without any other specific configuration).

like image 132
Donovan Muller Avatar answered Oct 29 '25 05:10

Donovan Muller



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!