Isolation

The third reason listed for using transactions was isolation.
Consider an application suite in which multiple threads of control
(multiple processes or threads in one or more processes) are changing
the values associated with a key in one or more databases. Specifically,
they are taking the current value, incrementing it, and then storing it
back into the database.

Such an application requires isolation. Because we want to change a value
in the database, we must make sure that after we read it, no other thread
of control modifies it. For example, assume that both thread #1 and
thread #2 are doing similar operations in the database, where thread #1
is incrementing records by 3, and thread #2 is incrementing records by
5. We want to increment the record by a total of 8. If the operations
interleave in the right (well, wrong) order, that is not what will
happen:

As you can see, instead of incrementing the record by a total of 8,
we've incremented it only by 3 because thread #1 overwrote thread #2's
change. By wrapping the operations in transactions, we ensure that this
cannot happen. In a transaction, when the first thread reads the
record, locks are acquired that will not be released until the
transaction finishes, guaranteeing that all writers
will block, waiting for the first thread's transaction to complete (or
to be aborted).

Here is an example function that does transaction-protected increments
on database records to ensure isolation:

The DB_RMW flag in the DB->get() call specifies a write lock
should be acquired on the key/data pair, instead of the more obvious read
lock. We do this because the application expects to write the key/data
pair in a subsequent operation, and the transaction is much more likely to
deadlock if we first obtain a read lock and subsequently a write lock, than
if we obtain the write lock initially.