We have loads of XML files which we have to load, parse and save into multiple tables of a massive legacy database.
I have written code which does it using EF and it works perfectly with 1 thread. However I need to speed this process up so I need to process multiple XML files concurrently and therefore I use multiple threads. When I use multiple threads which have their own instance of DbContext, I constantly get deadlocks:
Transaction (Process ID 426) was deadlocked on lock resources with another process and has been chosen as the deadlock victim.
I searched the internet and found a solution which needed us to set the isolation level of the database to Snapshot however because it may have massive negative impacts on the legacy systems which use the same database it's not an option.
To avoid the deadlocks, I wrapped every dbContext.SaveChanges() in a lock() statement. This prevents the deadlocks but then the process becomes very slow again as if everything is done using one thread.
Can you please help me with this problem and guide me as to how I can update the DB using multiple instances of ObjectContext concurrently without causing deadlocks?
p.s. We can not use TransactionScope for some reasons!
p.p.s. We use SQL Server 2005
Aucun commentaire:
Enregistrer un commentaire