当前位置:网站首页>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());
}
}
边栏推荐
- What is tone. Diao's story
- 强化学习 Q-learning 实例详解
- Now that the teenager has returned, the world's fireworks are the most soothing and ordinary people return to work~
- Database SQL language 01 where condition
- d,ldc構建共享庫
- How wide does the dual inline for bread board need?
- MySQL foundation 06 DDL
- Leetcode 2097 - Legal rearrangement of pairs
- 对非ts/js文件模块进行类型扩充
- Is there a handling charge for spot gold investment
猜你喜欢
leetcode 6103 — 从树中删除边的最小分数
[FPGA tutorial case 6] design and implementation of dual port RAM based on vivado core
Niu Ke swipes questions and clocks in
leetcode 2097 — 合法重新排列数对
Arduino dy-sv17f automatic voice broadcast
Basic concept and implementation of overcoming hash
强化学习 Q-learning 实例详解
MySQL
C application interface development foundation - form control (2) - MDI form
leetcode刷题_两数之和 II - 输入有序数组
随机推荐
Matlab Doppler effect produces vibration signal and processing
2022 coal mine gas drainage examination question bank and coal mine gas drainage examination questions and analysis
Tp6 fast installation uses mongodb to add, delete, modify and check
Give you an array numbers that may have duplicate element values. It was originally an array arranged in ascending order, and it was rotated once according to the above situation. Please return the sm
Do not log in or log in to solve the problem that the Oracle database account is locked.
Thinkphp+redis realizes simple lottery
Arduino dy-sv17f automatic voice broadcast
C application interface development foundation - form control (3) - file control
Now that the teenager has returned, the world's fireworks are the most soothing and ordinary people return to work~
Kivy tutorial how to create drop-down lists in Kivy
Androd Gradle 对其使用模块依赖的替换
[system analyst's road] Chapter V double disk software engineering (development model development method)
On Fibonacci sequence
测试右移:线上质量监控 ELK 实战
如今少年已归来,人间烟火气最抚凡人心 复工了~
【FPGA教程案例6】基于vivado核的双口RAM设计与实现
每日一题之干草堆的移动
Excel calculates the difference between time and date and converts it into minutes
Mathematical knowledge: divisible number inclusion exclusion principle
SwiftUI 组件大全之使用 SceneKit 和 SwiftUI 构建交互式 3D 饼图(教程含源码)