Avoid Deadlocks

  Рет қаралды 2,598

Jeff Zuerlein

Jeff Zuerlein

Күн бұрын

I’ve been trying to figure out the cause of a SQL deadlock problem, and it dawned on me that preventing deadlocks shouldn’t be that hard. You just have to save data in the same order every time, and the easiest way to do that is to have a repository with one save method.
I'll show you what a deadlock is, and how to prevent them.
#sql #csharp #database
Blog:
betterwithcode.com/
LinkedIn:
/ jeff-zuerlein-2aa67b7
To find deadlocks in your database...
SP_BlitzLock:
github.com/BrentOzarULTD/SQL-...
00:00 Introduction
00:40 Example Aggregate
01:08 What is a deadlock?
02:54 Fixing the repository.
03:51 Tools to find problems.

Пікірлер: 22
@o_glethorpe
@o_glethorpe 17 күн бұрын
Nice to see someone that is actually teaching something.
@JeffZuerlein
@JeffZuerlein 17 күн бұрын
Thank you!
@persiansayed
@persiansayed 16 күн бұрын
Great video! Please keep on making these informative videos. 😊
@JeffZuerlein
@JeffZuerlein 16 күн бұрын
More to come!
@MartinoBordin
@MartinoBordin 28 күн бұрын
The aggregate\aggregate root concept in DDD will naturally lead to this approach. Great video
@JeffZuerlein
@JeffZuerlein 28 күн бұрын
Thank you! I'm curious, do you generally use EF Core, Dapper, NHibernate, or SQL Client for data access?
@MartinoBordin
@MartinoBordin 28 күн бұрын
@@JeffZuerlein In my latest project I'm using EF Core. I believe it has everything nowadays to do proper data access
@AndrewBoudreau
@AndrewBoudreau 20 күн бұрын
​​@@MartinoBordinif you are using ef and have aggregates why not just use dbcontext.savechanges? I always felt like save on repository is making ef somuch less powerful. Thoughts?
@samerelsahih
@samerelsahih 27 күн бұрын
Great video thank you ! If you're using EF, a good way would be in my opinion to use the UOW (Unit of work) pattern. It's easy to use, very straightforward and saves you from similar problems.
@JeffZuerlein
@JeffZuerlein 26 күн бұрын
Good point! If you're using SQL Client or Dapper, the UOW would probably have a SQL transaction. But with EF Core, I think that logic for order of updates would reside somewhere in the DB Context. Maybe I should read through that source code and find out how it chooses to order update statements. Thank you for the feedback!
@MrSupasonik
@MrSupasonik 26 күн бұрын
​@@JeffZuerleinThat's why I prefer Dapper; one has to invest so much time to understand how EF works under the hood, learning its abstractions and constraints (sure, you can skip this, but it will cause a mess in any serious cases). In contrast, with Dapper, it's much quicker and simpler, and you fully understand what will be sent to the SQL Server and how it will be processed there. Anyway, important (and concise, that's greatly appreciated) explanation. Keep up the good work!
@JeffZuerlein
@JeffZuerlein 26 күн бұрын
I really love Dapper. It gives you all the control you want, but I really want the change tracking that comes with EF. I can't tell you how many unneeded update statements get executed in Dapper because you can't tell if the domain entity mutated.
@frankbanini8884
@frankbanini8884 17 күн бұрын
Great video. I just subscribed to your channel.
@JeffZuerlein
@JeffZuerlein 17 күн бұрын
Awesome! Thank you!
@chandez
@chandez 16 күн бұрын
Just one point. We don't always want to persist the entity and all its aggregates. For example, if we have a section for address changes. I've seen applications that use this technique to the extreme, persisting many aggregates unnecessarily.
@JeffZuerlein
@JeffZuerlein 16 күн бұрын
You bring up a point that bugs me with repositories that don’t have have access to change tracking. I don’t like to save data to the db when it hasn’t changed. It feels wasteful. EF Core ‘s best feature, to me, is change tracking. I have the tendency to look at an aggregate as a set of data that depends on each other. They have business rules that tie each other together. So if you had an address that could be changed and didn’t affect another entity, then I do think the address would be its own aggregate, with a repository of its own. I think it depends on the business rules.
@chandez
@chandez 16 күн бұрын
@@JeffZuerlein Exactly always depends on the context. 😃
@aarondcmedia9585
@aarondcmedia9585 14 күн бұрын
I never understand why people have videos with some part of the content at the beginning, with no context or discernible meaning. Was there a memo I missed? It happens nearly everywhere, including TV and makes no sense at all.
@JeffZuerlein
@JeffZuerlein 14 күн бұрын
Touché. Attention spans these days can be very short. Most people only watch the first 10 seconds of a video, so it’s hard to make good intros.
@aarondcmedia9585
@aarondcmedia9585 14 күн бұрын
@@JeffZuerlein yeah I am clearly too old hahaa!
@ibrahimhussain3248
@ibrahimhussain3248 22 күн бұрын
That's not possible with the FKs shown. Cannot insert an address without a person. The FK constraint will fail
@JeffZuerlein
@JeffZuerlein 22 күн бұрын
Good callout...I should have specified update statements.
T-SQL Skills: Loading Millions Of Rows Of Test Data In Seconds
11:19
"Stop Using Async Await in .NET to Save Threads" | Code Cop #018
14:05
FOUND MONEY 😱 #shorts
00:31
dednahype
Рет қаралды 7 МЛН
C++ Full Tutorial in One Shot Video (2024) | C++ Full Course
10:35:48
CoDing SeeKho
Рет қаралды 36 М.
Deep Dive on Locks & Deadlocks for Application Developers
27:50
Zoran Horvat
Рет қаралды 5 М.
5 Rules For DTOs
17:56
Ardalis
Рет қаралды 35 М.
Span of T vs. Memory of T
9:44
Jeff Zuerlein
Рет қаралды 6 М.
Difference between blocking and deadlocking
6:52
kudvenkat
Рет қаралды 138 М.
This is why understanding database concurrency control is important
9:05
7 Database Paradigms
9:53
Fireship
Рет қаралды 1,5 МЛН
ACID Properties in Databases With Examples
4:57
ByteByteGo
Рет қаралды 77 М.
Manage User Rights By Decorating Their Claims Identity
9:35
Jeff Zuerlein
Рет қаралды 2,8 М.
Save Work Efficiently on Your Computer 18/05/2024
0:51
UNIQUE PHOTO EDITING
Рет қаралды 308 М.
Теперь это его телефон
0:21
Хорошие Новости
Рет қаралды 1,6 МЛН
Xiaomi Note 13 Pro по безумной цене в России
0:43
Простые Технологии
Рет қаралды 1,7 МЛН
Обманет ли МЕНЯ компьютерный мастер?
20:48
Харчевников
Рет қаралды 168 М.