Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Concurrency issue using datatable in business object

Posted on 2006-03-24
Medium Priority
Last Modified: 2010-08-05
I've got a business object built around a datatable, that also has an "index" made of a hashtable filled with sorted lists.  I store the business object in a public shared variable between page cycles.

What are some possible approaches and techniques that could be used to "lock" the business object to make sure that it isn't being updated simultaneously, possibly damaging the index and/or index-datatable relationship?

Any guidance on this would be appreciated.

Question by:codequest
  • 5
  • 3
LVL 12

Expert Comment

ID: 16282090
Oh wow, that's confusing, this changed!

I'll start over.

LVL 12

Expert Comment

ID: 16282110
You can do almost exactly what you are saying. If your BO is a shared object between multiple logged on users, you can store it in the ApplicationState. Then when you access it, you should do:

BO myBO = (BO)Application["myBO"];
lock (BO.SyncRoot)
   // do stuff here

Is it an object that is shared between lots of sessions?


Author Comment

ID: 16283689
Thanks for the response.  My apologies for the mid-flight change...I thought the orginal question meandered too much without getting to the point.

Your answer looks about right.   I'd appreciate it if you could fill in a couple more blanks...(& BTW, I'm using VB, though I can read the C#...sort of!)

An example from MSDN   at  http://msdn2.microsoft.com/en-us/library/system.collections.queue.syncroot.aspx:

Dim myCollection As New Queue()
 Dim item As Object
 SyncLock myCollection.SyncRoot
  For Each item In myCollection
  ' Insert your code here.
  Next item
 End SyncLock

which I would extrapolate in my case to be used like:

Public Function X()                      '<<< any function that's going to access and change the object
dim wrkObj = GetObj("myObj")
SyncLock wrkObj.SyncRoot

   (do whatever to/with wrkObj)
   PutObj("myObj") = wrkObj

End SyncLock
End Function

Q:  just to verify:  is the above the same thing as you're suggesting (only written in VB?)

The reason I use GetObj and PutObj (or whatever) here is that I've tested Application cache versus public shared a couple of times now ('cause I can't remember, or I don't believe the result) and I keep getting that public shared is much faster (see test code and result below).

Q:  Is there some other reason to use Application cache instead of a public shared variable?  They both seem to persist that same (but do they?), and I don't expect there will be multiple programmers on this (without a complete overhaul of the code anyway.)

Q:  Does the SyncRoot.Lock (more or less)
    a) automagically lock the object,
    b) put other threads that want the object into some kind of "wait until the first thread is done with it" state,
    c) let them know when SyncRoot lock has ended, and
    d) let the processor do whatever with the cycles while it's waiting...
    (dimly, I remember something about OS queues, interrupts, I/O waits and so on....so maybe SyncRoot uses/does something like that....)

I'll do some research on SyncRoot.

Any other comments on this would be welcome.  


===========  App cache versus public shared time test ================
(vb code behind a page with one button to launch this code)
Imports System.Diagnostics
Partial Class Pages_Test2
    Inherits System.Web.UI.Page
    Public Shared HT As New Hashtable
    Dim AC = System.Web.HttpContext.Current.Application

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim t1a, t1b, t2a, t2b As Double
        Dim w1 As String
        Dim w2 As String
        Dim c As Integer = 10000

        t1a = Timer
        For n As Integer = 1 To c      
            w1 = AC("A")
            w2 = w1
            AC("A") = w2
        t1b = Timer

        t2a = Timer
        For n As Integer = 1 To c * 10     '<<<< NOTE FACTOR OF TEN HERE ON PUBLIC SHARED LOOP
            w1 = HT("A")
            w2 = w1
            HT("A") = w2
        t2b = Timer

        Debug.WriteLine((t1b - t1a) & "  " & (t2b - t2a))

    End Sub
End Class
RESULT      t1b-t1a = 1.59375     t2b-t2a = 0.03125    

again, this is with the public shared variant doing 10x more loops


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


Author Comment

ID: 16283722
And yes, sorry, dozens of users operating on the business object at the same time....

Author Comment

ID: 16286131
Synclock description very straightforward (answers 3rd Q above)

LVL 12

Accepted Solution

AGBrown earned 2000 total points
ID: 16298950
Here we go then (Monday eugh)
(1) and (3): Yes. SyncLock x.SyncRoot is the same as the lock(SyncRoot). SyncRoot is the field name of an object in the class that you want to access. This object is only used for locking and nothing else. As such it is a way of locking a class that steps around certain deadlock scenarios rather than putting the lock on the class instance itself. If I wanted to provide a way of locking a class that would be accessed by multiple threads, I would simply declare a public field, which is commonly called SyncRoot:

Dim SyncRoot As Object = new Object()

That is all that happens.

You can view a lock statement exactly as you say. The first object to get a lock on the "SyncRoot" is then able to complete the block of code in the lock before any other object in any other thread can enter the lock and start doing the same.

(2) public shared hashtable vs. ApplicationState. Well, this is stretching my VB.NET knowledge a little. AFAIK a shared field is accessible by all instances of a given class. It is the equivalent to the C# static keyword.

I have never even considered using a shared variable to provide access to information that should persist between postbacks. I don't know why. In terms of arguments for or against, I can't honestly think of many other than that the ApplicationState is a centralised persistent store on the server that all pages can access. In terms of loosely coupled design, it is therefore a good thing. However, if you have an item on a page that needs to persist between postbacks, and is only accessed by instances of that particular page (and is not particular to a given user Session, or a particular disconnected instance of a page) then I guess you could indeed use Shared variables.


Author Comment

ID: 16303066
Thanks for the extra info on this.  Big help!

Author Comment

ID: 16345097
I think I discovered an important consideration in why to use Cache over other means for persistence between page cycles.

The system will automatically delete stuff from the Cache if it needs memory.   This means you have to test to see if your dataset is there, each time you access Cache to get the dataset.   However, it means you don't have to worry about memory usage causing the server to have problems.  

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month13 days, 21 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question