Java Reference
In-Depth Information
THE ACID TESTS
Atomicity : A transaction should be all or nothing. If it fails to complete, the database will be left as if
none of the operations had ever been performed—this is known as a rollback .
Consistency : A transaction should be incapable of breaking any rules defined for the database. For
example, foreign keys must be obeyed. If for some reason this is impossible, the transaction will be
rolled back.
Isolation : The effects of the transaction will be completely invisible to all other transactions until it has
completed successfully. This guarantees that the transaction will always see the data in a sensible
state. For example, an update to a user's address should only contain a correct address (i.e., it will
never have the house name for one location but the ZIP code for another); without this rule, a transac-
tion could easily see when another transaction had updated the first part but had not yet completed.
Durability : The data should be retained intact. If the system fails for any reason, it should always be
possible to retrieve the database up to the moment of the failure.
The isolation levels permitted by JDBC and Hibernate are listed in Table 8-2.
Table 8-2. JDBC Isolation Levels
Level
Name
Transactional Behavior
0
None
Anything is permitted; the database or driver does not support
transactions.
1
Read Uncommitted
Dirty, nonrepeatable, and phantom reads are permitted.
2
Read Committed
Nonrepeatable reads and phantom reads are permitted.
4
Repeatable Read
Phantom reads are permitted.
8
Serializable
The rule must be obeyed absolutely.
A dirty read may see the in-progress changes of an uncommitted transaction. As with the
isolation example discussed in the preceding sidebar, it could see the wrong ZIP code for an
address.
A nonrepeatable read sees different data for the same query. For example, it might deter-
mine a specific user's ZIP code at the beginning of the transaction and again at the end, and
get a different answer both times without making any updates.
A phantom read sees different numbers of rows for the same query. For example, it might
see 100 users in the database at the beginning of the query and 105 at the end without making
any updates.
The HSQLDB database that we are using in this topic only supports the first level of isola-
tion here: Read Uncommitted. While this means that deadlocks cannot occur (see the “Dead-
locks” section later in the chapter), the three undesirable behaviors of dirty, nonrepeatable,
and phantom reads are permitted.
Search WWH ::




Custom Search