Springmvc frame concurrency occurs when ID becomes another user ID problem

Source: Internet
Author: User

Today's test write code, there is a login operation with one account, the operation is logged a different ID.

A search-on-the-web solution confirms the problem: a userid attribute is defined in the controller that transmits the value through the UserID each time. This UserID property is then shared in multithreaded situations

Solution: Delete the attributes inside the controller and replace them with other alternatives

Found an article on the controller single case and a number of analysis of the more Good articles:

For SPRINGMVC Controller Singleton and multiple examples, here is an example of the following. First: The class is a multi-instance, a normal property and a static property.

Result: General properties: 0 ....... Static properties: 0 Normal properties: 0 ...... Static properties: 1 Normal properties: 0 ...... Static properties: 2 Normal properties: 0 ...... Static properties: 3 Therefore, for the common properties of multiple cases, it will not be shared and will not have an effect, and the property will be shared for static properties.
Second time: class changed to single case

Result: General properties: 0 ....... Static properties: 0 Normal properties: 1 ...... Static properties: 1 Normal properties: 2 ...... Static properties: 2 Normal properties: 3 ...... Static properties: 3 So the common and static properties are shared for a single case.
Third: Classes Remove @scope annotations

Result: General properties: 0 ....... Static properties: 0 Normal properties: 1 ...... Static properties: 1 Normal properties: 2 ...... Static properties: 2 Normal properties: 3 ...... Static properties: 3 Therefore, SPRINGMVC is a singleton by default.
In addition print in other methods

The result of the output is

Jumping to another method does not go to the initial value, but to share the property.
In the end: Try not to define the attribute in the controller, if you need to define the attribute in special cases, then add the annotation @scope ("prototype") on the class to the pattern of multiple cases, the former struts is based on the properties of the class to send, Defining properties can be common to the whole class, so the default is a number of cases, or the multi-threaded access is definitely the property value of the common class, it is certainly unsafe, but Springmvc is based on the method of development, are used to receive the value of parameters, a method to end the parameter is destroyed, multi-threaded access will have a memory space to produce The parameters inside are not shared, and all SPRINGMVC default to a singleton, so it is not appropriate for the controller to define attributes within the class, as long as the controller does not define the attributes, then the singleton is completely safe. Springmvc This design is the main reason is to improve the performance of the program and later maintenance of the program only for the maintenance of the line, if the properties of the struts are more defined, do not know which method used this property, the maintenance of the program is still very troublesome. Citation: http://blog.csdn.net/qq_27026603/article/details/67953879 Liu Sujun's Blog

Springmvc frame concurrency occurs when ID becomes another user ID problem

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.