Help - Best approach using Shared Classes ?

Discussion in 'ASP .Net' started by Paul, Apr 30, 2004.

  1. Paul

    Paul Guest

    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.
     
    Paul, Apr 30, 2004
    #1
    1. Advertising

  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" <> wrote in message
    news:...
    > 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.
    >
    >
     
    Christopher Kimbell, Apr 30, 2004
    #2
    1. Advertising

  3. Paul

    Rick Spiewak Guest

    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" <> wrote in message
    news:...
    > 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.
    >
    >
     
    Rick Spiewak, Apr 30, 2004
    #3
  4. Paul

    Scott Allen Guest

    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"
    <> 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.
    >
     
    Scott Allen, Apr 30, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DJ Dev
    Replies:
    3
    Views:
    16,240
    Gandalf
    Feb 8, 2004
  2. Joe Fallon
    Replies:
    3
    Views:
    788
    =?Utf-8?B?Z3V5?=
    Jul 16, 2004
  3. ben
    Replies:
    3
    Views:
    532
    Kevin Spencer
    Nov 15, 2004
  4. mrhicks
    Replies:
    1
    Views:
    4,933
    Andrey Tarasevich
    May 20, 2004
  5. mca
    Replies:
    0
    Views:
    110
Loading...

Share This Page