当前位置:网站首页>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());
}
}
边栏推荐
- MySQL
- Create your first Kivy program Hello word (tutorial includes source code)
- Druid database connection pool
- 【第29天】给定一个整数,请你求出它的因子数
- Now that the teenager has returned, the world's fireworks are the most soothing and ordinary people return to work~
- Button wizard play strange learning - go back to the city to buy medicine and add blood
- Vim 9.0正式发布!新版脚本执行速度最高提升100倍
- Expérience de recherche d'emploi d'un programmeur difficile
- C#应用程序界面开发基础——窗体控制(2)——MDI窗体
- C application interface development foundation - form control (3) - file control
猜你喜欢

C#应用程序界面开发基础——窗体控制(2)——MDI窗体
![[技术发展-23]:DSP在未来融合网络中的应用](/img/2e/f39543a18a8f58b1d341ce72cc4427.png)
[技术发展-23]:DSP在未来融合网络中的应用

信息熵的基础
![[interview question] 1369 when can't I use arrow function?](/img/7f/84bba39965b4116f20b1cf8211f70a.png)
[interview question] 1369 when can't I use arrow function?

How is the mask effect achieved in the LPL ban/pick selection stage?

Database SQL language 01 where condition

MySQL
![[Androd] Gradle 使用技巧之模块依赖替换](/img/5f/968db696932f155a8c4a45f67135ac.png)
[Androd] Gradle 使用技巧之模块依赖替换

攻克哈希的基本概念与实现

Niu Ke swipes questions and clocks in
随机推荐
Database SQL language 01 where condition
MySQL基础用法02
【第29天】给定一个整数,请你求出它的因子数
MySQL basics 03 introduction to MySQL types
Button wizard play strange learning - go back to the city to buy medicine and add blood
Top ten regular spot trading platforms 2022
Appuyez sur l'apprentissage de l'esprit de frappe - reconnaissance des coordonnées de fond multithreadées
测试右移:线上质量监控 ELK 实战
数学知识:台阶-Nim游戏—博弈论
MySQL basic usage 02
[Cao gongzatan] after working in goose factory for a year in 2021, some of my insights
Concise analysis of redis source code 11 - Main IO threads and redis 6.0 multi IO threads
On Fibonacci sequence
机器学习术语
2022 coal mine gas drainage examination question bank and coal mine gas drainage examination questions and analysis
Is there a handling charge for spot gold investment
Key wizard play strange learning - front desk and Intranet send background verification code
Is there anything in common between spot gold and spot silver
How is the mask effect achieved in the LPL ban/pick selection stage?
[FPGA tutorial case 5] ROM design and Implementation Based on vivado core