Help - Best approach using Shared Classes ?

P

Paul

Hi.

Just trying to find out the best approach as I beleive it might give me
problems later on down the road.

I have an ASP.NET application which references a shared database class which
contains methods for serialising and de-serialising objects to the database
storage. I put this as a shared class as multiple web clients will be using
the class to store and retreive data, the problem I'm haivng now is that I
think multiple threads are overwritting the data.

In the database class its has some variables that store the tables name and
then some funcitons which execute requests against the table name, however
client A could be looking at table1 and client B will be looking at table2.
Now would the best approach be to use a SyncLock on the class before I run
the setting of the varibles and functions or get each client to initiate the
class with a Dim myClass as New MyClass ? What would be the best approach,
bering in mind that one clients requests may take a little while and the
Synclock would lock the class until this has completed and at this point we
may have 100 - 200 requests ???

An example being..............

Public MyClass
private shared TableName as string

public shared function SetTableName(s as String)
TableName = s
end function

public shared function DoSomething
Dim SQLString = "SELECT * from " + TableName
end function

End Class


Would I just use

SyncLock GetType(MyClass)
MyClass.SetTableName = "Table1"
MyClass.DoSomething
End Synclokc

or

Dim mc as New MyClass
MyClass.SetTableName = "Table1"
MyClass.DoSomething



Thanks in advance.
 
C

Christopher Kimbell

Hi,

Why would you want to share a class between several request threads?
Most managed dataproviders have support for connection-pooling, this cuts
down the cost of creating a database connection dramatically. It is not
reccommended to open a connection when the application starts and use this
for all database access.

For each web request, create your database object, open a connection, do
your stuff, close the connection then dispose of the database object. Each
request has its own set of data and you avoid threading issues.

Chris
 
R

Rick Spiewak

You should not keep tablenames in the shared variables. It's OK to have the
methods shared, but you'll need to have instance variables as well, and
allow the application to create a new instance for each table access.
 
S

Scott Allen

Paul:

You should seriously consider redesigning your data access class so it
is not shared. Unless you are purposefully trying to limit the number
of connections (which can be done another way) or restrict the amount
of database activity from your app, it's not worth the pain of writing
safe multithreaded code and the scalability limitations to serialize
all those requests through one object instance.

HTH,
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top