Search code examples
javaormosgimybatisibatis

MyBatis+OSGi: how to remove mappers dynamically


I have and OSGi architecture and I use xml mappers+interfaces (as it's recommended).

Mybatis configuration file I put in glassfish/domains/domain1/config folder. This configuration is loaded when MyBatisConnectionFactory is called first time. That's ok.

Now every bundle has its own mappers. I can add them dynamically by putting

com.acme.myproject.MyMapper and com/acme/myproject/MyMapper.xml into relative folders and having in Activator

public void start(BundleContext context) throws Exception {
    getSqlSessionFactory().getConfiguration().addMapper(MyMapper.class);
}

It seems to work. When my bundle starts mappers are added to configuration. The problem is that if I update bundle changes are not taken into effect till I restart my glassfish domain. As I understand I must firstly remove previous mappers but can't find the way to do it. Please, help me.


Solution

  • Looks like this was opened as a MyBatis issue years ago and closed as won't-fix, but they did provide a workaround: http://code.google.com/p/mybatis/issues/detail?id=98.

    The code link doesn't work, but the conceptual workaround remains, and the file in question can likely be tracked down within log4j.

    Here is one place where the code is stored, which appears it's from Maven Central: http://grepcode.com/file/repo1.maven.org/maven2/log4j/log4j/1.2.14/org/apache/log4j/helpers/FileWatchdog.java

    In short, they recommend:

    You just need to reload your SqlSessionFactory calling the SqlSessionFactoryBuilder.build when you detect the file has changed.