本文介绍了在Spring Boot应用中为Activiti指定单独的数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Spring Boot应用程序中使用两个单独的数据源?

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.

到目前为止,Activiti的表和我的应用程序的表都是在Activiti的表中创建的。

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

我知道我可以定义两个单独的DataSource bean,如:

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;
}

但是我如何通知Activiti使用activitiDataSource?

But how do i inform Activiti to use the activitiDataSource?

我正在使用Activiti 5.16.4,btw ...

I am using Activiti 5.16.4, btw...

谢谢!

推荐答案

@ andy-wilkinson给出了答案,但这是有关如何使用它的示例。
按照您的建议,创建另一个 DataSource ,然后将其连接到 SpringProcessEngineConfiguration 。像这样:

@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将使用 activitiDataSource 创建其表并保留其数据。

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

现在,您可以创建另一个 DataSource 来携带应用程序表和数据。这是一个基于。
基本上,它将 customerId 保留在 WaiterEntity / WaiterRepository (为简便起见,省略了Spring Data JPA),然后将该持久值传递给Activiti basic2.bpmn 进程,该进程仅将其打印到控制台。 / p>

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

请注意 @Primary primaryDataSource 上。如果您不进行此操作,将在 activitiDataSource 中创建 WAITER_ENTITY 表(没有任何其他特定配置)。

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).

这篇关于在Spring Boot应用中为Activiti指定单独的数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 01:16