threading issue
-
-
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?
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
-
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
-
thx! I was using mutex and 'd been doing locking in the code. Looks like it is unneccessary.
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/