When you use Tomcat as the Web application container, the session is not found for the new thread to be enabled

Source: Internet
Author: User

Today to do a function, in order to quickly respond to the front end, after the business is completed, another thread does not affect the business of the statistical work, and then immediately return the results of business operations to the foreground.

As a result, the request object is not found on the new line thread report null pointer. Under inspection, we use the STRUTS2 framework, which binds the request to the thread local variables in the struts2. Through the context of the serveltactioncontext can be taken. Then I will manually set the request into the context of the struts2 in the new line thread.

The request was found, but Request.getsession () was empty. It is strange that it is impossible to destroy the session in such a short time. found that if the original main line sleep, not so anxious to return to the front, there will be no such mistake. Speculation is what the container does when it completes the response, causing the request to find no associated session.

Look at Tomcat's source code and discover that his Httpresquest interface implementation Class Org.apache.catalina.connector.Request has a recycle method. As the name implies recycling, the context of this request is cleared, of course, there is no session. When this new request comes in, it is not created, it can be used directly to bind the stateless object to a new context, which is equivalent to the function of an object pool.

So the reason is that the main thread executes very quickly, when the main thread binding request is cleaned up and no longer associated with the session. Our newly created thread binds to the request object and cannot find the session after the main thread has finished. The workaround is simple, and the session is bound to the new thread with a thread-local variable.

When you use Tomcat as the Web application container, the session is not found for the new thread to be enabled

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.