John Shedletsky
John Shedletsky

Reputation: 7168

Entity Framework 6 Fails Under High Load

I am stress testing my website. It uses Entity Framework 6.

I have 10 threads. This is what they are doing:

  1. Fetch some data from the web.
  2. Create new database context.
  3. Create/Update records in the database using Database.SqlQuery(sql).ToList() to read and Database.ExecuteSqlCommand(sql) to write (about 200 records/second)
  4. Close context

It crashes within 2 minutes with a database deadlock exception (consistently on a read!).

I have tried wrapping steps 2-4 in a Transaction, but this did not help.

I have read that as of EF6, ExecuteSqlCommand is wrapped in a transaction by default (https://msdn.microsoft.com/en-us/data/dn456843.aspx). How do I turn this behavior off?

I don't even understand why my transactions are deadlocked, they are read/writing independent rows.

Is there a database setting I can flip somewhere increase the size of my pending transaction queue?

Upvotes: 0

Views: 431

Answers (2)

Charles Burns
Charles Burns

Reputation: 10602

What transaction type is being used? .Net's TransactionScope defaults to SERIALIZABLE, at least in my applications which admittedly do not use EF. SERIALIZABLE transactions deadlock much more easily in my experience than other types such as ReadCommitted.

Upvotes: 0

Otávio Décio
Otávio Décio

Reputation: 74270

I doubt EF has anything to do with it. Even though you are reading/writing independent rows, locks can escalate and lock pages. If you are not careful with your database design, and how you perform the reads and writes (order is important) you can deadlock, with EF or any other access technique.

Upvotes: 1

Related Questions