473,385 Members | 1,772 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Help - Best approach using Shared Classes ?

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.
Jul 21 '05 #1
6 1837
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.

Jul 21 '05 #2
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.

Jul 21 '05 #3
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.

Jul 21 '05 #4
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.

Jul 21 '05 #5
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.


Jul 21 '05 #6
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.


Jul 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Chuck Ritzke | last post by:
I keep asking myself this question as I write class modules. What's the best/smartest/most efficient way to send a large object back and forth to a class module? For example, say I have a data...
16
by: D Witherspoon | last post by:
I am developing a Windows Forms application in VB.NET that will use .NET remoting to access the data tier classes. A very simple way I have come up with is by creating typed (.xsd) datasets. For...
3
by: Paul | last post by:
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...
1
by: epigram | last post by:
I'm creating a data-centric asp.net application that will be using SQL Server 2000. I'm looking for some articles, design tips, etc. to help me decide how I should design my application. I...
20
by: Keith G. Murphy | last post by:
I'm trying to get a feel for what most people are doing or consider best practice. Given a mod_perl application talking to a PostgreSQL database on the same host, where different users are...
55
by: Sam | last post by:
Hi, I have a serious issue using multithreading. A sample application showing my issue can be downloaded here: http://graphicsxp.free.fr/WindowsApplication11.zip The problem is that I need to...
13
by: andrea | last post by:
Sorry for the stupid question, I know, but sometimes is necessary starts from the basic. I don't know how to pass the result of a method generated from a DAL class to a BL class returning the...
16
by: Rex | last post by:
Hi All - I have a question that I think MIGHT be of interest to a number of us developers. I am somewhat new to VIsual Studio 2005 but not new to VB. I am looking for ideas about quick and...
4
by: =?Utf-8?B?bW9mbGFoZXJ0eQ==?= | last post by:
In VB6, we created a number of ActiveX DLLs that all shared a similar interface. The main application would load these in dynamically (late-bound.) This worked well for our situation because we...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.