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