解决问题记录-Cause: org.apache.ibatis.executor.ExecutorException: Executor was closed
2020-06-28 05:00
106 查看
解决问题:Cause: org.apache.ibatis.executor.ExecutorException: Executor was closed
问题意思:执行者被关闭了
在原有的项目上进行改造,使用Mybatis注解进行开发。在原有的代码上进行修改。
private static StudentDao studentDao = null; static { InputStream resourceAsStream = null; SqlSessionFactory build = null; SqlSession sqlSession = null; try { resourceAsStream = Resources.getResourceAsStream("MyBatisConfig.xml"); build = new SqlSessionFactoryBuilder().build(resourceAsStream); sqlSession = build.openSession(); studentDao = sqlSession.getMapper(StudentDao.class); } catch (IOException e) { e.printStackTrace(); } }
看了挺久发现,使用的是静态代码块,那么这些代码只会在类加载时执行唯一一次,在调用studentDao方法之后就进行了关闭sqlSession,那么后续再一次调用,sqlSession已经被关闭了,而且不会创建新的sqlSession,只会在类加载时创建唯一一次,所以才会报Executor was closed 错误。
解决
private static StudentDao studentDao = null; private static SqlSession sqlSession= null; private static SqlSessionFactory build = null; static { try { InputStream resourceAsStream = Resources.getResourceAsStream("MyBatisConfig.xml"); build = new SqlSessionFactoryBuilder().build(resourceAsStream); } catch (IOException e) { e.printStackTrace(); } } @Override public List<Student> findAll() { sqlSession = build.openSession(); studentDao = sqlSession.getMapper(StudentDao.class); ArrayList<Student> all = studentDao.findAll(); sqlSession.close(); return all; }
在每个方法中获取SqlSession对象,使用完成之后归还到连接池中。在静态代码块中,工厂对象只会创建一次,你可以在方法中创建SqlSession对象,根据不同的接口动态获取不同的代理对象,更加灵活,也不会出现Executor was closed 错误。
只作为个人记录,如有帮助不胜荣幸。
相关文章推荐
- 解决org.apache.ibatis.binding.BindingException: Invalid bound statement (not found) 问题
- 学习mybatis框架中遇到org.apache.ibatis.exceptions.PersistenceException: 问题该怎么解决
- 完美解决org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)问题大全
- 学习mybatis框架中遇到org.apache.ibatis.exceptions.PersistenceException: 问题解决
- 解决org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)问题 静态资源过滤
- 【问题解决】Maven搭建Web项目中org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)问
- 解决org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)问题
- org.apache.ibatis.binding.BindingException: Parameter 'XXXX' not found.的问题解决办法
- 学习mybatis框架中遇到org.apache.ibatis.exceptions.PersistenceException: 问题该怎么解决
- 解决org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)问题
- 关于org.apache.ibatis.binding.BindingException: Invalid bound statement (not found),找不到mapper文件问题解决
- 解决org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)问题
- SpringBoot问题之org.apache.ibatis.executor.ExecutorException: No constructor found in...的问题
- 解决org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)问题
- org.apache.ibatis.binding.BindingException 奇葩问题解决
- org.apache.ibatis.binding.BindingException: Parameter 'XXXX' not found.的问题解决办法
- MyBatis框架中遇到org.apache.ibatis.exceptions.PersistenceException: 问题解决了
- org.apache.ibatis.binding.BindingException: Parameter 'xxxxx' not found.问题解决
- SpringBoot问题之org.apache.ibatis.executor.ExecutorException: No constructor found in...的问题
- MyBatis3.4.0以上的分页插件错误:Could not find method on interface org.apache.ibatis.executor.statement.StatementHandler named prepare. Cause: java.lang.NoSuchMethodException: org.apache.ibatis.executor.stateme