views:

2034

answers:

5

hi all.

I searched for this question but don't grasp total answer.

Some answers are:

  • "Session.Abandon() destroys the session
  • "Session.Clear() just removes all values

i don't understand this. What is the difference between destroying a session and removing its values (please with a example show me this).

A friend told me this:

Clearing the session will not unset the session, it still exists with the same ID for the user but with the values simply cleared.

Abandon will destroy the session completely, meaning that you need to begin a new session before you can store any more values in the session for that user.

this code works and dont throw any exception:

Session.Abandon();
Session["tempKey1"] = "tempValue1";

When you Abandon() a Session, you (or rather the user) will get a new SessionId

when i Test Session Id Dont makes any change when i Abandone session

i just find one difference: session.Abandon() raises Session_End event

+1  A: 

Clearing a session removes the values that were stored there, but you still can add new ones there. After destroying the session you cannot add new values there.

RaYell
thanks but this works:(inconsistent with your idea)Session.Abandon(); Session["tempKey1"] = "tempValue1"; Response.Write(Session["tempKey1"].ToString());
backdoor
It does because session will be destroyed after you refresh your page actually. Session id it (usually) stored in a cookie.
RaYell
i dont understand.i mean after i call Session.Abandon() still i can add values to it. (even if page is posted back and again renders).
backdoor
You may have you session set to autoregenerate id after it was destroyed. So when you assign a value to destroyed session new session will be generated automatically.
RaYell
yes, using this session id will regenerate but my question is: what is different between session.clear() and session.abandone() at all.in this way when autoregenerate is seted to false these tow dont differ in anything else raising Sesion_End?
backdoor
+1  A: 

The session data is stored in a file on the server.

Clearing the session will not unset the session, it still exists with the same ID for the user but with the values simply cleared.

Abandon will destroy the session completely, meaning that you need to begin a new session before you can store any more values in the session for that user.

Evernoob
thanks but this works:(inconsistent with your idea)Session.Abandon(); Session["tempKey1"] = "tempValue1"; Response.Write(Session["tempKey1"].ToString());
backdoor
Session data is not stored in a file - either:1) In memory (in or our of process) or 2) in an SQL server DB
UpTheCreek
+2  A: 

When you Abandon() a Session, you (or rather the user) will get a new SessionId (on the next request). When you Clear() a Session, all stored values are removed, but the SessionId stays intact.

Hans Kesting
thanks.but accroding to mattew macdonalds book it can use same session id. i means that if regenerateExpiredSessionId attribute at configuration/system.web/sessionState element in web.config file is false ASP.Net uses old session id
backdoor
+12  A: 

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 Id Dont makes any change when i Abandone 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.
Dmytrii Nagirniak
and if i call session.clear() this you tells accures again. not?(all else raising Session_End event)
backdoor
Session.Clear will **only** remove items from the Session. Nothing more. You can call it as many times as needed.
Dmytrii Nagirniak
Added meatphora-comparisson.
Dmytrii Nagirniak
@AnthonyWJones, you are right "destroy" is incorrect to say. Better is REMOVE objects from session.But Session.Clear also does not destroy objects, it removes thm from session so they can be garbage collected.Also, storing COMPLEX objects is not recommended, otherwise I consider it fine.
Dmytrii Nagirniak
@Dmitriy: Why "storing COMPLEX objects is not recommended"?
Kamarey
@Dmitriy: Actually I had confused Session with the classic-asp session where is definitely not a good idea to store objects. I would agree though that complex or rather large objects stored in the session may result in scalability concerns.
AnthonyWJones
@Kamarey, storing objects in session MIGHT require it to be serialisable. Complex objects might not always be easily (de)serialised. In addition to that (de)serialisation of complex objects is time and resources consumming process. So in short, for performance and scalability reasons
Dmytrii Nagirniak
@Dmitriy: in this way if object is not disposable what happens?(do not implement IDisposable Interface)it just omits to be garbage collected?you sure?
backdoor
@Dmitriy: when i call session.abandone() even in next request session ID is same as when sesion is alive.
backdoor
@backdoor, I have added the explanation to my answer why you can have the same session id. I'll repeat: Session (and therfore its ID) never changes within one request. At the end of the request the session is thrown away. So *next* request will have new session and new id.
Dmytrii Nagirniak
@Dmitriy: and i tell my friend: when i call session.abandone() even in next request session ID is same as when sesion is alive (last post backs...)
backdoor
Should not be the case unless browser caches your page or cookie for some reason is not removed: http://technicalsol.blogspot.com/2008/07/understanding-sessionabandon.htmlhttp://bytes.com/topic/asp-classic/answers/470642-session-abandon-not-working
Dmytrii Nagirniak
Actually the session id can be reused (so you have the same id but different session). I added link to the full explanation to my answer.
Dmytrii Nagirniak
@ Dmitriy: by default SessionID will reused for creating Session on ASP.Net unless you configure it to dont do this. it means that in default situation session.abandon() and session.clear() have not difference else session.abandon() fires Session_End event..?
backdoor
No there is a difference. Clear removes all the data from session immidiately and preserves the session for further requests.Abandon removes the items at the end of the request, destoys the current session object. So in case you store session in the database you will end up with a new row for the session. While Clear will still use the same row. That is only ID that can be still the same.
Dmytrii Nagirniak
tanks i've got it...thus just 2 difference there are: 1- raising Session_End event2- clearing SessionObject at the end of processing page instead of immediatly not?
backdoor
Updated the answer again. Hope that is more clear.
Dmytrii Nagirniak
+1  A: 

clear-its remove key or values from session state collection..

abandon-its remove or deleted session objects from session..

maxy