How and in what way are you invoking these ajax calls?
Asp.net MVC Ajax
I blog at http://rajeshpillai.net and have a community startup http://ownabook.org/
(Don't forget to click "Mark as Answer" on the post(s) that helped you.)
Access to ASP.NET session state is exclusive per session, which means that if two different users make concurrent requests, access to each separate session is granted concurrently. However, if two concurrent requests are made for the same session (by using
the same SessionID value), the first request gets exclusive access to the session information.
The second request executes only after the first request is finished. (The second session can also get access if the exclusive lock on the information is freed because the first request exceeds the lock time-out.) If the
EnableSessionState value in the
@ Page directive is set to ReadOnly, a request for the read-only session information does not result in an exclusive lock on the session data. However, read-only requests for session data might still have
to wait for a lock set by a read-write request for session data to clear.
It's about predictability rather than thread safety.
If two requests for the same user, which both need read/write access to that user's session state, were allowed to run concurrently, then they could end up with session state being in an unpredictable state, because both requests are reading and writing
to it. By serializing the requests per-user, this situation doesn't come into play.
When you use async action, then your request ends up with basically 3 phases: the beginning, where you schedule all the background work; the time when the background work is going on; and the end, when you collect all the results and formulate the response.
The middle bit is where most of your time should be spent, and during that middle bit, the session is not locked, so concurrent connections from the same user will be allowed to execute while the work is going on in the background.
Marked as answer by ricka6 on Dec 09, 2009 05:49 PM
liangxingyua...
0 Points
6 Posts
[Critical Question] Can mvc's controller method be concurrently executed in one page??
Dec 08, 2009 09:03 AM|LINK
TestCase:
1.I have a asp.net page, using the mvc 1.0.
2.I have make two ajax call to the same method of mvc controller.
In this method, i just thread.sleep(6000);
3.After page is being request and load, the two ajax call happen in the same time to same method.
Result:
Expect: Two ajax call return in already same time, each of time should just cost near 6 seconds.
Actual: Two ajax call return one by one depending on the ajax call seq,
And the first ajax call cost near 6 seconds, but the next ajax call cost near 12(6 * 2)secondes.
Seems the two ajax call is excuted in a seq not concurrently.
Do anyone know what's the problem???
I am so so so confused.!!!
"ASP.NET MVC 1.0"
thinkrajesh
Participant
1356 Points
232 Posts
Re: [Critical Question] Can mvc's controller method be concurrently executed in one page??
Dec 08, 2009 09:17 AM|LINK
Need some clarification..
How and in what way are you invoking these ajax calls?
Asp.net MVC Ajax
(Don't forget to click "Mark as Answer" on the post(s) that helped you.)
liangxingyua...
0 Points
6 Posts
Re: [Critical Question] Can mvc's controller method be concurrently executed in one page??
Dec 08, 2009 12:55 PM|LINK
I also had the same concern as your before.
I have try different ajax lib.
One is ExtJs, the second is JQuery($.Post()).
I saw two requests have been actually sent out at the same time via firebug in firefox.
Moreover, if i test different methods in the same controller, i got the same result that seems can't be concurrent.
So i am confused.
Anyone who can help me or who can try a test as my clarfication
Thanks a lot!
MVC 1.0
bradwils
Contributor
5779 Points
691 Posts
Microsoft
Re: [Critical Question] Can mvc's controller method be concurrently executed in one page??
Dec 08, 2009 03:36 PM|LINK
Because requests in MVC have access to session state, the requests are serialized to prevent corruption to session state.
If you have long-running actions, you should make them async.
ricka6
All-Star
15088 Points
2277 Posts
Microsoft
Moderator
Re: [Critical Question] Can mvc's controller method be concurrently executed in one page??
Dec 08, 2009 07:56 PM|LINK
If you have multiple parallel calls to make, that's a bigger bonus for using the new MVC 2 Async controller. See my MSDN article Using an Asynchronous Controller in ASP.NET MVC and my blog entry Should my database calls be Asynchronous?
liangxingyua...
0 Points
6 Posts
Re: [Critical Question] Can mvc's controller method be concurrently executed in one page??
Dec 09, 2009 08:06 AM|LINK
Did you mean this strategy is to prevent from two thread accessing the sessoin state in the same time?
Becuase session state is not thread-safe.
Right?
liangxingyua...
0 Points
6 Posts
Re: [Critical Question] Can mvc's controller method be concurrently executed in one page??
Dec 09, 2009 08:21 AM|LINK
Thanks a lot!!
I saw the frist link: Using an Async controller. And the second link doesn' t work
It seems the controller use a background thread to do the real work, so it can return my call suddently.
I have question for this:
Why we can't have mutilple control instance for one session when call happening?
And the parallel call and async call is a different issue.
Certainly, We can use the async call to sovle the parallel call, but it just look like we have solve but not actully in the strategy.
So can we allow the parallel call to the same page for the same controller in current mvc framework?
liangxingyua...
0 Points
6 Posts
Re: [Critical Question] Can mvc's controller method be concurrently executed in one page??
Dec 09, 2009 12:36 PM|LINK
Maybe i found the where the problem is.
Check here
http://msdn.microsoft.com/en-us/library/ms178581.aspx
It say
Concurrent Requests and Session State
<div>Access to ASP.NET session state is exclusive per session, which means that if two different users make concurrent requests, access to each separate session is granted concurrently. However, if two concurrent requests are made for the same session (by using the same SessionID value), the first request gets exclusive access to the session information. The second request executes only after the first request is finished. (The second session can also get access if the exclusive lock on the information is freed because the first request exceeds the lock time-out.) If the EnableSessionState value in the @ Page directive is set to ReadOnly, a request for the read-only session information does not result in an exclusive lock on the session data. However, read-only requests for session data might still have to wait for a lock set by a read-write request for session data to clear.
</div>bradwils
Contributor
5779 Points
691 Posts
Microsoft
Re: [Critical Question] Can mvc's controller method be concurrently executed in one page??
Dec 09, 2009 04:22 PM|LINK
It's about predictability rather than thread safety.
If two requests for the same user, which both need read/write access to that user's session state, were allowed to run concurrently, then they could end up with session state being in an unpredictable state, because both requests are reading and writing to it. By serializing the requests per-user, this situation doesn't come into play.
When you use async action, then your request ends up with basically 3 phases: the beginning, where you schedule all the background work; the time when the background work is going on; and the end, when you collect all the results and formulate the response. The middle bit is where most of your time should be spent, and during that middle bit, the session is not locked, so concurrent connections from the same user will be allowed to execute while the work is going on in the background.
liangxingyua...
0 Points
6 Posts
Re: [Critical Question] Can mvc's controller method be concurrently executed in one page??
Dec 10, 2009 12:13 AM|LINK
Cool thanks!
So when in the background work, the thread can't access the session because it doesn't exist.
And this protect us for predictability sesseion.
Right?