Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Business Logic errors caused by ThreadLocal's failure to call the remove method

2025-01-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the example analysis that ThreadLocal does not call the remove method will lead to business logic errors, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Why causes the business logic to go wrong:

When ThreadLocal is used for thread pooling, web applications, or threads are reused many times, it is important to pay special attention to web applications:

We all know that many classes of web applications are singleton patterns. For example, the class created by the default injection method of spring is a singleton. When different http requests arrive at the server, the same instance is actually used. If the instance uses a global variable, when request A modifies this variable, other requests that follow (regardless of whether the A request ends or not), such as when request B uses the variable again, It is actually modified by request A, which can lead to business logic errors and is difficult to find. This situation is usually solved by using ThreadLocal, because different requests use the same instance, but the threads used are different, but it is important to pay special attention to that the threads of the web container are reused, and the web container uses thread pools, when a request uses a thread. The thread will be put back into the thread pool to be used by other requests, which leads to a problem. Different requests may still use the same thread (as long as the number of requests is greater than the number of threads), and ThreadLocal belongs to the thread. If we finish using the ThreadLocal object and do not delete it manually, then the later request will have a chance to use the used ThreadLocal object. If a request uses ThreadLocal It is first get () to judge and then set (), then there will be a problem, because get is the content of other request set, if every request uses ThreadLocal, it is set and then get, then there will be no problem, because a thread is only used by one request at the same time, as long as we set it to the content we want before each use, it will not be overwritten in the process of use. It is best to call the remove method every time you finish using ThreadLocal to delete it, so as to avoid business errors caused by get before set.

Example:

There are 3 libraries in the sub-library db1 db2 db3

The web application uses a thread pool if there are 10 threads

When the request1 request arrives at the web server, the db1 is located after the logical processing of the library. The thread number assigned by the tomcat thread pool is thread1, and the data source saved in the threadlocal.set given to the thread1 thread is db1.

Because the thread in the tomcat thread pool is reused, request2 happens to assign the request to threade1. If the request for request2 does not go through the partition logic and uses the configured dead db2 data source, then this will cause request2 to use the data source db1 of threadlocal in the thread1 that last processed the request request1. This will lead to logic errors and errors in finding the corresponding table.

This is the example analysis of business logic errors caused by ThreadLocal's failure to call the remove method. I hope the above can be helpful and learn more. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 221

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report