Parallel computing using Sql connections RRS feed

  • General discussion

  • Hi,

    I have to parse the xml data and save data to database as a batch using Parallel approach. Connection to sql is not working. i.e. The code part of saving the data to db is not executing and returning.

    Any suggestion on this? Or What is the best approach for the above?


    Chandra Shekar Y

    Thursday, March 22, 2018 3:19 PM

All replies

  • The SqlConnection object is not safe for multithreading.

    One option is to open a separate SqlConnection for each of your threads, instead of sharing a single connection for all of them.

    Another one is to open a single connection with MultipleActiveResultSets=true (MARS) and add locks around each usage of the SqlConection (which would serialize your writes to the database, so it would detract a bit from the parallelism achieved by your multiple threads).

    Saturday, March 24, 2018 11:07 AM
  • You could use MS SQL Server Service Broker to parse the XML and process it saving it to a database. SB can execute C# too since it works with the CLI and the .NET Framework. 
    Saturday, March 24, 2018 6:35 PM