Configure Spring boot multiple data source :not working

I have a requirement to configure one DB for Camunda Engine and another for Business entities.

I did changes as mentioned in the doc
https://docs.camunda.org/manual/7.13/user-guide/spring-boot-integration/configuration/

@Configuration
@EnableTransactionManagement
public class DatabaseConfiguration {

@Bean
@Primary
@ConfigurationProperties(prefix = "datasource.primary")
public DataSourceProperties primaryDataSource() {
	return new DataSourceProperties();
}

@Bean(name = "camundaBpmDataSource")
@ConfigurationProperties(prefix = "bpm.datasource")
public BasicDataSource secondDataSource() {
	return DataSourceBuilder.create().type(BasicDataSource.class).build();
}

@Bean
public PlatformTransactionManager transactionManager(@Qualifier("camundaBpmDataSource") DataSource dataSource) {
	return new DataSourceTransactionManager(dataSource);
}

}

Camunda related config DB works but business related DB config using JPA its not working though its not throwing any error. when i query from DB it returns empty results.

For testing i am using H2 for Camunda and mssql for business entity

datasource:
primary:
url: jdbc:jtds:******
username: *****
password: *****
driver-class-name: net.sourceforge.jtds.jdbc.Driver
hikari:
connection-test-query: SELECT 1
tomcat:
test-while-idle: true
validationQuery: SELECT 1
testOnBorrow: true
initial-size: 5
max-active: 10
max-idle: 10
min-idle: 5

camunda:
bpm:
id-generator: strong
database:
type: h2
history-level: full
filter:
create: All Tasks
job-execution:
deployment-aware: true
enabled: true
admin-user:
id: demo
password: demo

bpm:
datasource:
url: “jdbc:h2:mem:camunda;DB_CLOSE_DELAY=1000”
username: sa
password:
driverClassName: org.h2.Driver

Are you sure you’ve provided a transaction manager for you business DB?

yes, by default it is checking under camunda DB i think

@gowtham_m We are facing the same problem. Did you find a proper solution to this?

@mpochert Below one worked for me

package com.abc.xyz;

import java.util.HashMap;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = “com.abc.xyz”, entityManagerFactoryRef = “primaryEntityManager”, transactionManagerRef = “primaryTransactionManager”)

public class DatabaseConfiguration {

@Value("${spring.datasource.url}")
private String dbSourceUrl;

@Value("${spring.datasource.username}")
private String dbUserName;

@Value("${spring.datasource.password}")
private String dbPassword;

@Bean
@Primary
public LocalContainerEntityManagerFactoryBean primaryEntityManager() {
	LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
	em.setDataSource(primaryDataSource());
	em.setPackagesToScan(new String[] { "com.abc.xyz" });
	HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
	em.setJpaVendorAdapter(vendorAdapter);
	HashMap<String, Object> properties = new HashMap<>();
	properties.put("hibernate.hbm2ddl.auto", "none");
	em.setJpaPropertyMap(properties);
	return em;
}

@Primary
@Bean
public DataSource primaryDataSource() {

	DriverManagerDataSource dataSource = new DriverManagerDataSource();
	dataSource.setDriverClassName("net.sourceforge.jtds.jdbc.Driver");
	dataSource.setUrl(dbSourceUrl);
	dataSource.setUsername(dbUserName);
	dataSource.setPassword(dbPassword);
	return dataSource;
}

@Primary
@Bean
public PlatformTransactionManager primaryTransactionManager() {

	JpaTransactionManager transactionManager = new JpaTransactionManager();
	transactionManager.setEntityManagerFactory(primaryEntityManager().getObject());
	return transactionManager;
}

@Bean(name = "camundaBpmDataSource")
@ConfigurationProperties(prefix = "bpm.datasource")
public BasicDataSource secondDataSource() {
	return DataSourceBuilder.create().type(BasicDataSource.class).build();
}

@Bean
public PlatformTransactionManager transactionManager(@Qualifier("camundaBpmDataSource") DataSource dataSource) {
	return new DataSourceTransactionManager(dataSource);
}

}

1 Like

Thanks for posting your solution @gowtham_m