DefaultSqlSession.close 抛出 NullPointerException: null 问题的处理方法

  • 时间:2021-03-20 20:58 作者:deniro 来源: 阅读:719
  • 扫一扫,手机访问
摘要:1 问题形容集成了 MyBatis3.x 后,加入 MyBatis 自己设置阻拦器。抛出 NullPointerException 异常。完整出错日志如下:java.lang.IllegalStateException: Failed to execute CommandLineRunner at o

1 问题形容

集成了 MyBatis3.x 后,加入 MyBatis 自己设置阻拦器。抛出 NullPointerException 异常。

完整出错日志如下:

java.lang.IllegalStateException: Failed to execute CommandLineRunner at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:798) [spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE] at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:779) [spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:322) [spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) [spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE] at com.fsti.cc.core.ds.DSApplication.main(DSApplication.java:23) [classes/:na]Caused by: java.lang.NullPointerException: null at org.apache.ibatis.session.defaults.DefaultSqlSession.close(DefaultSqlSession.java:263) ~[mybatis-3.5.6.jar:3.5.6] at org.mybatis.spring.SqlSessionUtils.closeSqlSession(SqlSessionUtils.java:195) ~[mybatis-spring-2.0.6.jar:2.0.6] at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:449) ~[mybatis-spring-2.0.6.jar:2.0.6] at com.sun.proxy.$Proxy55.selectList(Unknown Source) ~[na:na] at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:224) ~[mybatis-spring-2.0.6.jar:2.0.6] at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:147) ~[mybatis-3.5.6.jar:3.5.6] at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:80) ~[mybatis-3.5.6.jar:3.5.6] at org.apache.ibatis.binding.MapperProxy$PlainMethodInvoker.invoke(MapperProxy.java:152) ~[mybatis-3.5.6.jar:3.5.6] at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:85) ~[mybatis-3.5.6.jar:3.5.6] at com.sun.proxy.$Proxy56.selectAll(Unknown Source) ~[na:na] at com.fsti.cc.core.ds.DSApplication.run(DSApplication.java:28) [classes/:na] at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:795) [spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE] ... 5 common frames omitted

自己设置的 MyBatis 阻拦器采用 Spring 注解 @Bean 注入:

@Configurationpublic class MybatisInterceptorConfig {// @Bean// public Interceptor getInterceptor(){// return new UpdateSqlInterceptor();// } @Bean public String addInterceptors(SqlSessionFactory sqlSessionFactory) { sqlSessionFactory.getConfiguration().addInterceptor(new UpdateSqlInterceptor()); return "MybatisInterceptor"; }}

这里实际上采用了两种注入方式,实验后发现这两种方式都会抛出同样的异常。

2 起因分析

在自己设置 MyBatis 阻拦器时,需要实现 plugin 方法,假如使用 IDE 生成的方式就会默认返回 null:

最终导致Executor 对象为 null,所以 MyBatis 在尝试关闭 SqlSession 时,抛出空指针异常。

所以跟我们之前所用的阻拦器注入方式没关系。更便捷的方式是直接在阻拦器上使用 @Component 注解。形如:

@Component@Intercepts( @Signature( type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class} ))

3 问题处理

参考 Interceptor 类的 plugin 实现方法,使用 Plugin.wrap 包装后再返回回来:

  • 全部评论(0)
最新发布的资讯信息
【系统环境|】2FA验证器 验证码如何登录(2024-04-01 20:18)
【系统环境|】怎么做才能建设好外贸网站?(2023-12-20 10:05)
【系统环境|数据库】 潮玩宇宙游戏道具收集方法(2023-12-12 16:13)
【系统环境|】遥遥领先!青否数字人直播系统5.0发布,支持真人接管实时驱动!(2023-10-12 17:31)
【系统环境|服务器应用】克隆自己的数字人形象需要几步?(2023-09-20 17:13)
【系统环境|】Tiktok登录教程(2023-02-13 14:17)
【系统环境|】ZORRO佐罗软件安装教程及一键新机使用方法详细简介(2023-02-10 21:56)
【系统环境|】阿里云 centos 云盘扩容命令(2023-01-10 16:35)
【系统环境|】补单系统搭建补单源码搭建(2022-05-18 11:35)
【系统环境|服务器应用】高端显卡再度登上热搜,竟然是因为“断崖式”的降价(2022-04-12 19:47)
手机二维码手机访问领取大礼包
返回顶部