Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

cdn.s3techtraining.com
from cdn.s3techtraining.com More from this publisher
17.06.2013 Views

Chapter 14: Transactions and Locks Behind the scenes, something like this is happening: UPDATE checking SET Balance = Balance - 1000 WHERE Account = ‘Sally’ UPDATE savings SET Balance = Balance + 1000 WHERE Account = ‘Sally’ This is a hyper-simplification of what’s going on, but it captures the main thrust of things: You need to issue two different statements — one for each account. Now what if the first statement executes and the second one doesn’t? Sally would be out a thousand dollars! That might, for a short time, seem OK from your perspective (heck, you just made a thousand bucks!), but not for long. By that afternoon you’d have a steady stream of customers leaving your bank — it’s hard to stay in the bank business with no depositors. What you need is a way to be certain that if the first statement executes, the second statement executes. There really isn’t a way that we can be certain of that — all sorts of things can go wrong, from hardware failures to simple things, such as violations of data integrity rules. Fortunately, however, there is a way to do something that serves the same overall purpose — we can essentially forget that the first statement ever happened. We can enforce at least the notion that if one thing didn’t happen, then nothing did — at least within the scope of our transaction. In order to capture this notion of a transaction, however, we need to be able to define very definite boundaries. A transaction has to have very definitive beginning and end points. Actually, every SELECT, INSERT, UPDATE, and DELETE statement you issue in SQL Server is part of an implicit transaction. Even if you issue only one statement, that one statement is considered to be a transaction — everything about the statement will be executed, or none of it will. Indeed, by default, that is the length of a transaction — one statement. But what if we need to have more than one statement be all or nothing, as in our preceding bank example? In such a case, we need a way of marking the beginning and end of a transaction, as well as the success or failure of that transaction. To that end, there are several T-SQL statements that we can use to mark these points in a transaction. We can: ❑ BEGIN a transaction: Set the starting point. ❑ COMMIT a transaction: Make the transaction a permanent, irreversible part of the database. ❑ ROLLBACK a transaction: Say essentially that we want to forget that it ever happened. ❑ SAVE a transaction: Establish a specific marker to allow us to do only a partial rollback. Let’s look over all of these individually before we put them together into our first transaction. BEGIN TRAN 428 The beginning of the transaction is probably one of the easiest concepts to understand in the transaction process. Its sole purpose in life is to denote the point that is the beginning of a unit. If, for some reason,

we are unable or do not want to commit the transaction, this is the point to which all database activity will be rolled back. That is, everything beyond this point that is not eventually committed will effectively be forgotten, as far as the database is concerned. The syntax is: BEGIN TRAN[SACTION] [|] [ WITH MARK [ ]] I won’t dwell on the WITH MARK option here, as it is a topic having to do with very advanced point in time transaction work, which tends to be more administrator oriented and is well outside of the scope of this book. COMMIT TRAN The committing of a transaction is the end of a completed transaction. At the point that you issue the COMMIT TRAN, the transaction is considered durable. That is, the effect of the transaction is now permanent and will last even if you have a system failure (as long as you have a backup or the database files haven’t been physically destroyed). The only way to undo whatever the transaction accomplished is to issue a new transaction that, functionally speaking, is a reverse of your first transaction. The syntax for a COMMIT looks pretty similar to a BEGIN: COMMIT TRAN[SACTION] [|] ROLLBACK TRAN Whenever I think of a ROLLBACK, I think of the old movie The Princess Bride. If you’ve ever seen the film (if you haven’t, I highly recommend it), you’ll know that the character Vizzini (considered a genius in the film) always says, “If anything goes wrong, go back to the beginning.” That is some mighty good advice. A ROLLBACK does just what Vizzini suggests — it goes back to the beginning. In this case, it’s your transaction that goes back to the beginning. Anything that happened since the associated BEGIN statement is effectively forgotten. The only exception to going back to the beginning is through the use of what are called save points, which we’ll describe shortly. The syntax for a ROLLBACK again looks pretty much the same as a BEGIN or COMMIT, with the exception of allowance for a save point. ROLLBACK TRAN[SACTION] [|| |] SAVE TRAN Chapter 14: Transactions and Locks To save a transaction is essentially to create something of a bookmark. You establish a name for your bookmark (you can have more than one). After this bookmark is established, you can reference it in a rollback. What’s nice about this is that you can roll back to the exact spot in the code that you want to just by naming a save point to which you want to roll back. 429

Chapter 14: Transactions and Locks<br />

Behind the scenes, something like this is happening:<br />

UPDATE checking<br />

SET Balance = Balance - 1000<br />

WHERE Account = ‘Sally’<br />

UPDATE savings<br />

SET Balance = Balance + 1000<br />

WHERE Account = ‘Sally’<br />

This is a hyper-simplification of what’s going on, but it captures the main thrust of things: You need to<br />

issue two different statements — one for each account.<br />

Now what if the first statement executes and the second one doesn’t? Sally would be out a thousand dollars!<br />

That might, for a short time, seem OK from your perspective (heck, you just made a thousand<br />

bucks!), but not for long. By that afternoon you’d have a steady stream of customers leaving your<br />

bank — it’s hard to stay in the bank business with no depositors.<br />

What you need is a way to be certain that if the first statement executes, the second statement executes.<br />

There really isn’t a way that we can be certain of that — all sorts of things can go wrong, from hardware<br />

failures to simple things, such as violations of data integrity rules. Fortunately, however, there is a way<br />

to do something that serves the same overall purpose — we can essentially forget that the first statement<br />

ever happened. We can enforce at least the notion that if one thing didn’t happen, then nothing did — at<br />

least within the scope of our transaction.<br />

In order to capture this notion of a transaction, however, we need to be able to define very definite<br />

boundaries. A transaction has to have very definitive beginning and end points. Actually, every SELECT,<br />

INSERT, UPDATE, and DELETE statement you issue in <strong>SQL</strong> <strong>Server</strong> is part of an implicit transaction. Even<br />

if you issue only one statement, that one statement is considered to be a transaction — everything about<br />

the statement will be executed, or none of it will. Indeed, by default, that is the length of a transaction —<br />

one statement.<br />

But what if we need to have more than one statement be all or nothing, as in our preceding bank example?<br />

In such a case, we need a way of marking the beginning and end of a transaction, as well as the success<br />

or failure of that transaction. To that end, there are several T-<strong>SQL</strong> statements that we can use to mark<br />

these points in a transaction. We can:<br />

❑ BEGIN a transaction: Set the starting point.<br />

❑ COMMIT a transaction: Make the transaction a permanent, irreversible part of the database.<br />

❑ ROLLBACK a transaction: Say essentially that we want to forget that it ever happened.<br />

❑ SAVE a transaction: Establish a specific marker to allow us to do only a partial rollback.<br />

Let’s look over all of these individually before we put them together into our first transaction.<br />

BEGIN TRAN<br />

428<br />

The beginning of the transaction is probably one of the easiest concepts to understand in the transaction<br />

process. Its sole purpose in life is to denote the point that is the beginning of a unit. If, for some reason,

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!