当前位置:网站首页>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());
}
}
边栏推荐
- 【第29天】给定一个整数,请你求出它的因子数
- 给你一个可能存在 重复 元素值的数组 numbers ,它原来是一个升序排列的数组,并按上述情形进行了一次旋转。请返回旋转数组的最小元素。【剑指Offer】
- 2022 coal mine gas drainage examination question bank and coal mine gas drainage examination questions and analysis
- MySQL basic usage 02
- 看疫情之下服装企业如何顺势而为
- Basic remote connection tool xshell
- leetcode:701. Insertion in binary search tree [BST insertion]
- LDC Build Shared Library
- Appuyez sur l'apprentissage de l'esprit de frappe - reconnaissance des coordonnées de fond multithreadées
- Basis of information entropy
猜你喜欢
Arduino dy-sv17f automatic voice broadcast
基本远程连接工具Xshell
leetcode刷题_两数之和 II - 输入有序数组
Wireshark data analysis and forensics a.pacapng
给你一个可能存在 重复 元素值的数组 numbers ,它原来是一个升序排列的数组,并按上述情形进行了一次旋转。请返回旋转数组的最小元素。【剑指Offer】
C#应用程序界面开发基础——窗体控制(1)——Form窗体
什么是调。调的故事
Expérience de recherche d'emploi d'un programmeur difficile
一位苦逼程序员的找工作经历
Androd Gradle 对其使用模块依赖的替换
随机推荐
uniapp组件-uni-notice-bar通告栏
[untitled]
[flutter] icons component (fluttericon Download Icon | customize SVG icon to generate TTF font file | use the downloaded TTF icon file)
Uniapp component -uni notice bar notice bar
Kivy tutorial how to create drop-down lists in Kivy
Top ten regular spot trading platforms 2022
如今少年已归来,人间烟火气最抚凡人心 复工了~
Mathematical Knowledge: Steps - Nim Games - Game Theory
Look at how clothing enterprises take advantage of the epidemic
C application interface development foundation - form control (4) - selection control
Strongly connected components of digraph
kivy教程之在 Kivy App 中使用 matplotlib 的示例
[fh-gfsk] fh-gfsk signal analysis and blind demodulation research
2022 Jiangxi Provincial Safety Officer B certificate reexamination examination and Jiangxi Provincial Safety Officer B certificate simulation examination question bank
MySQL foundation 06 DDL
MySQL
产业互联网的产业范畴足够大 消费互联网时代仅是一个局限在互联网行业的存在
Using tensorboard to visualize the model, data and training process
SwiftUI 组件大全之使用 SceneKit 和 SwiftUI 构建交互式 3D 饼图(教程含源码)
leetcode:701. Insertion in binary search tree [BST insertion]