The question is, is it possible that requests for the same session are executed from multiple threads? Are methods in ASP.NET reentrant? Especially we are using AJAX which means that asychronous requests are taking place.
问题是,是否有可能从多个线程执行同一会话的请求? ASP.NET中的方法是否可重入?特别是我们正在使用AJAX,这意味着正在进行异步请求。
Would this mean to place locks around operations on objects placed inside the session?
这是否意味着将锁定放在会话内放置的对象上的操作?
I know that locks are essential when handling static and application wide variables, but the question is is the same true for session objects?
我知道在处理静态和应用程序范围的变量时,锁是必不可少的,但问题对于会话对象是否同样如此?
2 个解决方案
#1
3
ASP.NET normally uses one thread per request. It can use more than one thread, e.g. when serving asynchronous pages, but even then only one thread will be processing the request at any given time.
ASP.NET通常每个请求使用一个线程。它可以使用多个线程,例如在提供异步页面时,即使这样,在任何给定时间只有一个线程将处理该请求。
It's safe to use the session state from multiple threads, however, because accesses to the session object are serialized. From MSDN:
但是,从多个线程使用会话状态是安全的,因为会话对象的访问是序列化的。来自MSDN:
What if other pages attempt to concurrently access the session state? In that case, the current request might end up working on inconsistent data, or data that isn't up to date. Just to avoid this, the session state module implements a reader/writer locking mechanism and queues the access to state values. A page that has session-state write access will hold a writer lock on the session until the request terminates.
如果其他页面尝试同时访问会话状态怎么办?在这种情况下,当前请求可能最终会处理不一致的数据或不是最新的数据。为了避免这种情况,会话状态模块实现了一个读/写锁定机制,并将对状态值的访问排队。具有会话状态写访问权限的页面将在会话上保持写入程序锁定,直到请求终止。
#2
2
The question is, is it possible that requests for the same session are executed from multiple threads?
问题是,是否有可能从多个线程执行同一会话的请求?
If you use HTTP Session inside your page, then requests for the same session will be queued by the ASP.NET engine. They will never run in parallel. So for example if you send multiple AJAX requests to some ASP.NET page which uses session those requests will execute sequentially.
如果在页面内使用HTTP Session,则ASP.NET引擎将对同一会话的请求进行排队。它们永远不会并行运行。因此,例如,如果您向使用会话的某个ASP.NET页面发送多个AJAX请求,那么这些请求将按顺序执行。
If you don't use Session then multiple concurrent requests will run in parallel.
如果您不使用Session,那么多个并发请求将并行运行。
#1
3
ASP.NET normally uses one thread per request. It can use more than one thread, e.g. when serving asynchronous pages, but even then only one thread will be processing the request at any given time.
ASP.NET通常每个请求使用一个线程。它可以使用多个线程,例如在提供异步页面时,即使这样,在任何给定时间只有一个线程将处理该请求。
It's safe to use the session state from multiple threads, however, because accesses to the session object are serialized. From MSDN:
但是,从多个线程使用会话状态是安全的,因为会话对象的访问是序列化的。来自MSDN:
What if other pages attempt to concurrently access the session state? In that case, the current request might end up working on inconsistent data, or data that isn't up to date. Just to avoid this, the session state module implements a reader/writer locking mechanism and queues the access to state values. A page that has session-state write access will hold a writer lock on the session until the request terminates.
如果其他页面尝试同时访问会话状态怎么办?在这种情况下,当前请求可能最终会处理不一致的数据或不是最新的数据。为了避免这种情况,会话状态模块实现了一个读/写锁定机制,并将对状态值的访问排队。具有会话状态写访问权限的页面将在会话上保持写入程序锁定,直到请求终止。
#2
2
The question is, is it possible that requests for the same session are executed from multiple threads?
问题是,是否有可能从多个线程执行同一会话的请求?
If you use HTTP Session inside your page, then requests for the same session will be queued by the ASP.NET engine. They will never run in parallel. So for example if you send multiple AJAX requests to some ASP.NET page which uses session those requests will execute sequentially.
如果在页面内使用HTTP Session,则ASP.NET引擎将对同一会话的请求进行排队。它们永远不会并行运行。因此,例如,如果您向使用会话的某个ASP.NET页面发送多个AJAX请求,那么这些请求将按顺序执行。
If you don't use Session then multiple concurrent requests will run in parallel.
如果您不使用Session,那么多个并发请求将并行运行。