码迷,mamicode.com
首页 > 其他好文 > 详细

What is the difference between Session.Abandon() and Session.Clear()

时间:2020-06-22 12:52:09      阅读:53      评论:0      收藏:0      [点我收藏+]

标签:cal   release   layout   cte   out   source   win   cts   session   

What is the difference between Session.Abandon() and Session.Clear()

 Clear - Removes all keys and values from the session-state collection.

Abandon - removes all the objects stored in a Session. If you do not call the Abandon method explicitly, the server removes these objects and destroys the session when the session times out.
It also raises events like Session_End.

Session.Clear can be compared to removing all books from the shelf, while Session.Abandon is more like throwing away the whole shelf.

You say:

When I test Session, it doesn‘t makes any change when I Abandon the session.

This is correct while you are doing it within one request only.
On the next request the session will be different. But the session ID can be reused so that the id will remain the same.

If you will use Session.Clear you will have the same session in many requests.

Generally, in most cases you need to use Session.Clear.
You can use Session.Abandon if you are sure the user is going to leave your site.

So back to the differences:

  1. Abandon raises Session_End request.
  2. Clear removes items immidiately, Abandon does not.
  3. Abandon releases the SessionState object and its items so it can ba garbage collected to free the resources. Clear keeps SessionState and resources associated with it.

 

What is the difference between Session.Abandon() and Session.Clear()

标签:cal   release   layout   cte   out   source   win   cts   session   

原文地址:https://www.cnblogs.com/chucklu/p/13175962.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!