当前位置:网站首页>The thread reuse problem of PageHelper using ThreadLocal, did you use it correctly?
The thread reuse problem of PageHelper using ThreadLocal, did you use it correctly?
2022-07-03 01:28:00 【Morning Xi light】
Preface
PageHelper It is a more commonly used paging plug-in , By implementing Mybatis Of Interceptor The interface is complete query sql Dynamic paging , The paging parameter is determined by ThreadLocal Preservation .
ordinary Paging execution process :
Set up page Parameters
perform query Method
Interceptor Interface Intermediate calibration ThreadLocal Whether there is a set page Parameters
There is page Parameters , To regenerate the count sql and page sql, And execute the query . non-existent page Parameters , Go straight back to Query results
perform LOCAL_PAGE.remove() eliminate page Parameters
Problem scenario
Observe the above execution process , You can find , If in the first place 1 And the first step 2 Step There is an exception between , that LOCAL_PAGE Corresponding to the current thread in page Parameters do not remove.
Without using thread pools , The current thread will be destroyed after execution , At this time Current thread Medium threadLocals Parameters Will be affected by the situation , It's empty 了 LOCAL_PAGE in Of the current thread page Parameters .
But if a thread pool is used , The current thread has finished executing , It's not going to be destroyed , Instead, the current thread will be stored in the pool again , Mark as idle , For later use . When using this thread later , because Threads Of threadLocals The value still exists , Even though we're in the third place 1 Step is not set page Parameters , The first 3 Step You can also get page Parameters , To generate count sql and page sql, Thus affecting our normal query .
SpringBoot Built in... Will be used in the project Tomcat As a server , and Tomcat Threads will be used by default to process requests , This leads to the above problems .
Solution
because Tomcat Threads are used to handle request request , So when the request is completed , Clear the current thread threadLocals Property value , Which is execution LOCAL_PAGE.remove() that will do .
Realization way :
Use aop, For all controller To deal with
Realization HandlerInterceptor perhaps WebRequestInterceptor Yes request Interceptor interface requested , adopt afterCompletion Method execution LOCAL_PAGE.remove() .— recommend , The implementation is simple
The second way is used here , Realization HandlerInterceptor Interface :
public class PageLocalWebInterceptor implements HandlerInterceptor {
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
// PageHelper.clearPage() Internal calls LOCAL_PAGE.remove()
PageHelper.clearPage();
}
}
Define configuration class , The configuration class needs to implement WebMvcConfigurer Interface completion for WebMvc Related configuration of , register PageLocalWebInterceptor :
@Configuration
public class FrameworkAutoConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new PageLocalWebInterceptor());
}
}
边栏推荐
- 2022 coal mine gas drainage examination question bank and coal mine gas drainage examination questions and analysis
- Uniapp component -uni notice bar notice bar
- d. LDC build shared library
- Matlab finds the position of a row or column in the matrix
- 按键精灵打怪学习-多线程后台坐标识别
- Why can't the start method be called repeatedly? But the run method can?
- Button wizard play strange learning - automatic return to the city route judgment
- On Fibonacci sequence
- 看疫情之下服装企业如何顺势而为
- 音程的知识的总结
猜你喜欢
![[机缘参悟-36]:鬼谷子-飞箝篇 - 面对捧杀与诱饵的防范之道](/img/c6/9aee30cb935b203c7c62b12c822085.jpg)
[机缘参悟-36]:鬼谷子-飞箝篇 - 面对捧杀与诱饵的防范之道

MySQL

Detailed explanation of Q-learning examples of reinforcement learning

C#应用程序界面开发基础——窗体控制(3)——文件类控件

C application interface development foundation - form control (2) - MDI form

Wireshark data analysis and forensics a.pacapng
![[flutter] icons component (fluttericon Download Icon | customize SVG icon to generate TTF font file | use the downloaded TTF icon file)](/img/ca/1d2473ae51c59b84864352eb17de94.jpg)
[flutter] icons component (fluttericon Download Icon | customize SVG icon to generate TTF font file | use the downloaded TTF icon file)

MySQL基础用法02

MySQL

看完这篇 教你玩转渗透测试靶机Vulnhub——DriftingBlues-9
随机推荐
给你一个可能存在 重复 元素值的数组 numbers ,它原来是一个升序排列的数组,并按上述情形进行了一次旋转。请返回旋转数组的最小元素。【剑指Offer】
Canvas drawing -- bingdd
Key wizard hit strange learning - automatic path finding back to hit strange points
基本远程连接工具Xshell
The difference between tail -f, tail -f and tail
MySQL foundation 07-dcl
Button wizard play strange learning - automatic return to the city route judgment
Is there anything in common between spot gold and spot silver
看完这篇 教你玩转渗透测试靶机Vulnhub——DriftingBlues-9
Appuyez sur l'apprentissage de l'esprit de frappe - reconnaissance des coordonnées de fond multithreadées
不登陆或者登录解决oracle数据库账号被锁定。
Daily topic: movement of haystack
Kivy tutorial - example of using Matplotlib in Kivy app
对非ts/js文件模块进行类型扩充
2022 cable crane driver examination registration and cable crane driver certificate examination
C#应用程序界面开发基础——窗体控制(1)——Form窗体
Work experience of a hard pressed programmer
Database SQL language 01 where condition
[shutter] animation animation (the core class of shutter animation | animation | curvedanimation | animationcontroller | tween)
Do not log in or log in to solve the problem that the Oracle database account is locked.