首页 > 其他分享 >不规范使用PageHelper导致线程污染出现报错

不规范使用PageHelper导致线程污染出现报错

时间:2023-03-03 12:00:38浏览次数:33  
标签:clearPage spring LIMIT PageHelper jdbc 线程 报错

问题复现:

在原有项目基础上开发获取最新的第一个模板的接口。接口中只有一个查询sql:select x from x where x limit 1。

调试总是报错:

org.springframework.jdbc.BadSqlGrammarException: 
### Error querying database.  Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 10000' at line 5
### The error may exist in class path resource [mapper/AlipayCardTemplateMapper.xml]
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: select         template_id,callback         from alipay_card_template         order by pkno desc         limit 1 LIMIT ?
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 10000' at line 5
; bad SQL grammar []; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 10000' at line 5
	at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:234) ~[spring-jdbc-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
	at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72) ~[spring-jdbc-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
	at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73) ~[mybatis-spring-1.3.2.jar!/:1.3.2]
	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446) ~[mybatis-spring-1.3.2.jar!/:1.3.2]
	at com.sun.proxy.$Proxy128.selectOne(Unknown Source) ~[na:na]
	at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:166) ~[mybatis-spring-1.3.2.jar!/:1.3.2]

发现打印sql语句,莫名后面多追加了LIMIT 10000导致报错,可能是其他地方分页影响到了,ThreadLocal线程污染。

最后排查,找到是有一个接口前面使用PageHelper.startPage(page, size);,后面并没有mapper查询,而是调用的远程接口查询。并且经过前后端搜索,发现pageSize=10000的参数只有这个接口。

很快定位接口,并修改:

在原有逻辑上加上try catch 并在finally中手动clearPage。

try {
            PageHelper.startPage(page, size);
            // 原有逻辑调用远程接口查询
            。。。
            PageInfo<String> pageInfo = new PageInfo<>(cusTags);
            。。。
            
        }catch (Exception e){
            log.error("queryCusTags,异常:",e);
            return new ResponseObjectResult(new ResponseStatus(ResultCode.FAIL));
        }finally {
            /**
             * 1.原因:分页后面没有紧跟mapper调用,而是调用远程接口返回列表。mybatis没有走拦截器不会自动PageHelper.clearPage()。需要手动PageHelper.clearPage();
             * 否则会出现线程污染导致报错(getTemplateInfo()莫名后面多追加了LIMIT 10000导致报错,可能是其他地方分页影响到了,ThreadLocal线程污染。)。
             * 2.复现:开发环境/getTemplateInfo接口去掉PageHelper.clearPage()。手机页面连续点击领取会员卡(调用的是/getTemplateInfo接口),postman同时连续请求/queryCusTags。
             * 发现/getTemplateInfo接口报错BadSqlGrammarException:limit 1 LIMIT ? 。多追加了个LIMIT ?。
             * 3.修改为此处在finally中手动clear。再次重复步骤2多次,未再出现连续点击‘领取会员卡’按钮报错。
             */
            PageHelper.clearPage();
        }    

 修改后再次测试多遍,未发现错误。

看项目中有好几个地方在查询前都主动clearPage。估计是没找到产生的根源在哪儿,查询前加clearPage最简单有效。如果找到根源,那么其他地方完全不用加clearPage也不会出现报错。

 

标签:clearPage,spring,LIMIT,PageHelper,jdbc,线程,报错
From: https://www.cnblogs.com/super-chao/p/17175108.html

相关文章

  • npm vue-router安装报错
    因为2022年2月7日以后,vue-router的默认版本,为4版本,而且vue-router4,只能在vue3中,只有vue-router3中,能用在vue2中如果把vue-router4强制安装到vue2中,则会报上面的错误;解......
  • 全局视角看技术-Java多线程演进史
    作者:京东科技文涛全文较长共6468字,语言通俗易懂,是一篇具有大纲性质的关于多线程的梳理,作者从历史演进的角度讲了多线程相关知识体系,让你知其然知其所以然。前言2022......
  • linux下进程和线程的区别和联系
    进程用fork()或者vfork()生成(vfork是专门为了加载其他程序的子程序而优化的,随着fork()的优化,vfork已经被优化)fork()生成的子进程与父进程共享代码区内存,对于其他内存fork......
  • Yolov5环境报错解决:No labels found in 与 Could not run 'torchvision::nms' with ar
    问题记录yolov5环境1Nolabelsfoundin(Done)报错内容F:\WorkSpace\GitSpace\yolov5>pythontrain-self.pytrain-self:weights=weights/yolov5s.pt,cfg=models/y......
  • Pandas使用时间索引筛选时报错 AssertionError: <class 'numpy.ndarray'>
    Icameacrossthesimilarproblem,mysolutionwas:makesuretheindexistypeof'DatetimeIndex',Idothis:df.index=pd.to_datetime(df.index)sortedth......
  • 线程礼让
    线程礼让指让当前正在执行的线程暂停,但不阻塞。将线程从运行状态转为就绪状态。让cpu重新调度,礼让不一定成功。 //线程礼让//礼让不一定成功,看cpu心情publicclassT......
  • Kali更新源报错处理
    kaliaptupdate更新源报错:Certificateverificationfailed:ThecertificateisNOTtrusted.Thecertificateissuerisunknown.Couldnothandshake:Errorinth......
  • 线程停止的方法
    使用一个标志位进行终止,当flag=false时,终止线程运行privatebooleanflag=true;@Overridepublicvoidrun(){inti=0;while(flag){......
  • C++11/std::atomic - 原子变量(不加锁实现线程互斥)
     文章目录1原子操作2C++11原子变量3使用原子变量3.1没有使用线程互斥的数据操作3.2使用互斥量保证线程互斥3.3使用原子量std::atomic保证数据互斥......
  • GitLab修改群组报错“路径已经被使用”
    GitLab修改群组报错“路径已经被使用” 1、问题背景在GitLab修改群组时,遇到如下报错“路径已经被使用” 2、原因分析根据报错提示信息,新建的群组路径已经被占用......