Skip to main content

290 posts tagged with ".NET"

.NET Framework and Core for Application Development

View All Tags

Fixed: "Check constraint 'CK_products_QuantityOnHand' is violated" with ExecuteUpdateAsync in EF Core and MySQL

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Under load, sales failed with MySqlException: Check constraint 'CK_products_QuantityOnHand' is violated. and an HTTP 500. The stock never went below zero, so the constraint had worked. The problem is that a buyer who should have heard "out of stock" heard "server error".

Fixed: "Connection must be valid and open to rollback transaction" with MySql.Data

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

My log said Connection must be valid and open to rollback transaction, and that wasn't what had gone wrong. Something had failed after the commit, my catch tried to roll back a transaction that was already finished, and the new exception replaced the real one. The fix also removes code: you rarely need RollbackAsync at all.

Fixed: "The connection is already in a transaction and cannot participate in another transaction" in EF Core

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

Calling one helper method from inside a transaction failed with The connection is already in a transaction and cannot participate in another transaction. The helper had its own BeginTransactionAsync, which made sense when it was written, and it stopped making sense once something else called it inside a transaction.

Fixed: Two Requests Buy the Last Item in EF Core (Overselling)

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

One unit left, five simultaneous orders, and all five answered 201 Created. The order code was in a transaction, and it checked the stock first. It looked correct, and the first time I ran it, it even behaved. The check and the write were two separate steps, and every order did the check before any of them had written.

Fixed: EF Core Retry Places the Same Order Twice After a Lost COMMIT

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

One request answered 201, and the database had two orders, with the stock taken twice. MySQL had committed the first attempt, the connection dropped before the reply arrived, and EF Core's retry, knowing only that the commit had failed, did it all again. Retrying is only safe when each attempt can tell that an earlier one already worked.