1

I am having the following issue: when I am connecting to db using MyBatis I get an NPE.

Here is the class for setting connection:

public class SetDBConnection {

    private Mapper mapper;
    private SqlSession session;
    private Configuration configuration;

    public SetDBConnection() {


        configuration = new Configuration();
        configuration.setJdbcTypeForNull(JdbcType.VARCHAR);
        configuration.setLogImpl(Slf4jImpl.class);
        configuration.setDefaultExecutorType(ExecutorType.REUSE);
        configuration.addMapper(Mapper.class);

        Properties properties = null;
        try {
            properties = readProperties();
        } catch (IOException e) {
            e.printStackTrace();
        }
        configuration.setVariables(properties);

    }


    public Mapper openSession() {

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
        session = sqlSessionFactory.openSession();

        mapper = session.getMapper(Mapper.class);
        return mapper;
    }

    public void closeSession() {
        session.commit();
        session.close();
    }

    private Properties readProperties() throws IOException {
        Properties properties = new Properties();
        InputStream inputStream = getClass().getClassLoader()
                .getResourceAsStream("connection.properties");


        if (inputStream != null) {
            properties.load(inputStream);
        } else {
            throw new FileNotFoundException("property file  not found in the classpath");
        }
        inputStream.close();

        return properties;
    }
}

And here I call it and try to Insert data SetDBConnection conn = new SetDBConnection();

    Person p = new Person();
    p.setName("Alex");
    p.setLastName("Bondar");
    p.setTelephone("+79267157256");
    p.setPersonalId("777-216");
    Mapper mapper=conn.openSession();
    try {
        System.out.println("All set to go");
        mapper.saveOrUpdatePerson(p);
    } finally {
        conn.closeSession();
    }

Stacktrace is the following:

Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException: 
### Error opening session.  Cause: java.lang.NullPointerException
### The error may exist in org/abondar/experimental/mybatisdemo/mappers/Mapper.java (best guess)
### Cause: java.lang.NullPointerException
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
    at org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSessionFromDataSource(DefaultSqlSessionFactory.java:100)
    at org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSession(DefaultSqlSessionFactory.java:47)
    at org.abondar.experimental.mybatisdemo.SetDBConnection.openSession(SetDBConnection.java:51)
    at org.abondar.experimental.mybatisdemo.Main.main(Main.java:19)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.lang.NullPointerException
    at org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSessionFromDataSource(DefaultSqlSessionFactory.java:95)
    ... 8 more

What can be wrong and is there any way not to re-establish DB-connection for every action(select,insert or delete)?

5
  • 2
    Where do you get the NPE? Can you post the stacktrace? Commented Feb 3, 2016 at 12:56
  • inputStream.close(); is outside the null check Commented Feb 3, 2016 at 13:07
  • @cricket_007 it's not the source of problem. Commented Feb 3, 2016 at 13:10
  • Well, looking at the stack trace you posted, it happens on line 51 of SetDBConnection. We can't see line numbers. What line is that, then? Commented Feb 3, 2016 at 13:17
  • @cricket_007 that's the line 51: session = sqlSessionFactory.openSession(); Commented Feb 3, 2016 at 13:23

2 Answers 2

4

It seems you have no Environment and TransactionFactory defined. According to the docs you should initialize MyBatis somehow like this:

DataSource dataSource = BlogDataSourceFactory.getBlogDataSource();
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("development", transactionFactory, dataSource); 
Configuration configuration = new Configuration(environment);
Sign up to request clarification or add additional context in comments.

2 Comments

But what is BlogDataSourceFactory?
It's the DataSourceFactory-Implementation used in the example. As stated in the docs "You can plug any 3rd party DataSource by implementing the interface org.apache.ibatis.datasource.DataSourceFactory". Essential is that you have a valid DataSource at this point.
0

Thanks Frank for DataSource idea. I used default pooled dataSource and this problem looks to be solved

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.