Thread pool and ThreadLocal

Source: Internet
Author: User

A bit of code was changed in Hhoj today.

In the previous app, use Threadlocal to save the user Userinfomodel for that thread. Fetched from Usersession when the thread is activated and then put into threadlocal.

Today I want to change the user name to be saved only in session. Thread activation is based on getting the latest userinfomodel from the database.

So the question came.

First, if the user name is empty, the threadlocal is not emptied.

The problem is that, in the non-logged-in state, if the previous thread is reused from the thread pool after repeated flushing,

Then the Userinfomodel in the previous threadlocal will be strung into the session and saved.

Causes the user not logged in the state to refresh several times the page becomes the sign-in state ...

(╯°-°) ╯︵┻━┻. And the first login is often the administrator (that is, me).

And this crossed is contagious. A session this accidentally gets a model that will be written to its session to save it,

The next time a new thread is encountered, it is transmitted to the next thread.

So soon all the threads have an administrator's Userinfomodel ... T.T ....

In the event of an online pool, if you use threadlocal, or another thread-key structure, every time you activate a new thread, you must clear all threadlocal ...

In the event of an online pool, if you use threadlocal, or another thread-key structure, every time you activate a new thread, you must clear all threadlocal ...

In the event of an online pool, if you use threadlocal, or another thread-key structure, every time you activate a new thread, you must clear all threadlocal ...

Important thing to say three times ...

Thread pool and ThreadLocal

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.