cancel
Showing results for 
Search instead for 
Did you mean: 

Multiple Data sources in activiti

davanna
Champ in-the-making
Champ in-the-making
I have written a spring boot application. I have a two data sources - one is used by activiti engine for it's database and another datasource for updating my tables. The database updates to my tables are done in the event listeners. I have configured both the data sources as follows. The code is working fine but I do sense that there may be issues here as the same default transaction manager is used by activiti engine to update it's tables and also to update my tables. This transaction manager is the JpaTransactionManager. The datasource field in the transactionmanager is pointing to my database as I have defined it as primary. The transactionfactory defined in SpringProcessEngineConfiguration is pointing to activiti datasource. The transactionFactory is used by the DbSqlSession in mybatis. I am trying to understand how Spring is updating the tables properly here as the datasources are different in both the cases.


<code language="java">

@Configuration
public class ActivitiConfiguration extends AbstractProcessEngineAutoConfiguration {

   @Primary
   @Bean(name = "myDataSource")
   @ConfigurationProperties(prefix = "spring.datasource.mine")
   public DataSource getMyDataSource() {
      return DataSourceBuilder.create().build();
   }

   @Bean(name = "activitiDataSource")
   @ConfigurationProperties(prefix = "spring.datasource.activiti")
   public DataSource getActivitiDataSource() {
      return DataSourceBuilder.create().build();
   }

   @Primary
   @Bean(name = "transactionManager")
   public PlatformTransactionManager transactionManager(@Qualifier("myDataSource") DataSource dataSource) {
      JpaTransactionManager transactionManager = new JpaTransactionManager();
      transactionManager.setDataSource(dataSource);
      return transactionManager;
   }

   @Bean
   public SpringProcessEngineConfiguration springProcessEngineConfiguration(PlatformTransactionManager activitiTransactionManager,
         SpringAsyncExecutor springAsyncExecutor, @Qualifier("activitiDataSource") DataSource dataSource) throws IOException {

      return baseSpringProcessEngineConfiguration(dataSource, activitiTransactionManager, springAsyncExecutor);
   }

}

</code>
3 REPLIES 3

jbarrez
Star Contributor
Star Contributor
If you have two datasource, you need XA transactions to be configured. A regular JPATransactionManager is not going to cut it.

davanna
Champ in-the-making
Champ in-the-making
Thanks for the reply. I finally ended using the JTATransaction. But the above code works perfectly fine with my integration tests. Ideally with TransactionMaanger pointing to first datasource and working on second datasource, the db operations on second datasoruce must fail. It is not happening here. Just want to validate my theory that mybatis using second datasource is using the default autocommit feature and the transaction enclosing this operation is a no-op.

jbarrez
Star Contributor
Star Contributor
With two datasources, a regular transactionmanager won't cut it. When you involve multiple databases, you generally need XA transactions.