Small issues caused by session blocking read/write locks

Source: Internet
Author: User
  • Introduction

Let's take a look at the two Ajax methods. What kind of results will there be?

Javascript:

$ (Function () {// the first $. ajax ({type: "Post", URL: "/home/Method1", success: function (MSG) {response ('processing m1'finished .html ('completed first method ');}}); // The second $. ajax ({type: "Post", URL: "/home/method2", success: function (MSG) {(('{m2'{.html ('second method complete ');}});});

Server

  public class HomeController : Controller    {        //        // GET: /Home/        public ActionResult Index()        {            Session["test"] = 123;            return View();        }        public ActionResult Method1()        {            Thread.Sleep(6000);            return Json("");        }        public ActionResult Method2()        {            return Json("");        }    }

I thought:The two Ajax requests are asynchronous requests. Whoever finishes processing them first will return them first.

But the reality is quite unexpected. See.

Obviously, the second request is executed only after the first request is processed.

  • Root Cause

In fact, the problem occurs on the session, so we will remove the session ["test"] And then look at the result again:

It turns out that the demon is a session. The same session, that is, the sessionid of the request is the same, will initialize the session object and lock the session object. Other requests of the same session will use the session object and can only wait. Therefore, the previous situation occurs.

  • Solution

The lock will not be applied if you only need to identify the current session behavior as read-only.

       [SessionState(SessionStateBehavior.ReadOnly)]

 

 

[SessionState(SessionStateBehavior.ReadOnly)]    public class HomeController : Controller    {        //        // GET: /Home/        public ActionResult Index()        {            Session["test"] = 123;            return View();        }        public ActionResult Method1()        {            Thread.Sleep(6000);            return Json("");        }        public ActionResult Method2()        {            return Json("");        }    }

 

 

 

The result meets the statement, for example:

Small issues caused by session blocking read/write locks

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.