CHAPTER 21 ADO.NET PART I: THE CONNECTED LAYER
1001
1992
BMW
Saab
Tan
Pink
Daisy
Pinkey
Please enter your command: P
Enter ID of Car to look up: 904
Petname of 904 is Hank.
Please enter your command: Q
Press any key to continue . . .
Source Code You can find the AutoLotCUIClient application under the Chapter 21 subdirectory.
Understanding Database Transactions
Let’s wrap up this examination of the connected layer of ADO.NET by taking a look at the concept of a
database transaction. Simply put, a transaction is a set of database operations that must either all work
or all fail as a collective unit. As you might imagine, transactions are quite important to ensure that table
data is safe, valid, and consistent.
Transactions are important when a database operation involves interacting with multiple tables or
multiple stored procedures (or a combination of database atoms). The classic transaction example
involves the process of transferring monetary funds between two bank accounts. For example, if you
were to transfer $500.00 from your savings account into your checking account, the following steps
should occur in a transactional manner:
•
The bank should remove $500.00 from your savings account.
•
The bank should add $500.00 to your checking account.
It would be an extremely bad thing if the money were removed from the savings account, but not
transferred to the checking account (due to some error on the bank’s part) because then you would be
out $500.00! However, if these steps are wrapped up into a database transaction, the DBMS ensures that
all related steps occur as a single unit. If any part of the transaction fails, the entire operation is rolled
back to the original state. On the other hand, if all steps succeed, the transaction is committed.
Note You might be familiar with the acronym ACID from looking at transactional literature. This represents the
four key properties of a prim-and-proper transaction: Atomic (all or nothing); Consistent (data remains stable
throughout the transaction); Isolated (transactions do not step on each other’s feet); and Durable (transactions are
saved and logged).
853