shared methods (MSDAAB)

  • Thread starter Thread starter tom
  • Start date Start date
T

tom

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
 
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:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vastmSyncLock.asp
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
 
Tom,

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

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.
 
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
 
Back
Top