views:

51

answers:

3

Is this the right way of declaration dbreaders when mulitple users access the same page?

public dbReader as system.Data.IDataReader at class level or

Dim dbReader as System.Data.IDataReader in each function inside a class.

What would be the best practice to make the dbReader thread safe in VB.Net?

Does declaring them as static makes it thread safe?

Thanks in Advance,

+1  A: 

I would recommend you using reentrant functions when possible which are thread safe by definition instead of using class fields:

Function GetIds() As IEnumerable(Of Integer)
    Dim result = New List(Of Integer)()
    Using conn = New SqlConnection("SomeConnectionString")
        Using cmd = conn.CreateCommand()
            conn.Open()
            cmd.CommandText = "SELECT id FROM foo"
            Using reader = cmd.ExecuteReader()
                While reader.Read()
                    result.Add(reader.GetInt32(0))
                End While
            End Using
        End Using
    End Using
    Return result
End Function
Darin Dimitrov
I use DAAB to open and close connections. And then call that data access method which in turn returns resultset as dbreader.From aspx page I will declare a dbreader , get that data and use it. for example
sony
If sClose = 1 And iAssnKey > 0 Then dbReader = DAL.GetAssnCtrlPKey(ClientKey, iAssnKey) If Validation.Validate_DataReader(dbReader) Then If dbReader.Read = True Then If DB_Objects.Convert_objToBool(dbReader.Item("Active")) = False Then sClose = 0 End If End If End If Validation.Close_DataReader(dbReader) End If
sony
You shouldn't be closing connections. You should simply dispose them as in my example so that they are properly returned to the connection pool. No class/static fields needed.
Darin Dimitrov
so first when I call a data access method it opens the connection returns the data . Once I use the data I should dispose the dbreader.Like dbReader.Dispose(). And I do not close connection anywhere explicity.
sony
Just use `Using` as in my example. It will take care of everything. You don't need to explicitly call `reader.Dispose`.
Darin Dimitrov
So I should use using statement in the DAAB data access method in mycase http://social.msdn.microsoft.com/Forums/en/adodotnetdataproviders/thread/25b9bcdc-5aca-4831-a80d-a1f981e2ee64 Darin if you can see this link where I posted an issue, basically I posted the current thread in relation to the one on the link. If can give me an idea if this because of thread safe issue. that would be great.
sony
Thank you Darin
sony
A: 

If you are Diming the variable in a function, no other thread can access that variable making it thread-safe by definition.

However, if you are declaring it at a class level, you might want to use SyncLock which will prevent other threads from accessing it if it is currently being used by an other one.

Example:

Public Sub AccessVariable()
    SyncLock Me.dbReader
         'Work With dbReader
    End SyncLock
End Sub
Andrew Moore
so if another thread is also accessing the same page same reader and trying to update or insert that reader which is coded as Synlock. Would that result in any inconsistencies of data being retrieved .
sony
@sony: Not if you use `SyncLock` like above everytime to work with the variable. `SyncLock` is made to prevent two threads from working on the same variable. The second thread will sleep until the lock is released.
Andrew Moore
Ok that helps. and what do you suggest , say I have 3 functions inside a class. Each of these functions call a different dataaccess method and gets the result set as data reader. Now I declare a data reader in the aspx page and say like dbreader=GetFunction1Data(pkey).
sony
Now inorder to open the reader thread safe mode , I should put it this way. public sub function1()
sony
synclock Me.dbreader 'work with dbreader end synclock end sub
sony
At the sametime a second function call a datareader in another thread , it will not interrupt the one already started.
sony
Is this right that I have to put the synclock in the aspx page?
sony
And how should the dbreader should be declared at class level or the function level on the aspx page.
sony
@sony: It doesn't matter if you use different functions. As long as you are `SyncLock` using the same object, you'll be fine. It only interrupts (actually, pauses) the second operation until the first one frees the lock. If you need access at multiple locations, then use class level variable. If you need only function level access, don't use SyncLock and `Dim` in the function. A function can only be executed by one thread at a time (well, if another thread runs the same function, it will be a totally different dbReader variable).
Andrew Moore
Thankyou Andrew
sony
+1  A: 
Alex Essilfie
I think this would be of great help.
sony