MyBatis CDI – Multi tenant

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

MyBatis CDI – Multi tenant

Homero Villarreal
Hi guys,

I’m trying to set up a multi-tenant application with only MyBatis and CDI.


1. I have a single mybatis-config.xml with multiple environments that will acting like a tenant.
2. The tenant configuration will be stored in a RequestScoped bean. In order to be injected and retrieve the requested tenant. This is selected after the user is logged in.
3. I want the daos/repositories to be as follow, using an unnamed session and without some special annotation.


public class TestRepository {
    private SqlSession session;

    public List<String> getData()
        return session.selectList(....);

4. I created a dynamic producer in order to retrieve to required tenant. However, I stored all the SqlSessionFactory objects created using the SqlSessionFactoryBuilder in a temporary HashMap, but isn't a proxy bean.


    public SqlSessionFactory produce() {
        return resolver.getSession("example");

With this, I got the following message twice, first when the SqlSessionFactory is build, Secondly, when the sentence session.selectList is executed or any operation: PooledDataSource forcefully closed/removed all connections.

Just a couple of questions.
  1. Is it possible to register a @SessionFactoryProvider producer dynamically in MyBatis?
  2. Is it possible to setup and add the SqlSessionFactory without a producer?
  3. Do you have any recommendations or some guidelines?


You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit