### Error querying database. Cause: org.apache.ibatis.executor.ExecutorException: Executor was clos

本文详细解析了在使用MyBatis框架时遇到的Executor被关闭异常,并提供了具体的解决方案。通过封装sqlSession对象的获取和关闭过程,有效避免了因sqlSession重复使用导致的错误。

在运行一个mybatis程序中出现了如下错误:

信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: org.apache.ibatis.executor.ExecutorException: Executor was closed.
### Cause: org.apache.ibatis.executor.ExecutorException: Executor was closed.
    at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)

 

出现这种错误主要是我们调用一个对象的时候获取的sqlSession对象但是它只创建一次,一旦关闭,后面就会报错

解决方法:我们将它封装起来:

private SqlSession sqlSession;
private SqlSession getSqlSession() {
    sqlSession=SqlSessionFactoryUtils.getSqlSessionFactory().openSession();
    return sqlSession;
}
public List<Users> findAll() {
    try {
        list = getSqlSession().selectList("findAll");
    } catch (Exception ex) {
        ex.printStackTrace();
    }finally {
        sqlSession.close();
    }
    return list;
}

调用私有函数去获得sqlSession对象,这样就解决了上述问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值