shared methods (MSDAAB)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tom

    shared methods (MSDAAB)

    Hello,

    I'm using the ms data access application blocks.
    My question is about the fact that everywhere shared
    methods are used.

    Does this mean that when more than 1 user at the same
    time executes the same function they can interfere with
    one another as these functions are the same for all
    instances of the class (and thus internal variables
    also?)?

    I'll explain with a simple example :
    user 1 and user 2 both want to call function A that
    returns a dataset based on a provided sql statement.

    1. user 1 calls function A
    2. function A does some internal processing (and has not
    yet reached the point where it gets the dataset) and sets
    internal variable to hold sql statement
    3. user 2 calls function A with different sql statement
    4. internal variable for sql statement of user 2 gets set
    5. function reaches the point for user 1 where it
    executes the return of the dataset --> Will it use the
    sql statement of user 1 or that of user 2 ??
    ....

    I hope you understand what I'm trying to explain and
    hopefully you can also explain to me if this scenario is
    possible or not?

    thanx,
    t
  • Jan Tielens

    #2
    Re: shared methods (MSDAAB)

    In that case you are dealing with re-entrance, which you should try to
    avoid! In VB.NET you can use the SyncLock statement (Allows statements to be
    synchronized on a single expression.) for helping you out:

    The SyncLock statement ensures that multiple threads do not execute the same
    statements at the same time. When the thread reaches the SyncLock block, it
    evaluates the expression and maintains this exclusivity until it has a lock
    on the object that is returned by the expression. This prevents an
    expression from changing values during the running of several threads, which
    can give unexpected results from your code.

    Note The type of the expression in a SyncLock statement must be a
    reference type, such as a class, a module, an interface, array or delegate.
    Example
    Class Cache
    Private Shared Sub Add(ByVal x As Object)
    SyncLock GetType(Cache)
    End SyncLock
    End Sub

    Private Shared Sub Remove(ByVal x As Object)
    SyncLock GetType(Cache)
    End SyncLock
    End Sub
    End Class


    --
    Greetz,
    Jan
    _______________ _______________ ____
    Read my weblog: http://weblogs.asp.net/jan
    "tom" <anonymous@disc ussions.microso ft.com> schreef in bericht
    news:008101c3cb 1e$72fdd100$a40 1280a@phx.gbl.. .[color=blue]
    > Hello,
    >
    > I'm using the ms data access application blocks.
    > My question is about the fact that everywhere shared
    > methods are used.
    >
    > Does this mean that when more than 1 user at the same
    > time executes the same function they can interfere with
    > one another as these functions are the same for all
    > instances of the class (and thus internal variables
    > also?)?
    >
    > I'll explain with a simple example :
    > user 1 and user 2 both want to call function A that
    > returns a dataset based on a provided sql statement.
    >
    > 1. user 1 calls function A
    > 2. function A does some internal processing (and has not
    > yet reached the point where it gets the dataset) and sets
    > internal variable to hold sql statement
    > 3. user 2 calls function A with different sql statement
    > 4. internal variable for sql statement of user 2 gets set
    > 5. function reaches the point for user 1 where it
    > executes the return of the dataset --> Will it use the
    > sql statement of user 1 or that of user 2 ??
    > ...
    >
    > I hope you understand what I'm trying to explain and
    > hopefully you can also explain to me if this scenario is
    > possible or not?
    >
    > thanx,
    > t[/color]


    Comment

    • Codemonkey

      #3
      Re: shared methods (MSDAAB)

      Tom,

      As Jan mentioned, re-entrance can be a bad thing if more than one thread
      changes a variable that is shared between threads.
      [color=blue]
      > 4. internal variable for sql statement of user 2 gets set[/color]

      By internal, do you mean local to the function or local to the class
      (shared)?

      If you mean local to the function, you should be fine. Each time a function
      (shared or not) is called, all its local variables get pushed on to the
      current thread's stack - two threads calling the same function should not be
      able to interfere with each other because they each get their own copy of
      the variables.

      However, if the variables are stored at the class level, you should use the
      synclock statement to make sure only one thread can access a variable at a
      time.

      Hope this helps,

      Trev.


      "tom" <anonymous@disc ussions.microso ft.com> wrote in message
      news:008101c3cb 1e$72fdd100$a40 1280a@phx.gbl.. .[color=blue]
      > Hello,
      >
      > I'm using the ms data access application blocks.
      > My question is about the fact that everywhere shared
      > methods are used.
      >
      > Does this mean that when more than 1 user at the same
      > time executes the same function they can interfere with
      > one another as these functions are the same for all
      > instances of the class (and thus internal variables
      > also?)?
      >
      > I'll explain with a simple example :
      > user 1 and user 2 both want to call function A that
      > returns a dataset based on a provided sql statement.
      >
      > 1. user 1 calls function A
      > 2. function A does some internal processing (and has not
      > yet reached the point where it gets the dataset) and sets
      > internal variable to hold sql statement
      > 3. user 2 calls function A with different sql statement
      > 4. internal variable for sql statement of user 2 gets set
      > 5. function reaches the point for user 1 where it
      > executes the return of the dataset --> Will it use the
      > sql statement of user 1 or that of user 2 ??
      > ...
      >
      > I hope you understand what I'm trying to explain and
      > hopefully you can also explain to me if this scenario is
      > possible or not?
      >
      > thanx,
      > t[/color]


      Comment

      • tom

        #4
        Re: shared methods (MSDAAB)

        Thanx Trev,

        You're comment helped a lot.

        By internal I indeed meant local to the function.
        I just was not sure if these local variables could be
        overwritten when using shared methods as I thought that
        only one instance of the function was stored.

        t

        [color=blue]
        >-----Original Message-----
        >Tom,
        >
        >As Jan mentioned, re-entrance can be a bad thing if more[/color]
        than one thread[color=blue]
        >changes a variable that is shared between threads.
        >[color=green]
        >> 4. internal variable for sql statement of user 2 gets[/color][/color]
        set[color=blue]
        >
        >By internal, do you mean local to the function or local[/color]
        to the class[color=blue]
        >(shared)?
        >
        >If you mean local to the function, you should be fine.[/color]
        Each time a function[color=blue]
        >(shared or not) is called, all its local variables get[/color]
        pushed on to the[color=blue]
        >current thread's stack - two threads calling the same[/color]
        function should not be[color=blue]
        >able to interfere with each other because they each get[/color]
        their own copy of[color=blue]
        >the variables.
        >
        >However, if the variables are stored at the class level,[/color]
        you should use the[color=blue]
        >synclock statement to make sure only one thread can[/color]
        access a variable at a[color=blue]
        >time.
        >
        >Hope this helps,
        >
        >Trev.
        >
        >
        >"tom" <anonymous@disc ussions.microso ft.com> wrote in[/color]
        message[color=blue]
        >news:008101c3c b1e$72fdd100$a4 01280a@phx.gbl. ..[color=green]
        >> Hello,
        >>
        >> I'm using the ms data access application blocks.
        >> My question is about the fact that everywhere shared
        >> methods are used.
        >>
        >> Does this mean that when more than 1 user at the same
        >> time executes the same function they can interfere with
        >> one another as these functions are the same for all
        >> instances of the class (and thus internal variables
        >> also?)?
        >>
        >> I'll explain with a simple example :
        >> user 1 and user 2 both want to call function A that
        >> returns a dataset based on a provided sql statement.
        >>
        >> 1. user 1 calls function A
        >> 2. function A does some internal processing (and has[/color][/color]
        not[color=blue][color=green]
        >> yet reached the point where it gets the dataset) and[/color][/color]
        sets[color=blue][color=green]
        >> internal variable to hold sql statement
        >> 3. user 2 calls function A with different sql statement
        >> 4. internal variable for sql statement of user 2 gets[/color][/color]
        set[color=blue][color=green]
        >> 5. function reaches the point for user 1 where it
        >> executes the return of the dataset --> Will it use the
        >> sql statement of user 1 or that of user 2 ??
        >> ...
        >>
        >> I hope you understand what I'm trying to explain and
        >> hopefully you can also explain to me if this scenario[/color][/color]
        is[color=blue][color=green]
        >> possible or not?
        >>
        >> thanx,
        >> t[/color]
        >
        >
        >.
        >[/color]

        Comment

        Working...