当前位置:网站首页>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());
}
}
边栏推荐
- [flutter] icons component (fluttericon Download Icon | customize SVG icon to generate TTF font file | use the downloaded TTF icon file)
- 强化学习 Q-learning 实例详解
- [fh-gfsk] fh-gfsk signal analysis and blind demodulation research
- [day 29] given an integer, please find its factor number
- Cut point of undirected graph
- Arduino dy-sv17f automatic voice broadcast
- Why can't the start method be called repeatedly? But the run method can?
- MySQL - database query - condition query
- Excel calculates the difference between time and date and converts it into minutes
- 数学知识:台阶-Nim游戏—博弈论
猜你喜欢

leetcode 2097 — 合法重新排列数对

wirehark数据分析与取证A.pacapng

Androd gradle's substitution of its use module dependency
![[fh-gfsk] fh-gfsk signal analysis and blind demodulation research](/img/8a/8ca80f51a03341c982d52980c54b01.png)
[fh-gfsk] fh-gfsk signal analysis and blind demodulation research

Niu Ke swipes questions and clocks in
![[技术发展-23]:DSP在未来融合网络中的应用](/img/2e/f39543a18a8f58b1d341ce72cc4427.png)
[技术发展-23]:DSP在未来融合网络中的应用

Using tensorboard to visualize the model, data and training process

dotConnect for PostgreSQL数据提供程序

C#应用程序界面开发基础——窗体控制(1)——Form窗体

How is the mask effect achieved in the LPL ban/pick selection stage?
随机推荐
[Androd] Gradle 使用技巧之模块依赖替换
Test shift right: Elk practice of online quality monitoring
kivy教程之在 Kivy App 中使用 matplotlib 的示例
Key wizard play strange learning - multithreaded background coordinate recognition
What is tone. Diao's story
2022 Jiangxi Provincial Safety Officer B certificate reexamination examination and Jiangxi Provincial Safety Officer B certificate simulation examination question bank
Excel removes the data after the decimal point and rounds the number
【第29天】给定一个整数,请你求出它的因子数
MySQL - database query - condition query
leetcode:701. Insertion in binary search tree [BST insertion]
tail -f 、tail -F、tailf的区别
[shutter] animation animation (the core class of shutter animation | animation | curvedanimation | animationcontroller | tween)
Excel calculates the difference between time and date and converts it into minutes
MySQL
SwiftUI 组件大全之使用 SceneKit 和 SwiftUI 构建交互式 3D 饼图(教程含源码)
[flutter] icons component (fluttericon Download Icon | customize SVG icon to generate TTF font file | use the downloaded TTF icon file)
Look at how clothing enterprises take advantage of the epidemic
机器学习术语
Expérience de recherche d'emploi d'un programmeur difficile
不登陆或者登录解决oracle数据库账号被锁定。