Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. threading issue

threading issue

Scheduled Pinned Locked Moved Database
databasehelpquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Laffis
    wrote on last edited by
    #1

    Hi, can anyone bring more insight as to make db access thread-safe programmatically? i.e. if there are multithreaded programme or even several programmes trying to access a same db, what do you guys usually do to prevent them conflicting each other?

    C 1 Reply Last reply
    0
    • L Laffis

      Hi, can anyone bring more insight as to make db access thread-safe programmatically? i.e. if there are multithreaded programme or even several programmes trying to access a same db, what do you guys usually do to prevent them conflicting each other?

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      Let the database sort it out. So long as you follow the following rules you shouldn't run into many problems: * Ensure that things are transacted properly. * Access the tables in the same order for each function. e.g. If some function access table A, B, and D and another function access tables B, C and D ensure that they are accessed in the order A, B, C, and D because if the first function accesses tables in the order D, B and A and the second function access the tables in the order B, C and D then you could run into deadlock. The first function locks table D, meanwhile the second function locks table B, the first function then wants to access table B but can't so it waits for the lock to be released. The second function then gets table C and then requests table D but it cannot because it is locked. The second function then waits for the lock on table D to be released. Now both functions cannot complete until the other has - they are deadlocked. The database will detect this and rollback one of the queries (the deadlock victim). Your application can then attempt again to run function that was the deadlock victim.


      My: Blog | Photos WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More

      L 1 Reply Last reply
      0
      • C Colin Angus Mackay

        Let the database sort it out. So long as you follow the following rules you shouldn't run into many problems: * Ensure that things are transacted properly. * Access the tables in the same order for each function. e.g. If some function access table A, B, and D and another function access tables B, C and D ensure that they are accessed in the order A, B, C, and D because if the first function accesses tables in the order D, B and A and the second function access the tables in the order B, C and D then you could run into deadlock. The first function locks table D, meanwhile the second function locks table B, the first function then wants to access table B but can't so it waits for the lock to be released. The second function then gets table C and then requests table D but it cannot because it is locked. The second function then waits for the lock on table D to be released. Now both functions cannot complete until the other has - they are deadlocked. The database will detect this and rollback one of the queries (the deadlock victim). Your application can then attempt again to run function that was the deadlock victim.


        My: Blog | Photos WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More

        L Offline
        L Offline
        Laffis
        wrote on last edited by
        #3

        thx! I was using mutex and 'd been doing locking in the code. Looks like it is unneccessary.

        T 1 Reply Last reply
        0
        • L Laffis

          thx! I was using mutex and 'd been doing locking in the code. Looks like it is unneccessary.

          T Offline
          T Offline
          ToddHileHoffer
          wrote on last edited by
          #4

          Use transactional error handling with sql server. Do this especially if you are upating tables that have a related tables. Declare @intErrorCode int select @intErrorCode = @@Error begin transaction If @intErrorCode = 0 begin -- insert SQL Statement set @intErrorCode = @@Error end If @intErrorCode = 0 begin -- insert another SQL Statement set @intErrorCode = @@Error end IF @Error = 0 commit transaction else rollback transaction return @Error "People who never make mistakes, never do anything." My blog http://toddsnotsoamazinglife.blogspot.com/

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups