设为首页 加入收藏

TOP

深入 Spring Boot:排查 @Transactional 引起的 NullPointerException(一)
2018-01-12 06:06:50 】 浏览:763
Tags:深入 Spring Boot 排查 @Transactional 引起 NullPointerException

写在前面

这个demo来说明怎么排查一个@Transactional引起的NullPointerException。

https://github.com/hengyunabc/spring-boot-inside/tree/master/demo-Transactional-NullPointerException

定位 NullPointerException 的代码

Demo是一个简单的spring事务例子,提供了下面一个StudentDao,并用@Transactional来声明事务:

@Component
@Transactional
public class StudentDao {

    @Autowired
    private SqlSession sqlSession;

    public Student selectStudentById(long id) {
        return sqlSession.selectOne("selectStudentById", id);
    }

    public final Student finalSelectStudentById(long id) {
        return sqlSession.selectOne("selectStudentById", id);
    }
}

应用启动后,会依次调用selectStudentById和finalSelectStudentById:

    @PostConstruct
    public void init() {
        studentDao.selectStudentById(1);
        studentDao.finalSelectStudentById(1);
    }

用mvn spring-boot:run 或者把工程导入IDE里启动,抛出来的异常信息是:

Caused by: java.lang.NullPointerException
    at sample.mybatis.dao.StudentDao.finalSelectStudentById(StudentDao.java:27)
    at com.example.demo.transactional.nullpointerexception.DemoNullPointerExceptionApplication.init(DemoNullPointerExceptionApplication.java:30)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:366)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:311)

为什么应用代码里执行selectStudentById没有问题,而执行finalSelectStudentById就抛出NullPointerException?

同一个bean里,明明SqlSession sqlSession已经被注入了,在selectStudentById里它是非null的。为什么finalSelectStudentById函数里是null?

获取实际运行时的类名

当然,我们对比两个函数,可以知道是因为finalSelectStudentById的修饰符是final。但是具体原因是什么呢?

我们先在抛出异常的地方打上断点,调试代码,获取到具体运行时的class是什么:

System.err.println(studentDao.getClass());

打印的结果是:

class sample.mybatis.dao.StudentDao$$EnhancerBySpringCGLIB$$210b005d

可以看出是一个被spring aop处理过的类,但是它的具体字节码内容是什么呢?

dumpclass分析

我们使用dumpclass工具来把jvm里的类dump出来:

https://github.com/hengyunabc/dumpclass

wget http://search.maven.org/remotecontent?filepath=io/github/hengyunabc/dumpclass/0.0.1/dumpclass-0.0.1.jar -O dumpclass.jar

找到java进程pid:

$ jps
5907 DemoNullPointerExceptionApplication

把相关的类都dump下来:

sudo java -jar dumpclass.jar 5907 'sample.mybatis.dao.StudentDao*' /tmp/dumpresult

反汇编分析

用javap或者图形化工具jd-gui来反编绎sample.mybatis.dao.StudentDao$$EnhancerBySpringCGLIB$$210b005d。

反编绎后的结果是:

  1. class StudentDao$$EnhancerBySpringCGLIB$$210b005d extends StudentDao
  2. StudentDao$$EnhancerBySpringCGLIB$$210b005d里没有finalSelectStudentById相关的内容
  3. selectStudentById实际调用的是this.CGLIB$CALLBACK_0,即MethodInterceptor tmp4_1,等下我们实际debug,看具体的类型
  public final Student selectStudentById(long paramLong)
  {
    try
    {
      MethodInterceptor tmp4_1 = this.CGLIB$CALLBACK_0;
      if (tmp4_1 == null)
      {
        tmp4_1;
        CGLIB$BIND_CALLBACKS(this);
      }
      Method
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇G1 垃圾收集器介绍 下一篇Java Proxy 和 CGLIB 动态代理原理

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目