当前位置:网站首页>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());
}
}
边栏推荐
- Esp32 simple speed message test of ros2 (limit frequency)
- Basis of information entropy
- Arduino dy-sv17f automatic voice broadcast
- d,ldc構建共享庫
- Is there a handling charge for spot gold investment
- leetcode刷题_两数之和 II - 输入有序数组
- [Arduino experiment 17 L298N motor drive module]
- ThinkPHP+Redis实现简单抽奖
- Kivy教程大全之 创建您的第一个kivy程序 hello word(教程含源码)
- Strongly connected components of digraph
猜你喜欢

Why can't the start method be called repeatedly? But the run method can?

Niu Ke swipes questions and clocks in

信息熵的基础
![[机缘参悟-36]:鬼谷子-飞箝篇 - 面对捧杀与诱饵的防范之道](/img/c6/9aee30cb935b203c7c62b12c822085.jpg)
[机缘参悟-36]:鬼谷子-飞箝篇 - 面对捧杀与诱饵的防范之道

MySQL basics 03 introduction to MySQL types

MySQL basic usage 02

Force buckle 204 Count prime

每日一题之干草堆的移动

Arduino dy-sv17f automatic voice broadcast

Using tensorboard to visualize the model, data and training process
随机推荐
Force buckle 204 Count prime
2022 Jiangxi Provincial Safety Officer B certificate reexamination examination and Jiangxi Provincial Safety Officer B certificate simulation examination question bank
Matlab Doppler effect produces vibration signal and processing
leetcode刷题_两数之和 II - 输入有序数组
Database SQL language 02 connection query
Concise analysis of redis source code 11 - Main IO threads and redis 6.0 multi IO threads
Druid database connection pool
LDC Build Shared Library
按键精灵打怪学习-多线程后台坐标识别
Is there anything in common between spot gold and spot silver
什么是调。调的故事
[shutter] animation animation (shutter animation type | the core class of shutter animation)
Androd gradle's substitution of its use module dependency
[FPGA tutorial case 5] ROM design and Implementation Based on vivado core
强化学习 Q-learning 实例详解
Do not log in or log in to solve the problem that the Oracle database account is locked.
leetcode 6103 — 从树中删除边的最小分数
How wide does the dual inline for bread board need?
Expérience de recherche d'emploi d'un programmeur difficile
MySQL basics 03 introduction to MySQL types