简体   繁体   中英

Nullpointer exception while connecting to DB using MyBatis

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)?

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);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM