在运行一个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对象,这样就解决了上述问题。

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

被折叠的 条评论
为什么被折叠?



