By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,948 Members | 838 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,948 IT Pros & Developers. It's quick & easy.

Help - Best approach using Shared Classes ?

P: n/a
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.
Nov 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
"Paul" <pa******************@vectra-it.co.uk> wrote in message
news:uK**************@TK2MSFTNGP12.phx.gbl...
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.

Nov 18 '05 #2

P: n/a
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.

"Paul" <pa******************@vectra-it.co.uk> wrote in message
news:uK**************@TK2MSFTNGP12.phx.gbl...
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.

Nov 18 '05 #3

P: n/a
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,

--
Scott
http://www.OdeToCode.com

On Fri, 30 Apr 2004 17:09:39 +0100, "Paul"
<pa******************@vectra-it.co.uk> wrote:
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.


Nov 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.