将原有的循环单线程操作改下成多线程后发现线程意外停止,百思不得其解。与同事合作debug一天半后,终于解决问题。
报错信息如下:
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: interrupt
java.lang.NullPointException
报错的类为:
SpringContextUtils、AutoLogAspect、IPUtils
原因分析:
使用多线程,在多线程内部调用的方法有这样一个注解
@AutoLog(value = "XXXX"),这个注解是jeecg用来进行自动日志记录的。
而日志会使用到切面,所有报错的类中有AutoLogAspect,进入AutoLogAspect中查看报错的行
HttpServletRequest request = SpringContextUtils.getHttpServletRequest();调用了jeecg自己的SpringContextUtils获取上下文。
再进入SpringContextUtils,它使用
HttpServletRequest httpServletRequest=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();来获取http信息。
对,jeecg的日志框架要求获取到操作的ip,而多线程,HttpServletRequest只有第一个开始线程才能获取到,后续都是空的,ip工具类没办法从HttpServletRequest获取ip,自然就会报错空指针。
而Mybatis的报错应该是线程的中断导致的。
解决方法
在线程创建前,获取HttpServletRequest,将其设为线程共享。具体分析可以查看https://blog.csdn.net/sanyaoxu_2/article/details/98744119
在线程创建前,加上下面这段共享ServletRequestAttributes的代码。
ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
RequestContextHolder.setRequestAttributes(sra, true);