asp.net - Why is controller thread is blocked if I use the session? -
i have pretty simple controller:
public class homecontroller : controller { public actionresult index() { session["somedata"] = "123"; return view(); } [httppost] public actionresult longtest() { thread.sleep(5000); return json(new { text = datetime.now.tostring("hh:mm:ss.fff") + " - longtest"}); } [httppost] public actionresult cantanswer() { return json(new { text = datetime.now.tostring("hh:mm:ss.fff") + " - cantanswer"}); } }
i use these methods client's side way:
<script type="text/javascript"> $(document).ready(function () { $('#btnlongoperation').click(function () { $.post("/home/longtest", null, function (data) { $('#result').text(data.text); }, "json"); }); $('#btnwnotwork').click(function () { $.post("/home/cantanswer", null, function (data) { $('#result').text(data.text); }, "json"); }); }); </script> <div> <input id="btnlongoperation" type="button" value="long operation"/> <input id="btnwnotwork" type="button" value="won't work"/> </div> <div id="result">
if click first button , without waiting 5 seconds click second button second action won't called. if remove string using session in init method see actions able called without waiting each other. however, once use session object not see result of second action untill first 1 finished. can explain behavior of asp.net mvc?
this caused session locking. in essence, each request uses session state places lock on until done reading. subsequent requests cannot access session
until previous request releases lock.
the purpose of ensure integrity of session data. example, happen if request needed write session , request b needed read, requests issued simultaneously? data b reads unpredictable have no way of knowing whether pre or post-write.
read here more information: