I have created a simple Java EE program to experiment with MyBatis but I've hit a problem using the MyBatis CDI module. I have followed the instructions outlined on http://mybatis.github.io/cdi/injection.html, but when my program tries to use MyBatis a MybatisCdiConfigurationException
exception is thrown with the description "There are no SqlSessionFactory producers properly configured."
The code for the SqlSessionFactory I'm using is as follow:
import javax.annotation.Resource;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
import javax.sql.DataSource;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
public class SqlSessionFactoryProvider {
@Resource (name="jdbc/MyDatabase")
DataSource dataSource;
@Produces
@ApplicationScoped
public SqlSessionFactory produceFactory() {
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("development",
transactionFactory, dataSource);
Configuration configuration = new Configuration(environment);
configuration.addMapper(ToDoItemMapper.class);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
.build(configuration);
return sqlSessionFactory;
}
}
I've stepped through the code of the MyBatis CDI module and have found that when the follow line of code in CDIUtils findSqlSessionFactory(...)
is executed it fails find any beans that return SqlSessionFactory.class.
beans = beanManager.getBeans(SqlSessionFactory.class, qualifiers.toArray(new Annotation[]{}));
I've tried running my program on GlassFish 4 and 4.1 and they both have the same problem, but it works find if I run my program on WildFly 8.1. My first thought was that it might be a problem with the version of Weld shipped with Glassfish but it turns out that GlassFish 4.1 is running Weld version 2.2.2 whereas WildFly is running Weld version 2.1.2.
Is there anything I can do in GlassFish, my code or in the MyBatis CDI module to overcome this problem when running in GlassFish? Alternatively can anyone suggest anything that I can do to investigate the problem further please?