How to config data schema of BPM add-on in separate database?

Hi team,
When implement flowable bpm add-on, application automatic generate data schema in the main database.
How to config data schema of BPM add-on in separate additional database?

Hi,

You can try to have database tables of Flowable engine in a separate database, but we haven’t tested this approach, so there may be problems with it.

In order to configure Flowable engine to use separate database you will need to create a new DataSource bean and pass this bean to the Flowable process engine configuration.

ProcessEngineConfiguration can be enhanced using the EngineConfigurationConfigurer - you may read about it in Flowable docs:

import org.flowable.spring.SpringProcessEngineConfiguration;
import org.flowable.spring.boot.EngineConfigurationConfigurer;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;

@Component
public class MyProcessEngineConfigurer implements EngineConfigurationConfigurer<SpringProcessEngineConfiguration> {

    private DataSource bpmstoreDataSource;

    public MyProcessEngineConfigurer(@Qualifier("bpmstoreDataSource") DataSource bpmstoreDataSource) {
        this.bpmstoreDataSource = bpmstoreDataSource;
    }

    @Override
    public void configure(SpringProcessEngineConfiguration engineConfiguration) {
        engineConfiguration.setDataSource(bpmstoreDataSource);
    }
}

DataSource bean with the bpmstoreDataSource name must be configured in advance. In my experiment I’ve created it as a part of additional data store using Studio:

import io.jmix.core.JmixModules;
import io.jmix.core.Resources;
import io.jmix.data.impl.JmixEntityManagerFactoryBean;
import io.jmix.data.impl.JmixTransactionManager;
import io.jmix.data.persistence.DbmsSpecifics;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

@Configuration
public class BpmstoreStoreConfiguration {

    @Bean
    @ConfigurationProperties("bpmstore.datasource")
    DataSourceProperties bpmstoreDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    @ConfigurationProperties(prefix = "bpmstore.datasource.hikari")
    DataSource bpmstoreDataSource(@Qualifier("bpmstoreDataSourceProperties") DataSourceProperties properties) {
        return properties.initializeDataSourceBuilder().build();
    }

    @Bean
    LocalContainerEntityManagerFactoryBean bpmstoreEntityManagerFactory(
            @Qualifier("bpmstoreDataSource") DataSource dataSource,
            JpaVendorAdapter jpaVendorAdapter,
            DbmsSpecifics dbmsSpecifics,
            JmixModules jmixModules,
            Resources resources) {
        return new JmixEntityManagerFactoryBean("bpmstore", dataSource, jpaVendorAdapter, dbmsSpecifics, jmixModules, resources);
    }

    @Bean
    JpaTransactionManager bpmstoreTransactionManager(@Qualifier("bpmstoreEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
        return new JmixTransactionManager("bpmstore", entityManagerFactory);
    }
}

This solution is working for me. Thank you very much!