A business system transaction is the execution of a unit-of-work that accesses one or more shared resources, usually
databases. A unit-of-work is a set of activities
that relate to each other and must be completed together. The reservation
process is a unit-of-work made up of several activities: recording a
reservation, debiting a credit card, and generating a ticket.
Characteristics of a Transaction
There are four characteristics of a transaction that must be
met for a system to be considered safe.
Atomic: An atomic
transaction must execute completely or not at all. This means that every task
within a unit-of-work must execute without error. If any of the tasks fail, the
entire unit-of-work or transaction is aborted, meaning that any changes to the
data are undone. If all the tasks execute successfully, the transaction is
committed, which means that the changes to the data are made permanent or
durable.
Consistent: Consistency
refers to the integrity of the underlying data store. It must be enforced by
both the transactional system and the application developer. The transactional
system fulfills this obligation by ensuring that a transaction is atomic,
isolated, and durable. The application developer must ensure that the database
has appropriate constraints (primary keys, referential integrity, and so forth)
and that the unit-of-workthe business logicdoesn't result in inconsistent data
(i.e., data that is not in harmony with the real world it represents). In an
account transfer, for example, the debit to one account must equal the credit
to another account.
Isolated: Isolation
means that a transaction must be allowed to execute without interference from
other processes or transactions. In other words, the data that a transaction
accesses cannot be affected by any other part of the system until the
transaction or unit-of-work is completed.
Durable: Durability
means that all the data changes made during the course of a transaction must be
written to some type of physical storage before the transaction is successfully
completed. This ensures that the changes are not lost if the system crashes.
Transaction Isolation
There are three problems for transaction isolation .
Dirty read problem: A dirty read occurs when a transaction reads uncommitted changes
made by a previous transaction. If the first transaction is rolled back, the
data read by the second transaction becomes invalid because the rollback undoes
the changes. The second transaction will not be aware that the data it has read
has become invalid.
Non-Repeatable Read problem: Unrepeatable read occurs when a component
reads some data from a database, but upon rereading the data, the data has been
changed. This can arise when another concurrently executing transaction modifies
the data being read.
Suppose you read a data set x from the database. Another application overrides data set x with
new values. Now you reread the data set
x from the database. You will see that the values have magically changed.
We need to address this problem if you want to read each of
the rows that we are modifying, and then be able to update each row, knowing
that none of the rows are being modified by other concurrent transaction.
Phantom read problem: A
phantom is a new set of data that magically appears in a database between two
read operations.
Suppose our application queries the database using some
criteria and retrieves a dataset. Another application inserts new data that
satisfy our query. When we perform the query again then new set of data will
magically appeared.
Database Locks
Read locks: Read locks prevent other transactions from
changing data, read during a transaction until the transaction ends, thus
preventing nonrepeatable reads. Other transactions can read the data but not
write to it. The current transaction is also prohibited from making changes.
Whether a read lock locks only the records read, a block of records, or a whole
table depends on the database being used.
Write locks: Write locks are used for updates. A write
lock prevents other transactions from changing the data until the current
transaction is complete but allows dirty reads by other transactions and by the
current transaction itself. In other words, the transaction can read its own
uncommitted changes.
Exclusive write locks: Exclusive
write locks are used for updates. An exclusive write lock prevents other
transactions from reading or changing the data until the current transaction is
complete. It also prevents dirty reads by other transactions. Some databases do
not allow transactions to read their own data while it is exclusively locked.
Snapshots: A snapshot is a frozen view of the
data that is taken when a transaction begins. Some databases get around locking
by providing every transaction with its own snapshot. Snapshots can prevent
dirty reads, nonrepeatable reads, and phantom reads. They can be problematic
because the data is not real-time data; it is old the instant the snapshot is
taken.
Transaction
Isolation Levels
Transaction
isolation is defined in terms of the isolation conditions (dirty reads,
repeatable reads, and phantom reads). Isolation levels are commonly used in
database systems to describe how locking is applied to data within a
transaction.
Read Uncommitted: The transaction can read uncommitted
data (i.e., data changed by a different transaction that is still in progress).
Dirty reads, nonrepeatable reads, and phantom reads can occur. Bean methods
with this isolation level can read uncommitted changes.
Read
Committed : The transaction cannot read uncommitted data; data that
is being changed by a different transaction cannot be read. Dirty reads are
prevented; nonrepeatable reads and phantom reads can occur. Bean methods with
this isolation level cannot read uncommitted data.
Repeatable
Read : The transaction cannot change data that is being read by a
different transaction. Dirty reads and nonrepeatable reads are prevented;
phantom reads can occur. Bean methods with this isolation level have the same
restrictions as those in the Read Committed level and can execute only
repeatable reads.
Serializable:
The transaction has exclusive read and update privileges; different
transactions can neither read nor write to the same data. Dirty reads,
nonrepeatable reads, and phantom reads are prevented. This isolation level is
the most restrictive.
Isolation
Level
|
Transactions
|
Dirty
Reads
|
Non-Repeatable
Reads
|
Phantom
Reads
|
TRANSACTION_NONE
|
Not supported
|
Not applicable
|
Not applicable
|
Not applicable
|
READ_COMMITTED
|
Supported
|
Prevented
|
Allowed
|
Allowed
|
READ_UNCOMMITTED
|
Supported
|
Allowed
|
Allowed
|
Allowed
|
REPEATABLE_READ
|
Supported
|
Prevented
|
Prevented
|
Allowed
|
SERIALIZABLE
|
Supported
|
Prevented
|
Prevented
|
Prevented
|
Usually, you do not need to
do anything about the transaction isolation level; you can just use the default
one for your DBMS. The default transaction isolation level depends on your
DBMS. For example, for Java DB, it is TRANSACTION_READ_COMMITTED. JDBC
allows you to find out what transaction isolation level your DBMS is set to (using
the Connection method getTransactionIsolation) and also
allows you to set it to another level (using the Connection methodsetTransactionIsolation).
No comments:
Post a Comment