正在使用Session线程安全吗?

时间:2021-04-05 21:00:57

Consider a user making multiple requests at the same time, do I have to lock all code that works with the Session?

考虑用户同时发出多个请求,是否必须锁定与Session一起使用的所有代码?

If for example I have the following scenario, where in one tab of his browser the user opens a page and in the second he logs out.

例如,如果我有以下场景,在浏览器的一个选项卡中,用户打开一个页面,在第二个窗口中他退出。

Request 1:

要求1:

if(Session["user"] != null)
    lblName.Text = Session["user"].Name;

Request 2:

要求2:

if(logout)
   Session["user"] = null;

Is it possible that Request 1 throws a NullPointerException when accessing the Name property? Do i need to lock the code in Request 1, to make sure user still exists after checking for null? Or does ASP.NET deal with this automatically somehow?

请求1在访问Name属性时是否可能抛出NullPointerException?我是否需要锁定请求1中的代码,以确保用户在检查null后仍然存在?或者ASP.NET是否以某种方式自动处理?

4 个解决方案

#1


9  

As always, the answer depends on what you mean by "safety." In ASP .NET, each request gets exclusive access to its session state. This means that you don't have to worry about synchronizing access within the scope of a single request. If Session["user"] is non-null, then it will be non-null for the entire duration of the current request. In your example, request 1 will never throw a null reference exception.

一如既往,答案取决于“安全”的含义。在ASP .NET中,每个请求都可以独占访问其会话状态。这意味着您不必担心在单个请求的范围内同步访问。如果Session [“user”]为非null,则在当前请求的整个持续时间内它将为非null。在您的示例中,请求1将永远不会抛出空引用异常。

#2


13  

Two requests to an ASP.NET application for the same same session, where the handlers do not use the IReadOnlySessionState marker interface or have the EnableSessionState="ReadOnly" turned on for your pages, will be serialized by the ASP.NET runtime to guarantee consistency of the state. So if you have two pages that are able to write to the session state, they will be accessed serially no matter what the client does on their side.

对于同一会话的ASP.NET应用程序的两个请求,其中处理程序不使用IReadOnlySessionState标记接口或为您的页面启用了EnableSessionState =“ReadOnly”,将由ASP.NET运行时序列化以保证一致性国家因此,如果您有两个能够写入会话状态的页面,无论客户端做什么,它们都将被串行访问。

It's up to your application code to signal to ASP.NET with the afforementioned techniques whether or not a page/handler is going to write to the session state. If you do not, all requests will be serialized and the performance of your web application will suffer.

无论页面/处理程序是否要写入会话状态,您的应用程序代码都可以使用上述技术向ASP.NET发送信号。如果不这样做,所有请求都将被序列化,并且Web应用程序的性能将受到影响。

#3


4  

In ASP.NET, the Session module uses a pair of reader/writer locks per session, so Request 1 will have consistent reads, and Request 2 will block until Request 1 completes.

在ASP.NET中,会话模块每个会话使用一对读取器/写入器锁,因此请求1将具有一致的读取,并且请求2将阻塞,直到请求1完成。

#4


1  

Yes, Session is thread-safe.

是的,Session是线程安全的。

There's no need to lock anything. However, the need to check the values never cease to be essential.

没有必要锁定任何东西。但是,检查值的必要性永远不会是必不可少的。

Update

更新

Check @Peter Ruderman's answer :)

查看@Peter Ruderman的回答:)

I will have the decency of not copying it :)

我会体会到不复制它:)

#1


9  

As always, the answer depends on what you mean by "safety." In ASP .NET, each request gets exclusive access to its session state. This means that you don't have to worry about synchronizing access within the scope of a single request. If Session["user"] is non-null, then it will be non-null for the entire duration of the current request. In your example, request 1 will never throw a null reference exception.

一如既往,答案取决于“安全”的含义。在ASP .NET中,每个请求都可以独占访问其会话状态。这意味着您不必担心在单个请求的范围内同步访问。如果Session [“user”]为非null,则在当前请求的整个持续时间内它将为非null。在您的示例中,请求1将永远不会抛出空引用异常。

#2


13  

Two requests to an ASP.NET application for the same same session, where the handlers do not use the IReadOnlySessionState marker interface or have the EnableSessionState="ReadOnly" turned on for your pages, will be serialized by the ASP.NET runtime to guarantee consistency of the state. So if you have two pages that are able to write to the session state, they will be accessed serially no matter what the client does on their side.

对于同一会话的ASP.NET应用程序的两个请求,其中处理程序不使用IReadOnlySessionState标记接口或为您的页面启用了EnableSessionState =“ReadOnly”,将由ASP.NET运行时序列化以保证一致性国家因此,如果您有两个能够写入会话状态的页面,无论客户端做什么,它们都将被串行访问。

It's up to your application code to signal to ASP.NET with the afforementioned techniques whether or not a page/handler is going to write to the session state. If you do not, all requests will be serialized and the performance of your web application will suffer.

无论页面/处理程序是否要写入会话状态,您的应用程序代码都可以使用上述技术向ASP.NET发送信号。如果不这样做,所有请求都将被序列化,并且Web应用程序的性能将受到影响。

#3


4  

In ASP.NET, the Session module uses a pair of reader/writer locks per session, so Request 1 will have consistent reads, and Request 2 will block until Request 1 completes.

在ASP.NET中,会话模块每个会话使用一对读取器/写入器锁,因此请求1将具有一致的读取,并且请求2将阻塞,直到请求1完成。

#4


1  

Yes, Session is thread-safe.

是的,Session是线程安全的。

There's no need to lock anything. However, the need to check the values never cease to be essential.

没有必要锁定任何东西。但是,检查值的必要性永远不会是必不可少的。

Update

更新

Check @Peter Ruderman's answer :)

查看@Peter Ruderman的回答:)

I will have the decency of not copying it :)

我会体会到不复制它:)