4.10 Save Points
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
Already have an account? Sign In »
7 hours 36 minutes
I welcome back to Model four sequel programming to we're currently in less than three concurrency and transactions were in sub lesson 3.4 save points and to go over save points, I decided to go ahead and write up a mock procedure that we can apply to the employees database.
We're gonna walk through that in a moment.
First, we're gonna describe what save points are safe. Points are away for transactions to roll back on lee
some of the commits that they've made, or some of the inserts and updates that they've made.
So you would use this typically in large procedures where you have a lot of transactions going on and it doesn't make sense to roll them all back.
So with that said, let's walk through this procedure. It'll be available for download as well, so that you can apply it against your own database and we'll talk through it.
So we create the procedure we call established employees.
It accepts four parameters. The department I d. The from date, the title I D and the title,
and I put under scores at the end of the names so that they don't clash with the names and the sequel queries.
They come from the table.
This is new. We labeled our procedure
by putting a name with a colon right before they began. And ah, I named it any name you want just a show that you don't have to name it. Anything specific can literally name it Anything you want. We end up using that down here. When we leave, Um, when you use the leave command, you have to provide the label that you're leaving,
which is why we defined it up here.
So next we declare three variables, we declare the amp I d.
We declare the sequel air and we said, It's the fault valued of false.
We declare a handler
for sequel airs. And we said that the true if they occur
online 15 We then set the impiety to the next employee number.
Now we have to do that because the auto increment is not on the in this table. So if we open up the employees properties, Tab and D B will see that auto increment is not checked, which means we have to provide it,
which means we have to logically decide what the next number is. And the way we do that
as we select the max number that exists
and then we add one to it, and that will give us our next number.
Next we start the transaction,
we create our save point. This is the new thing in this procedure that we haven't seen before. So we're creating a save point.
We're inserting into the employees table the data that we have. I've put some proxy data in there
now. It could be a policy in the front end to generate an employee, and then the user would just changes these values to what they are and then call a day employee procedure. But, you know, that varies a lot from company to company application application.
So that's what we've done right here in this procedure.
If Sinclair is true,
then we roll back to save point before employees, and then we leave the procedure.
So if we leave the procedure, none of this below happens because we've left.
However, if there's no sequel air, we continue on.
So at the next point we create another save point,
and that is we're gonna call that safe point before department.
We're gonna insert into the employees department, underscore EMP table the values that we had ah received from the select statement along with the values we got from the parameters that were past that
After that runs, we check again. If the secret lair is true.
If it is, we then rolled back to the save point before department
when we set the sequel heir to False so that we can go ahead and reset it for the final statement.
Now, if the department insert fells, it might be okay. That's its own table with its own set of data.
Um, that doesn't affect the title, so we can still insert the title.
So if we haven't employees end up without a department that's different.
If we could still save, save it and have them end up with a title that's better than them ending up with a title and no department that both know department and no title.
So that's why we're doing this same point and this rollback that specific between the inserts.
So we create a save point before title, we insert into titles and again provide the data that we had
received from parameters above and the employees idea that we established above by a select statement. I almost forgot something.
We're gonna need this
date in there as well.
And finally, if there's a sequel, Air True. Then we rolled back to save point before Title
Re and F,
and at this point, we're at the end of the procedure and
and we exit.
So we used. We have three safe points in here. They each check each transaction
and roll back if necessary.
Now, this isn't the best example just because it's were essentially rolling back one transaction per save point
and a lot of practical examples, you'd have probably three or four transactions per save point that you would roll back in a larger procedure. But
and this procedure, you can see the how we're using safe points, how we're rolling back. And it's something that keeping your tool belt
down the road should you need it.
So that completes this Lessing on safe points.
I'll see you in the next lesson. Thank you