7982

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!

Answer1:

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

Answer2:

In Spring boot, you should config your datasource config in application.properties.

For example:

spring.datasource.url=jdbc:postgresql://localhost:5432/activiti_development spring.datasource.username=postgres spring.datasource.password=postgres spring.datasource.driver-class-name=org.postgresql.Driver

Reference: http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html

Recommend

  • Opening URL on Android web browser causes Google search
  • iOS failing to render vcf file
  • How to store User name and password details in strings.xml
  • For loop with two input files
  • accessing another activity's textView for appending text in different activities
  • writing proper “HEAD” and “GET” request in winsock c++
  • JSF 2.0: h:inputText inside composite component fails with non-String objects when validation is set
  • Android boolean preference problem
  • Why I can't enforce EditTextPreference to take just numbers?
  • Parsing Strings in SuperCSV
  • send intent from main activity to two fragment
  • Assign dynamic ids to hidden fields when iterating over a collection
  • Remove xml tag during xstream parsing?
  • Spring @Transactional - javax.persistence.TransactionRequiredException
  • Failed to read schema document 'http://www.springframework.org/schema/tool/spring-tool-4.1.xsd&
  • Retrofit method return wildcard
  • Jackson Java 8 DateTime serialisation
  • Circle movement upon rectangle Collision
  • How to call BeanFactoryPostProcessor.postProcessBeanFactory method when use Spring with XML configur
  • Java inject implementation using TypeLiteral
  • How to capture enclosing scope in Runnable
  • Django - can't get highchart to display data
  • Android - HorizontalScrollView will not scroll all the way
  • On which thread JavaFX change listeners are executed ?
  • JPA CDI Injecting DAO into an Entity
  • JSF NumberFormatException with f:setPropertyActionListener
  • FOSRestBundle: The controller must return a response (Array()) given
  • What is the likely cause of a net::ERR_CONNECTION_ABORTED when uploading a file to Spring
  • Append Textarea result from another class
  • Undefined navigator.push React-native 0.43.4
  • Using same constraints in multiple classes
  • drawing random circles, storing their coorindates in an array
  • Display validation errors inside
  • Laravel: Getting Session ID oddly truncates when using foreach
  • How to disable all widgets inside Panel or inside Composite?
  • how to avoid repetitive constructor in children
  • Highlight and Bold text in JTextPane
  • how to do an event when i swipe from fragment to the other
  • Akka Routing: Reply's send to router ends up as dead letters
  • unknown Exception android