当前位置:网站首页>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());
}
}
边栏推荐
- Key wizard hit strange learning - automatic path finding back to hit strange points
- dotConnect for PostgreSQL数据提供程序
- Top ten regular spot trading platforms 2022
- Dotconnect for PostgreSQL data provider
- The industrial scope of industrial Internet is large enough. The era of consumer Internet is only a limited existence in the Internet industry
- Is there a handling charge for spot gold investment
- Strongly connected components of digraph
- leetcode:871. Minimum refueling times [Pat has done before + maximum stacking + greed]
- 按键精灵打怪学习-自动回城路线的判断
- MySQL basics 03 introduction to MySQL types
猜你喜欢
Basic remote connection tool xshell
wirehark数据分析与取证A.pacapng
leetcode刷题_两数之和 II - 输入有序数组
How wide does the dual inline for bread board need?
Wireshark data analysis and forensics a.pacapng
MySQL
Soft exam information system project manager_ Real topic over the years_ Wrong question set in the second half of 2019_ Morning comprehensive knowledge question - Senior Information System Project Man
What is tone. Diao's story
软考信息系统项目管理师_历年真题_2019下半年错题集_上午综合知识题---软考高级之信息系统项目管理师053
MySQL basics 03 introduction to MySQL types
随机推荐
leetcode:871. Minimum refueling times [Pat has done before + maximum stacking + greed]
音程的知识的总结
[shutter] animation animation (shutter animation type | the core class of shutter animation)
Daily topic: movement of haystack
并发编程的三大核心问题 -《深入理解高并发编程》
【FPGA教程案例6】基于vivado核的双口RAM设计与实现
Look at how clothing enterprises take advantage of the epidemic
一比特苦逼程序員的找工作經曆
Key wizard hit strange learning - automatic path finding back to hit strange points
uniapp组件-uni-notice-bar通告栏
Excel calculates the difference between time and date and converts it into minutes
每日一题之干草堆的移动
【FPGA教程案例5】基于vivado核的ROM设计与实现
kivy教程之在 Kivy App 中使用 matplotlib 的示例
产业互联网的产业范畴足够大 消费互联网时代仅是一个局限在互联网行业的存在
MySQL - database query - basic query
Niu Ke swipes questions and clocks in
Androd Gradle 对其使用模块依赖的替换
Draw love with go+ to express love to her beloved
[技术发展-23]:DSP在未来融合网络中的应用