473,653 Members | 3,015 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.SetTabl eName = "Table1"
MyClass.DoSomet hing
End Synclokc

or

Dim mc as New MyClass
MyClass.SetTabl eName = "Table1"
MyClass.DoSomet hing

Thanks in advance.
Jul 21 '05 #1
6 1854
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******** ******@TK2MSFTN GP12.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.SetTabl eName = "Table1"
MyClass.DoSomet hing
End Synclokc

or

Dim mc as New MyClass
MyClass.SetTabl eName = "Table1"
MyClass.DoSomet hing

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******** ******@TK2MSFTN GP12.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.SetTabl eName = "Table1"
MyClass.DoSomet hing
End Synclokc

or

Dim mc as New MyClass
MyClass.SetTabl eName = "Table1"
MyClass.DoSomet hing

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******** ******@TK2MSFTN GP12.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.SetTabl eName = "Table1"
MyClass.DoSomet hing
End Synclokc

or

Dim mc as New MyClass
MyClass.SetTabl eName = "Table1"
MyClass.DoSomet hing

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******** ******@TK2MSFTN GP12.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.SetTabl eName = "Table1"
MyClass.DoSomet hing
End Synclokc

or

Dim mc as New MyClass
MyClass.SetTabl eName = "Table1"
MyClass.DoSomet hing

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.SetTabl eName = "Table1"
MyClass.DoSomet hing
End Synclokc

or

Dim mc as New MyClass
MyClass.SetTab leName = "Table1"
MyClass.DoSome thing

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.SetTabl eName = "Table1"
MyClass.DoSomet hing
End Synclokc

or

Dim mc as New MyClass
MyClass.SetTab leName = "Table1"
MyClass.DoSome thing

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
2139
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 access module that creates a large disconnected dataset from a database. I want to pass that dataset back to the calling program. And then perhaps I want to send that dataset to another class module. At first it seems that the "object oriented"...
16
3018
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 example dsParts.xsd and including that in the data tier. I then will create a class that looks like this Public Class CPart Inherits dsParts
3
1162
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 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...
1
1229
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 realize this is an obtuse request, but I just want to follow some best practices. Most of the examples I see have a micro focus on one particular aspect or another of technologies used in an asp.net application. If I simply aggragate all of those...
20
6590
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 logging onto the web server using LDAP for authentication, do most people 1) have the web server connecting to the database using its own user account (possibly through ident), and controlling access to different database entities strictly through...
55
3299
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 call operations on Controls from a delegate, otherwise it does not work. However each time I've done an operation, I must update the progressbar and progresslabel, but this cannot be done in the delegate as it does not work.
13
2154
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 results as it is. I mean, for instance, something like this. namespace DAL {
16
2793
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 efficient navigating within Visual Studio 2005. Let's say your project (or solution) has dozens of forms and hundreds or even thousands of routines. Two Questions: 1) BUILT-IN to Visual Studio 2005. What ideas do you have to quickly
4
2891
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 could update the DLLs independently of the main EXE, and performance was not a problem. We want to do the same thing in C# (at least test it), but we are not exactly clear what the best practice is. We would prefer to do something similar, but we...
0
8283
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8470
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8590
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7302
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6160
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4147
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4291
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2707
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1914
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.