Multi-tenant MyBatis SqlSessionFactory support

Greetings guys,

I would like to ask how is it possible to configure custom MyBatis configuration when I have multi-tenancy enabled in Camunda. We use PostgreSQL and multi-tenancy is configured in that way it uses schema as tenant discriminator.

We have followed the manual but when we execute a query using custom MyBatis SQL factory it always falls to default tenant. I am wondering how you build proper connection string with schema included. Is there a special way?

Here is the code:

private Properties getSqlSessionFactoryProperties(ProcessEngineConfigurationImpl conf) {
	Properties properties = new Properties();		
	properties.put("prefix", conf.getDatabaseTablePrefix());
	ProcessEngineConfigurationImpl.initSqlSessionFactoryProperties(properties, conf.getDatabaseTablePrefix(), conf.getDatabaseType());
	return properties;
}

private SqlSessionFactory createMyBatisSqlSessionFactory(InputStream config) {
	ProcessEngine processEngine = ProcessEngines.getProcessEngine("acme");
	ProcessEngineConfiguration processEngineConfiguration = processEngine.getProcessEngineConfiguration();
	
	//processEngineConfiguration.setJdbcUrl(processEngineConfiguration.getJdbcUrl()+"?searchpath=acme");
	DataSource dataSource = processEngineConfiguration.getDataSource();
	TransactionFactory transactionFactory = new ManagedTransactionFactory();

	Environment environment = new Environment("customTasks", transactionFactory, dataSource);

	XMLConfigBuilder parser = new XMLConfigBuilder( //
			new InputStreamReader(config), //
			"", // set environment later via code
			getSqlSessionFactoryProperties((ProcessEngineConfigurationImpl) processEngineConfiguration));
	Configuration configuration = parser.getConfiguration();
	configuration.setEnvironment(environment);
	configuration = parser.parse();
	
	configuration.setDefaultStatementTimeout(processEngineConfiguration.getJdbcStatementTimeout());

	SqlSessionFactory sqlSessionFactory = new DefaultSqlSessionFactory(configuration);
	return sqlSessionFactory;
}

Thank you in advance.

Best regards,
Laco

OK, so I reply to myself :slight_smile: The solution lies in knowing how to correctly do the mapping in this case - http://www.mybatis.org/mybatis-3/sqlmap-xml.html .

Once we included ${prefix} in front of all table names, it started to work correctly - which is logical…

1 Like