c# - How to solve threads conflict because of long time cost of SQL statements? -


i'm going insert data database o database a,and write logs database b.and now,i have function this:

list=databaseo.getdata(); if(list.id not in databaseb) {     insert data databasea;     insert log databaseb; } 

it runs want.but when put multiple-thread program,it,however,goes wrong. logs big,so cost lot of time insert database b.when thread no.1 trying insert log,thread no.2 tries find whether list.id in batabase b.so,i 2 same data in databasea.how solve this?

ps,i'm using c#

you lock operations using lock. way can let threads wait on eachother. example code like:

list=databaseo.getdata(); if(list.id not in databaseb) { lock(thelock) {         insert data databasea; } lock (thelock) {         insert log databaseb; } } 

you have declare 'thelock' somewhere before start threads , make accessible. lock object type of object, tend use basic object it.

note lock both operations in same lock if want whole process done (write data database , write logs b). in either case multi threading won't give of performance boost threads waiting on each other access database.


Comments