‫My name is Hussein and and this video, we're going to quickly show you a practical example of how Dedlock
‫happens in databases and this specific so quickly.
‫Guys, I did lock happens when two processes are or two clients are fighting for one or more resources
‫and either one is waiting for the other process to release the lock on a given resource.
‫And they will be in this infinite state where nobody can release that log.
‫So it's a very famous problem in computer science, obviously happens in dead bases.
‫And I'm going to show you how this happens.
‫And most databases actually catch these deadlocks and fail the transactions when they happen.
‫How about we jump into it?
‫So before I jump into this video, guys, I want to add I want you to be familiar with locks, mainly
‫shared lock, exclusive locks.
‫And so if you want to learn more, check out the videos I've made about these topics here.
‫Go and take a look at them and what that say.
‫Let's just jump into this video, guys, and show you how.
‫So I have here two clients terminal connecting to one Posterous in instances running on Dalkia.
‫I showed many times how to do that.
‫And this channel.
‫Check it out.
‫So in the first client, I'm going to stop begin a transaction and in the second client, I'm going
‫to also begin a transaction.
‫Right.
‫So both of them begin the transaction.
‫There is a table here called Test and it has a primary key and has one field and a primary key field.
‫So you guys know if it's a primary, you can only have one value at a time.
‫Right.
‫A unique value.
‫So I'm going to insert into this table, insert into test values, let's call it, I don't know, twenty.
‫Right.
‫And that's successful because 20 doesn't exist.
‫Right.
‫And I didn't commit my transaction yet.
‫And then the other transaction, I am going to insert test values.
‫Twenty one.
‫OK, and that also succeeds because 21 doesn't exist.
‫However, now in this transaction, I am going to attempt to insert 20.
‫So 20 in this case, guys, despite it not being committed, there is an exclusive WLOX obtained on
‫that value.
‫Nobody can actually change that or insert that value again.
‫However, we are not sure yet because this transaction did not commit yet.
‫Right.
‫So this might roll back or commit.
‫And if that happened, this transaction will unblock and continue.
‫However, let's let's spice things up here in this transaction.
‫I am going to actually insert the value of, you guessed it, twenty one.
‫And now this guy is also going to be blocked.
‫However, both of them will be in a state where they are waiting for each other and a deadlock will
‫be detected.
‫The last one entering that deadlock will be the one to fail.
‫So in this case, what happened here, Posterous immediately deducted that took us like I don't talk.
‫I think I second for that deadlock to be detected and ones that did not get deducted, the transaction
‫water was rolled back and that transaction actually won because it entered the deadlock at first.
‫So essentially, that's how a deadlock is handled by the database.
‫It's very complex thing.
‫Most databases handle these that looks very smartly.
‫And they and they rolled back the transaction that essentially entered the deadlock last.
‫However, let's just explain a more optimistic scenario, guys, huh?
‫So I'm going to roll back this.
‫I'm going to also roll back this clear here, clear here and begin a transaction and also here begin
‫a transaction where things are there's no deadlock.
‫What do we do?
‫Insert into test values?
‫Twenty.
‫And here I'm going to insert into test values.
‫30 and here, I'm going to also insert into test values 20.
‫And if I do that, I'm going to enter into this lock mode, and that's absolutely fine, because until
‫this transaction finishes, we are locked, right.
‫So what happens if I do roll back here, guys?
‫And when I do roll back immediately, that transaction actually succeeds because that 20 is now available
‫to be inserted.
‫Right.
‫So if, however, we flipped this.
‫Let's flip it, and I do.
‫Begins transaction here, begin transaction here, insert into 20 and here insert into, let's say 30,
‫then I'm going to insert into 20 here, which lock sales.
‫Right.
‫But here in this case, I'm going to commit what what happened to this transaction exactly is going
‫to fail with the duplicate key, because now that this transaction actually gone, this as this transaction
‫in this case, the 20 is not available and we're going to get that error right and fail.
‫Right.
‫Our guys.
‫That was a quick video describing deadlocks and locks and.
‫Very quickly, Olby, enjoy this video and I'm going to see you in the next one.
‫You guys say on.