Variables in multiple sessions

Discussion in 'ASP .Net' started by Jacques Oberto, Oct 1, 2009.

  1. Hi All,

    [newbie question]
    My ASP.NET page does some processing using
    data from user input. In the case of multiple
    simultanous sessions, will the internal variables
    of my application remain isolated between sessions
    or is there danger of data mixing?
    Thanks,

    Jacques
     
    Jacques Oberto, Oct 1, 2009
    #1
    1. Advertising

  2. Jacques Oberto

    Scott M. Guest

    Each page request, results in a specific instance of a page class being
    created. As with any instance of an object, it's data is isolated from data
    in other instances (even if it is another instance of the same kind of
    object).

    The only time you'll have to worry about one user accessing data that
    another use may also be using is when you have a page access a "shared"
    memory space, such as application variables or shared data, such as a
    database.

    -Scott

    "Jacques Oberto" <> wrote in message
    news:...
    > Hi All,
    >
    > [newbie question]
    > My ASP.NET page does some processing using
    > data from user input. In the case of multiple
    > simultanous sessions, will the internal variables
    > of my application remain isolated between sessions
    > or is there danger of data mixing?
    > Thanks,
    >
    > Jacques
    >
    >
     
    Scott M., Oct 1, 2009
    #2
    1. Advertising

  3. On Oct 1, 5:17 pm, "Jacques Oberto" <> wrote:
    > Hi All,
    >
    > [newbie question]
    > My ASP.NET page does some processing using
    > data from user input. In the case of multiple
    > simultanous sessions, will the internal variables
    > of my application remain isolated between sessions
    > or is there danger of data mixing?
    > Thanks,
    >
    > Jacques


    Yes, each active session is identified and tracked using a 120-bit
    SessionID, generated using an algorithm that guarantees uniqueness.
    The SessionID strings are communicated across client-server requests
    either by means of an HTTP cookie or a modified URL with the SessionID
    string embedded, depending on how you configure the application
    settings.

    More: http://msdn.microsoft.com/en-us/library/87069683.aspx
     
    Alexey Smirnov, Oct 1, 2009
    #3
  4. Jacques Oberto

    Scott M. Guest

    "Alexey Smirnov" <> wrote in message
    news:...
    On Oct 1, 5:17 pm, "Jacques Oberto" <> wrote:
    > Hi All,
    >
    > [newbie question]
    > My ASP.NET page does some processing using
    > data from user input. In the case of multiple
    > simultanous sessions, will the internal variables
    > of my application remain isolated between sessions
    > or is there danger of data mixing?
    > Thanks,
    >
    > Jacques


    >Yes, each active session is identified and tracked using a 120-bit
    >SessionID, generated using an algorithm that guarantees uniqueness.
    >The SessionID strings are communicated across client-server requests
    >either by means of an HTTP cookie or a modified URL with the SessionID
    >string embedded, depending on how you configure the application
    >settings.


    >More: http://msdn.microsoft.com/en-us/library/87069683.aspx


    I don't believe the OP is asking about session object per se, I believe he's
    just asking about the VB or C# code variables that are part of the page's
    code, which are contained within each instance of the page.

    -Scott
     
    Scott M., Oct 1, 2009
    #4
  5. "Scott M." a écrit
    > Each page request, results in a specific instance of a page class being
    > created. As with any instance of an object, it's data is isolated from
    > data in other instances (even if it is another instance of the same kind
    > of object).
    >
    > The only time you'll have to worry about one user accessing data that
    > another use may also be using is when you have a page access a "shared"
    > memory space, such as application variables or shared data, such as a
    > database.
    >
    > -Scott
    >


    Hi Scott,

    No offense meant but your reply is confusing me. If the page data is
    isolated
    from data of other page instances, are the application variables isolated as
    well or not?
    Thanks,

    Jacques
     
    Jacques Oberto, Oct 1, 2009
    #5
  6. Jacques Oberto

    Patrice Guest

    What are those "internal variables" ?

    Static (or VB Shared members) and what you stored in application variables
    or in the cache object is shared accross the application (and ASP.NET is a
    single app used by multiple users so all users will use the same value). In
    other cases, it (such as for session variable) it should be fine.

    If you have a problem, tell us what you see, it will be easier to diagnose
    than talking about generalities...

    --
    Patrice


    "Jacques Oberto" <> a écrit dans le message de groupe de
    discussion : ...
    > Hi All,
    >
    > [newbie question]
    > My ASP.NET page does some processing using
    > data from user input. In the case of multiple
    > simultanous sessions, will the internal variables
    > of my application remain isolated between sessions
    > or is there danger of data mixing?
    > Thanks,
    >
    > Jacques
    >
    >
    >
     
    Patrice, Oct 1, 2009
    #6
  7. Jacques Oberto

    Scott M. Guest

    "Jacques Oberto" <> wrote in message
    news:...
    >
    > "Scott M." a écrit
    >> Each page request, results in a specific instance of a page class being
    >> created. As with any instance of an object, it's data is isolated from
    >> data in other instances (even if it is another instance of the same kind
    >> of object).
    >>
    >> The only time you'll have to worry about one user accessing data that
    >> another use may also be using is when you have a page access a "shared"
    >> memory space, such as application variables or shared data, such as a
    >> database.
    >>
    >> -Scott
    >>

    >
    > Hi Scott,
    >
    > No offense meant but your reply is confusing me. If the page data is
    > isolated
    > from data of other page instances, are the application variables isolated
    > as
    > well or not?
    > Thanks,
    >
    > Jacques
    >

    Well, I think you need to clarify what type of "variables" you are referring
    to. In your original post, you did not mention anything about "application"
    variables, which are different from "session" variables, which are different
    from the variables you create within your C# or VB .NET code to process the
    page.

    Application variables are just that, variables that hold data that is
    specfic to the application and not any particular user. A hit count is a
    good example of data stored at the applicaiton level. It doesn't matter
    which user is asking for it, there is only one number that represents the
    current hit count and that data needs to be shared across all user sessions.
    Applicaiton variables look like this: Application("HitCount") = 1000.

    Session variables are also what their name suggests. They are variables that
    hold data that is specific to a particular user's session and is held in
    memory for the duration of the session. Session variables are used when data
    from one request/response cycle needs to be carried into the same visitors
    next request/response cycle that is part of the same server session.
    Session variables look like this: Session("UserName") = txtUser.Text.

    Regular page variables (which is what I thought you were asking about and
    what my reply is based on) are the simple variables you create within the
    code of your page such as:

    Public Class thePage
    Inherits System.Web.UI.Page

    Dim x As DateTime

    Sub Page_Load()
    x = DateTime.Now
    End Sub

    End Class

    In this case "x" is a page level variable that holds data specific to this
    particular "instance" of the page class and would hold data that is isolated
    from other instances of the same page class. If I requested the page and
    then you requested the page, the server would create two different instances
    of "thePage" class and each instance would have different data in its x
    variable that cannot be accessed by the other.

    -Scott
     
    Scott M., Oct 1, 2009
    #7
  8. "Scott M." a écrit

    >
    > Public Class thePage
    > Inherits System.Web.UI.Page
    >
    > Dim x As DateTime
    >
    > Sub Page_Load()
    > x = DateTime.Now
    > End Sub
    >
    > End Class
    >
    > In this case "x" is a page level variable that holds data specific to this
    > particular "instance" of the page class and would hold data that is
    > isolated from other instances of the same page class. If I requested the
    > page and then you requested the page, the server would create two
    > different instances of "thePage" class and each instance would have
    > different data in its x variable that cannot be accessed by the other.
    >


    Hi Scott,

    As I mentioned in my OP, it's the "application variables" I'm interested in.
    Your example above makes everything crystal clear.
    Thank you for your help.

    Jacques
     
    Jacques Oberto, Oct 1, 2009
    #8
  9. "Patrice" a écrit dans
    > What are those "internal variables" ?
    >
    > Static (or VB Shared members) and what you stored in application variables
    > or in the cache object is shared accross the application (and ASP.NET is a
    > single app used by multiple users so all users will use the same value).
    > In other cases, it (such as for session variable) it should be fine.
    >
    > If you have a problem, tell us what you see, it will be easier to diagnose
    > than talking about generalities...



    Hi Patrice,

    What you are saying seems to differ from the VB example + explanation given
    by Scott M in this same thread. It looks like I am not the only one to have
    problems with this...

    Jacques
     
    Jacques Oberto, Oct 1, 2009
    #9
  10. "Jacques Oberto" <> wrote in news:ea6jTpqQKHA.5032
    @TK2MSFTNGP05.phx.gbl:

    > Hi All,
    >
    > [newbie question]
    > My ASP.NET page does some processing using
    > data from user input. In the case of multiple
    > simultanous sessions, will the internal variables
    > of my application remain isolated between sessions
    > or is there danger of data mixing?


    If you have attached the data to a session, then they cannot cross
    contaminate via the session variables.

    Except, if you have a piece of state (Property/Field) marked as static
    (C#) or Shared (VB), as those are part of the class and not part of the
    object created from the class (ie, they are shared amongst all
    instances).

    In other words:

    public class MyClass
    {
    public static MyVariable { get; set; }
    }

    will shared across all instances, even if you have the following:

    Session["MyClass"] = instanceOfMyClass;

    Peace and Grace,


    --
    Gregory A. Beamer
    MVP; MCP: +I, SE, SD, DBA

    Twitter: @gbworld
    Blog: http://gregorybeamer.spaces.live.com

    *******************************************
    | Think outside the box! |
    *******************************************
     
    Gregory A. Beamer, Oct 1, 2009
    #10
  11. "Gregory A. Beamer" a écrit
    > If you have attached the data to a session, then they cannot cross
    > contaminate via the session variables.
    >
    > Except, if you have a piece of state (Property/Field) marked as static
    > (C#) or Shared (VB), as those are part of the class and not part of the
    > object created from the class (ie, they are shared amongst all
    > instances).
    >
    > In other words:
    >
    > public class MyClass
    > {
    > public static MyVariable { get; set; }
    > }
    >
    > will shared across all instances, even if you have the following:
    >
    > Session["MyClass"] = instanceOfMyClass;
    >


    I got it now. Thanks Gregory.

    Jacques
     
    Jacques Oberto, Oct 1, 2009
    #11
  12. Jacques Oberto

    Scott M. Guest

    "Jacques Oberto" <> wrote in message
    news:...
    >
    > "Patrice" a écrit dans
    >> What are those "internal variables" ?
    >>
    >> Static (or VB Shared members) and what you stored in application
    >> variables or in the cache object is shared accross the application (and
    >> ASP.NET is a single app used by multiple users so all users will use the
    >> same value). In other cases, it (such as for session variable) it should
    >> be fine.
    >>
    >> If you have a problem, tell us what you see, it will be easier to
    >> diagnose than talking about generalities...

    >
    >
    > Hi Patrice,
    >
    > What you are saying seems to differ from the VB example + explanation
    > given
    > by Scott M in this same thread. It looks like I am not the only one to
    > have
    > problems with this...
    >
    > Jacques


    Patrice is talking about yet another form of data storage, but not quite a
    variable. It doesn't conflict with what I've said it's just an additional
    way that data can be shared. It doesn't change my previous description.

    This really isn't something that there is a lot of confusion on. You pick
    the right kind of storage for the persistence you need.

    Applicaiton level persistence = appliation variables
    per user persistence = session variables / cookies
    per page persistence = page level variables

    -Scott
     
    Scott M., Oct 1, 2009
    #12
  13. Jacques Oberto

    Patrice Guest

    As Scott said, it's just that "internal variable" is no clear enough.


    I didn't catch that "internal variables of my application" were just what we
    call just "application variables" (i.e. stored in the Application object)
    and I was suspected you were using shared or static variables (which is
    still something else)...


    They are specifically done to be shared accross the whole application so if
    this is a "danger" you shoudl use something else...
    --
    Patrice


    "Jacques Oberto" <> a écrit dans le message de groupe de
    discussion : ...
    >
    > "Patrice" a écrit dans
    >> What are those "internal variables" ?
    >>
    >> Static (or VB Shared members) and what you stored in application
    >> variables or in the cache object is shared accross the application (and
    >> ASP.NET is a single app used by multiple users so all users will use the
    >> same value). In other cases, it (such as for session variable) it should
    >> be fine.
    >>
    >> If you have a problem, tell us what you see, it will be easier to
    >> diagnose than talking about generalities...

    >
    >
    > Hi Patrice,
    >
    > What you are saying seems to differ from the VB example + explanation
    > given
    > by Scott M in this same thread. It looks like I am not the only one to
    > have
    > problems with this...
    >
    > Jacques
     
    Patrice, Oct 2, 2009
    #13
  14. "Patrice" a écrit

    > I didn't catch that "internal variables of my application" were just what
    > we call just "application variables" (i.e. stored in the Application
    > object) and I was suspected you were using shared or static variables
    > (which is still something else)...
    >
    >
    > They are specifically done to be shared accross the whole application so
    > if this is a "danger" you shoudl use something else...



    Hi Patrice,

    Please notice that your reply can be interpreted <again> in opposite ways.
    "They" in the second paragraph refers to what?
    -application variables
    or
    -shared and static variables

    ????

    Thanks,

    Jacques
     
    Jacques Oberto, Oct 2, 2009
    #14
  15. Jacques Oberto

    Patrice Guest

    > Please notice that your reply can be interpreted <again> in opposite ways.
    > "They" in the second paragraph refers to what?
    > -application variables
    > or
    > -shared and static variables


    Both, actually "application variables" are stored in a container that is
    available accross the whole application. So all sessions will have access to
    them.

    A static or shared member is a class member. As a class (and not instance)
    member it has a single value that is available to the whole application (and
    as an ASP.NET app is a single application, all sessions will have access to
    them).

    I realize also that "Application variables" have a specific meaning when
    used in the context of ASP/NET :
    http://support.microsoft.com/kb/309018/en-us (that is variable stored in the
    "Application" container)

    Sorry for being unclear.
    --
    Patrice
     
    Patrice, Oct 2, 2009
    #15
  16. Jacques Oberto

    Scott M. Guest

    "Jacques Oberto" <> wrote in message
    news:%...
    >
    > "Patrice" a écrit
    >
    >> I didn't catch that "internal variables of my application" were just what
    >> we call just "application variables" (i.e. stored in the Application
    >> object) and I was suspected you were using shared or static variables
    >> (which is still something else)...
    >>
    >>
    >> They are specifically done to be shared accross the whole application so
    >> if this is a "danger" you shoudl use something else...

    >
    >
    > Hi Patrice,
    >
    > Please notice that your reply can be interpreted <again> in opposite ways.
    > "They" in the second paragraph refers to what?
    > -application variables
    > or
    > -shared and static variables
    >
    > ????
    >
    > Thanks,
    >
    > Jacques



    A "shared" or "static" piece of data, which *could* be called a variable,
    but is more often referred to as a class level "field" or "property" is
    simply a piece of data that resides with the type and no particular
    "instance" of that type.

    While I don't think this is what you initially were asking about, since it's
    come up, you should be clear on what it is.

    The best example of this would be getting the current date and time. Rather
    than making an "instance" of a DateTime type, you access the "Now" property
    directly against the type without making an instance of it, as in:

    DateTime.Now

    Rather than:

    Dim x As New DateTime
    Dim theTime as DateTime = x.Now

    Since anyone wanting the current date and time would need the same
    information (albeit a changing peice of information) anyone just gets the
    data directly from the type and as the data in the type changes (in this
    case the current time is changing all the time), you don't go through an
    instance to get the data, you just get it through the type.

    In this way, the data is "shared" across all instances of the type and if
    the data gets changed anywhere, it gets changes everywhere.

    -Scott
     
    Scott M., Oct 2, 2009
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ken Cox [Microsoft MVP]

    Re: Relationship between IIS Sessions and ASP.NET Sessions?

    Ken Cox [Microsoft MVP], Aug 8, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    5,373
    Luther Miller
    Aug 8, 2003
  2. jty202

    Question on Sessions variables

    jty202, Jan 17, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    367
    Stuart A Hill
    Jan 17, 2005
  3. Replies:
    9
    Views:
    960
  4. scottymo
    Replies:
    3
    Views:
    707
    Dominick Baier
    Sep 30, 2006
  5. Bookham Measures

    Moving from ASP Sessions to Database Sessions

    Bookham Measures, Jul 23, 2007, in forum: ASP General
    Replies:
    19
    Views:
    569
    Bookham Measures
    Aug 23, 2007
Loading...

Share This Page