Transactions in databases
Transaction is an impact on a database that transfers it from one consistent state to another and is expressed in changing the data stored in the database.
Main properties of the transaction
Atomicity ensures that no transaction is partially committed in the system. Either all of its suboperations will be executed, or none of them will be executed.
Consistency. A transaction that reaches its normal completion and thus commits its results maintains database consistency.
Isolation. During the execution of a transaction, concurrent transactions should not affect its outcome.
Durability. Regardless of problems at the lower levels (for example, a system outage or hardware failures), changes made by a successfully completed transaction should remain saved after the system is returned to work.
Levels of transaction isolation
In order to increase the isolation of transactions and, accordingly, the reliability of working with data:
- Read uncommitted (dirty read) - read uncommitted changes both from your own transaction and from parallel transactions. There is no guarantee that data changed by other transactions will not be changed at any time as a result of their rollback, so this reading is a potential source of errors. Lost changes are not possible, non-repeatable reads and phantoms are possible.
- Read committed - read all changes in your transaction and committed changes in parallel transactions. Lost changes and dirty reads are not allowed, nonrepeatable reads and phantoms are possible.
- Repeatable read (snapshot) - reading all changes in your transaction, any changes made by parallel transactions after the start of their own are not available. Lost changes, dirty and unrepeatable reads are impossible, phantoms are possible.
- Serializable - the result of parallel execution of a serializable transaction with other transactions must be logically equivalent to the result of any sequential execution of them. Synchronization problems do not arise.
What problems can arise with concurrent access using transactions?
With parallel execution of transactions, the following problems are possible:
- Lost update - when one data block is simultaneously changed by different transactions, one of the changes is lost;
- "Dirty" read - reading data, added or modified by a transaction, which subsequently will not be confirmed (rolled back);
- Non-repeatable read - after repeated reading within one transaction, the previously read data is changed;
- Phantom reads - one transaction during its execution several times selects many records according to the same criteria. Another transaction, in the intervals between these selections, adds or deletes records, or modifies the columns of some of the records used in the selection criteria of the first transaction, and ends successfully. As a result, it turns out that the same samples in the first transaction give different sets of records.
Read also:
- Difference between clustered and non-clustered indexes
- Types of indexes in database
- Transaction isolation levels in database
Comments
Post a Comment