Handle simultaneous access to database

I am making an application which will be running on serval pcs and access same database simultaneously. It makes more than one database access at a time on any single pc.

I need one of the access thread to get data from a data resouce and write the new data into the database to replace the old one. when this thread is getting and writing data to my database for an item, other threads accessing that item must wait till it finishes writing. The accessing threads are generated at runtime and the number of them is vary from time to time.

The pcs do not have knowledge to each other and there isn't any other machine or application working as a center to control the database access.

The pcs probably will work on the same network. but, this is not guranteed.

I also need to handle the failures. If the thread getting and writing data is blocked or dead, I need any one of the waiting thread to take the responsebility to get new data.

Could anyone give me some idea on how I can make this happen.

thanks



Answer this question

Handle simultaneous access to database

  • laboremus

    thanks

    this still has a problem.

    If the current updating thread is blocked or dead, i need one of the waiting threads to be the updating thread. So, I need to identify each thread.

    by the way, is there any other way to do this apart from the idea u said

    Thanks


  • IgorP

    One way to do it is to add a boolean column (call it IsWrite) in your Items table. Any thread writing to an item should set the IsWrite to 1 (but after it verifies that the IsWrite is 0 ex: Update Items Set IsWrite = 1 WHERE ItemID=@itemid AND IsWrite=0) before it start writing and sets it back to 0 after it ends (use try{ }finally{ } in your code). Any other thread wanting to read the same item, will check if IsWrite == 0 if it is not the case it should wait a random number of seconds (say 3 or 5) then try again.

  • Jeff Weber

    Enable the session in your webservice and when you perform a call to the webservice give a non null cookie container to the request.
    Here is the chain of action:
    For the 1st call, create a new CookieContainer
    Affect the CookieContainer to the request
    make the request
    when u get the result, get the CookieContainer back from the response and cache it on the client
    For subsequent webservice calls, use the same cached CookieContainer, this way the webserver will know that this is the same client that is making the request.

    GoodLuck


  • MikeLC#

    When you use try-finally block you are sure to have released the resources even if the thread encounters an exception and exits (=>DEAD).

    If you want to coordinates separated thread on different machines and different networks, you have to create a coordinator.
    each thread should send an "Alive" message to this coordinator (using .Net remoting) every laps of time (say 5min). If after this laps of time the coordinator didn't receive "alive" message from a thread it will consider it a dead one and will ask another thread on another machine to do the job.

    Of course this would require that each thread gets registered with the coordinator at startup. You can Identify the thread to the coordinator by the IP of their machine comined to the Thread ID or ThreadName

    Good luck


  • Pepp

    this is a web service application.

    for example, the service is running on serveral pcs. 10 user uses my webservice for same item in my database. the requests hit any of the pcs. my database is as a cache, caching data from different resoures. the first acess finds the data in database is out of date and decides to get newer data from resources. the rest of 9 must wait for it to finish or timeout. If it doesn't complete the update within timeout, another access should update and rest of 8 must wait and so on. The timeout one might recover and wait or kill itself. all access must fail themself if it couldn't get data within a period of time.

    the webservice is stateless, therefore has no knowledge of each other.

    How can I itdentify which access is updating.


  • Seppe001

    I'm not sure if I've fully understood your scenario but probably you can use a Mutex.



  • Handle simultaneous access to database