The Threadlocal of Session management

Source: Internet
Author: User
In the various session management scheme, the ThreadLocal mode has been used extensively. ThreadLocal is
A more specific thread-binding mechanism in Java. Data that is accessed through threadlocal is always associated with the current thread,
In other words, the JVM binds a private local instance access space for each thread that is running, which is often used for multithreaded environments
The present concurrency access problem provides an isolation mechanism.
First, we need to know that Sessionfactory is responsible for creating session,sessionfactory is the thread
Secure, multiple concurrent threads can access a sessionfactory at the same time and get the session instance from it. and
Session is not thread safe, that is, if multiple threads use a session instance for data access at the same time,
Will cause the session data access logic to be confusing. The following is a typical servlet that we try to pass through a class
Variable session to implement session reuse to avoid recreating each operation:
public class Testservlet extends HttpServlet {
Private session session;
public void doget (HttpServletRequest request,
HttpServletResponse response)
Throws Servletexception, IOException {
Session = GetSession ();
DoSomething ();
Session.flush ();
}
public void dosomething () {
.//Session-based access operations
}
}
The code appears to be correct, even when we test on a single machine may not have any problems, but such a generation
Hibernate Developer ' s Guide Version 1.0
September 2, many open source projects. Why not Open your Documents?
Once the code has been compiled and deployed into the actual operating environment, the ensuing inexplicable error is likely to make us feel no mind.
The problem is where.
First, the servlet run is multithreaded, and the application server does not create a servlet for each thread
instance, that is, Testservlet has only one instance in the application server (as in Tomcat, the other
The application server may have a different implementation, and this instance will be invoked by many threads concurrently, and the Doget method will be not
The same thread is called repeatedly, and it is conceivable that each call to the Doget method, this unique Testservlet instance of the
Session variables are reset, thread a runs, and other threads are executed, then session
is changed, then thread A calls the session again, possibly at this time with the
The session is no longer consistent, and obviously, mistakes are not the same.
The appearance of threadlocal makes the problem solved.
Let's make some minor changes to the above example:
public class Testservlet extends HttpServlet {
Private ThreadLocal localsession = new ThreadLocal ();
public void doget (HttpServletRequest request,
HttpServletResponse response)
Throws Servletexception, IOException {
Localsession.set (GetSession ());
DoSomething ();
Session.flush ();
}
public void dosomething () {
Session Sessions = (session) Localsession.get ();
.//Session-based access operations
}
}
As you can see, localsession is a threadlocal type of object, in the Doget method, we
The acquired session instance is saved by its set method, and in the DoSomething method, the Get method is used to fetch the
Out the session instance.
This is the unique feature of Threadlocal, which maintains a private variable space for each thread. As a matter of fact
The principle is to maintain a map in the JVM, the key of the map is the current thread object, and value is
The object instance that the thread saves through the Threadlocal.set method. When the thread calls the Threadlocal.get method,
Threadlocal returns the corresponding object in the map, based on the current thread object's reference.
In this way, threadlocal separates the variables of different threads by differentiating them by reference to each thread object
To.

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.