Threadlocal for session management

Source: Internet
Author: User
  • Among various session management solutions, the threadlocal mode is widely used. Threadlocal is
    Java is a special thread binding mechanism. Data accessed through threadlocal is always related to the current thread,
    That is to say, the JVM binds a private local instance access space for each running thread, so as
    The current concurrent access problem provides an isolation mechanism.
    First, we need to know that sessionfactory is responsible for creating sessions, and sessionfactory is a thread.
    Secure. multiple concurrent threads can simultaneously access one sessionfactory and obtain the session instance from it. While
    The session is not thread-safe. That is to say, if multiple threads use one session instance for data access at the same time,
    The session data access logic is disordered. The following is a typical servlet. We try to use a class
    The variable session implements session reuse to avoid re-creation of each operation:
    Public class testservlet extends httpservlet {
    Private session;
    Public void doget (httpservletrequest request,
    Httpservletresponse response)
    Throws servletexception, ioexception {
    Session = getsession ();
    Dosomething ();
    Session. Flush ();
    }
    Public void dosomething (){
    ... // Session-based access operation
    }
    }
    The Code seems to be correct, even when we test it on a single machine, there may be no problems, but such
    Hibernate developer's guide version 1.0
    September 2, 2004 so far open source projects. Why not open your documents ents?
    Once the code is compiled and deployed to the actual running environment, the following inexplicable errors may make us confused.
    Where is the problem?
    First, Servlet runs in multiple threads, and the application server does not create a servlet for each thread.
    Instance, that is, testservlet has only one instance in the Application Server (in Tomcat, this is the case, other
    The application server may have different implementations), and this instance will be concurrently called by many threads, and the doget method will also be disabled.
    The same thread is called repeatedly. You can imagine that every time you call the doget method, the only testservlet instance's
    All session variables will be reset. If other threads are also executed during thread a's operation, the session
    The reference will change, and then thread a will call the session. It is possible that the session at this time and the previous
    The session will no longer be consistent. Obviously, errors will not come in due time.
    The appearance of threadlocal solves this problem.
    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 session = (Session) localsession. Get ();
    ... // Session-based access operation
    }
    }
    As you can see, localsession is a threadlocal object. In the doget method, we
    The set method saves the obtained session instance. In the dosomething method
    Output session instance.
    This is the uniqueness of threadlocal. It maintains a private variable space for each thread. Actually,
    The implementation principle is to maintain a map in JVM. The map key is the current thread object, while the value is
    The object instance that the thread saves using the threadlocal. Set Method. When the thread calls the threadlocal. Get method,
    Threadlocal extracts the corresponding objects in the map based on the reference of the current thread object.
    In this way, threadlocal isolates variables of different threads by referencing each thread object.
    .

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.