SqlSessionFactory injection error with multiple datasources

58 views
Skip to first unread message

TheDeadOne

unread,
Jun 28, 2021, 2:29:54 PM6/28/21
to mybatis-user
In my project with Play Framework and MyBatis-Guice, I have multiple datasources and they work fine. Until I try to inject somewhere SqlSessionFactory. After that I got the error:

Unable to create binding for org.apache.ibatis.session.SqlSessionFactory. It was already configured on one or more child injectors or private modules bound at org.mybatis.guice.MyBatisModule.internalConfigure(MyBatisModule.java:116) (via modules: com.google.inject.util.Modules$OverrideModule -> services.modules.SomeBatisModule -> services.modules.SomeBatisModule$1) bound at org.mybatis.guice.MyBatisModule.internalConfigure(MyBatisModule.java:116) (via modules: com.google.inject.util.Modules$OverrideModule -> services.modules.AnotherBatisModule -> services.modules.AnotherBatisModule$1) If it was in a PrivateModule, did you forget to expose the binding?

How I can fix it?

TheDeadOne

unread,
Jun 29, 2021, 5:22:59 AM6/29/21
to mybatis-user
I don't know if this is correct, but this solution worked:

public class SomeBatisModule extends PrivateModule {
    @Override
    protected void configure() {
        ...

        bind(SqlSessionFactory.class).annotatedWith(Names.named("some-database")).to(SqlSessionFactory.class);
        expose(SqlSessionFactory.class).annotatedWith(Names.named("some-database"));
    }
}

public class SomeService {
    public SomeService(@Named("some-database") SqlSessionFactory sqlSessionFactory) {
        ...
    }
}

понедельник, 28 июня 2021 г. в 21:29:54 UTC+3, TheDeadOne:
Reply all
Reply to author
Forward
0 new messages