Transactions
So far, we have failed to mention one important feature of modern
relational database systems:
transactions. Most service-oriented web sites need to do more than
run SELECT
statements and insert single pieces of
data. Let’s look at an online banking application. To perform a
transfer of $50,000 between accounts, your program needs to perform
an operation that consists of two separate but related actions:
credit one account and debit another. Now, imagine that for some
reason or another, the SQL statement for the credit succeeds but the
one for the debit fails. One account holder is $50,000 richer, but
the other account has not been debited to match.
SQL failure is not the only potential problem. If another user checks the account balance in between the credit and the debit, he will see the original balance. The database is shown in an invalid state (more money is represented than actually exists). Granted, this kind of thing is unlikely to occur often, but in a universe of infinite possibilities, it will almost certainly happen sometime. This kind of problem is similar to the synchronization issues we discussed back in Chapter 3. This time, instead of concerning ourselves with the validity of data stored in a servlet, we are concerned with the validity of an underlying database. Simple synchronization is not enough to solve this problem: multiple servlets may be accessing the same database. For systems like banking software, chances are good that the database is being ...
Get Java Servlet Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.