‫Is going on, guys.
‫My name is Hossein, and this video I would like to discuss to phase locking with a practical example,
‫guys, so Two-Faced locking is the idea of having a acquiring database locks and releasing them in phases,
‫basically to phase the first phases, acquire, acquire, acquire, acquire.
‫And the second phase is release, release, release, release.
‫And the trick here, once you release, you cannot acquire.
‫How about we translate all of this stuff to an actual good example, a practical example with a very,
‫very common problem calls that double booking.
‫So a double booking is when we're trying to build as, let's say, a cinema booking system.
‫Right.
‫And two users are trying to book the exact same seat at the exact same millisecond.
‫And when that happens, you will end up with a double booking problem if you don't have to face locking
‫or another implementation.
‫Right.
‫And what happens here is that two transactions will get executed at the same time.
‫They will both check the database and they will both check that the oh, the seat is available, let
‫me book it.
‫And they will all both commit to the same time and leading in problem with double booking it.
‫That's actually extremely undesirable.
‫Right, because you had two people paying for the same seat and they the last one went essentially in
‫this case, which is absolutely bad.
‫So what do we want to do as the first person the who pays will immediately reject the second person?
‫That's what we want to do.
‫How about we actually jump into it and show you that at the database level?
‫Right.
‫So, guys, I talked about the double booking problem from a Web application point of view.
‫If you want to see more and more hands on Web application framework thing, check out the video I made
‫here.
‫But I'm going to talk from a database level perspective here.
‫How about we actually go ahead and start a transaction here and we're going to go ahead and start a
‫transaction here?
‫All right.
‫So what I'm going to do here, this user is about to book seat number thirteen.
‫So what I'm going to do a will ask to check of the seat is available or not.
‫Right.
‫So let's start from seats where ID equal 13 and says, hey, it's it's available.
‫And the second transaction concurrently at the same time does the same exact thing from seats where
‫it equals thirteen.
‫Hey, awesome.
‫It's available.
‫So what this transaction does I update update seats, it is booked equal one because it's now booked
‫and the name is now Hassane where ID is equal thirteen also.
‫So and the same thing happens on this transaction.
‫So they update seats set is booked equal one name equal edman.
‫That's the user here where already equal thirteen also.
‫Right.
‫And you'll start seeing that this transaction is waiting.
‫This is a specific behavior to Posterous.
‫However, what we happen here is like the moment I commit here, this transaction also commits.
‫Right.
‫And when I do that, I'm going to to go ahead and and let's say select star from before we commit the
‫other transaction, I'm going to select Star from seats here where I'd equal thirteen.
‫Hey, looks good.
‫Send an email to the user that oh this seat is yours.
‫But at the same time, the other transaction now just committed the transaction and the moment you select
‫again now the transaction belong to someone completely different.
‫So that's the double booking problem, guys, that we're trying to avoid.
‫All right.
‫So one way to avoid this is to do two phase locking guys, right?
‫So the idea here is how about we start again and do the two phase locking here?
‫So I'm going to do here is going to do select begin transaction, begin transaction.
‫And let's assume I'm going to book.
‫Seat number 14, because it's not booked and here's where I'm going to I am going to book seat number
‫14 here and I'm going to also book seat number 14.
‫But this time I'm going to implement two phase locking.
‫So what is two phase locking?
‫I'm going to obtain an exclusive lock on the row that that I am about to update.
‫And here's how I do it.
‫I'm going to do select start from seats where I'd equal 14.
‫But what I add is for update.
‫And when I do for update, select for update that rule, that object that tuple will have an exclusive
‫lock.
‫So I am now in phase one.
‫I'm growing my locks.
‫Right.
‫And the moment I do that, nothing seems different here.
‫I stuck on my result, but there is a something change here.
‫We acquired an exclusive lock on the roll.
‫So that means if the second person tries to do the exact same thing where I'd equal 14, whatever they
‫do for update or not, look where they happen, they're stuck.
‫That's because they also tried to obtain an exclusive lock on the row itself.
‫And that operation is now stuck because you cannot obtain exclusive lock because that lock has already
‫been obtained by another transaction.
‫That's the rules of the exclusive lock.
‫And if you want to learn more about exclusive locks, go check out the video I made here.
‫All right.
‫So it says, OK, this transaction is happy now.
‫Says, OK, update seats set is booked equal one and Namik all housing where I'd equal fourteen and
‫then boom.
‫And once I'm done, I'm going to commit and look what will happen here the more I commit.
‫This transaction is immediately unlike what happened when I committed I entered phase two of the phasing
‫of the two phase lock, which is the release phase, the shrinking phase, the moment I committed or
‫I lower back, what happened is I released that lock that I obtained here.
‫So it's OK.
‫I changed it.
‫Now it's safe for other people to read it.
‫And that's what we want.
‫The moment that transaction now reads this says oh oh, this road, this road blog to Hosain, this
‫seat belong to her saying I cannot book it so the user will get an error on this case.
‫So your application will throw in it on that case.
‫All right, guys.
‫So that was like two phase locking in a very short video to explain that.
‫Again, guys, check out the other videos.
‫What I took, I put this within an actual application with a user interface and all that jazz, our
‫guys.
‫That's it for me.
‫A very quick video today to show you about two phase locking I'm going to see in the next one.
‫You guys stay out.