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.
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.
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