c# - parallel.for loop giving timeout exception -
i using parallel.for make concurrent calls following.
sqlconnection connection = new sqlconnection(connectionstring); connection.open(); sqlcommand cmd = new sqlcommand(); //sqldatareader reader; cmd.commandtype = commandtype.text; cmd.connection = connection; parallel.for(0, 100, delegate(int i) { //insert database; cmd.commandtext = string.format("insert service_loadtest(id,servicecallcount) values ({0},'call_{1}')", i, i); cmd.executenonquery(); });
after inserting counts upto 70 ,i getting timeout exception "timeout expired. timeout period elapsed prior completion of operation or server not responding.".i have set connetion string timeout property,but of no luck. please help.
your code isn't thread-safe. try moving code loop:
parallel.for(0, 100, delegate(int i) { using (sqlconnection connection = new sqlconnection(connectionstring)) { connection.open(); sqlcommand cmd = new sqlcommand(); cmd.commandtype = commandtype.text; cmd.connection = connection; cmd.commandtext = string.format("insert service_loadtest(id,servicecallcount) values ({0},'call_{1}')", i, i); cmd.executenonquery(); } });
Comments
Post a Comment