A transaction is
a group of several operations that must behave atomically, i.e., as
if they are a single, indivisible operation. With regards to
databases, transactions allow you to combine one or more database
actions into a single atomic unit. If you have an application that
needs to execute multiple SQL statements to fulfill one goal (say, an
inventory management system that needs to move items from an
INVENTORY table to a
table), you probably want to use JDBC’s transaction
services to accomplish the goal.
Working with a transaction involves
the following steps: start the transaction, perform its component
operations, and then either commit the transaction if all the
component operations succeed or roll it back if one of the operations
fails. The ability to roll back a transaction is the key feature.
This means that if any one SQL statement fails, the entire operation
fails, and it is as though none of the component operations took
place. Therefore it is impossible to end up with a situation where,
for example, the
INVENTORY table has been debited,
SHIPPING table has not been credited.
Another issue with transactions and databases concerns changes to the database becoming visible to the rest of the system. Transactions can operate at varying levels of isolation from the rest of the database. At the most isolated level, the results of all the component SQL statements become visible to the rest of the system only when the transaction is committed. ...