ISOLATION LEVEL
It ensures the consistency of data in simultaneous operations. And is a structure that isolates. Protects the integrity of the data.
DIRTY READ
At time T0, Transaction A attempts to update the ‘price’ column of a product to $1999. At time T1, Transaction B reads the data for a product with ID 1001. By time T2, Transaction A encounters an exception, leading to the rollback of the entire update process. However, Transaction B has already read the updated data, resulting in a situation where the client observes incorrect information. This phenomenon is referred to as a ‘Dirty Read.
Accessing an updated value that has not been committed is considered a dirty read because it is possible for that value to be rolled back to its previous value. If you read a value that is later rolled back, you will have read an invalid value.
NON-REPEATABLE READS
At time T0, Transaction A reads the product with ID 1001. By time T1, Transaction B updates the ‘price’ column of the product with ID 1001 to $1999 and commits at time T2. Transaction A needs to read the product with ID 1001 again at time T3. It compares the two columns of data: the initial read at time T0 and the latest read at T3. However, Transaction A determines that the data are not equivalent. As a result, this phenomenon is referred to as non-repeatable reads.
A non-repeatable read occurs when transaction A retrieves a row, transaction B subsequently updates the row, and transaction A later retrieves the same row again. Transaction A retrieves the same row twice but sees different data.
PHANTOM READ
Phantom read is pretty similar to non-repeatable reads. There is only one exception among them. “Non-repeatable reads” is valid for one column. Phantom read is valid for a dataset. For instance, At time T0, Transaction A reads the product with ID 1001. By time T1, Transaction B updates the data set of the product (name, price, description etc.) with ID 1001 and commits at time T2. Transaction A needs to read the product with ID 1001 again at time T3. It compares the two sets of data: the initial read at time T0 and the latest read at T3. However, Transaction A determines that the data are not equivalent. As a result, this phenomenon is referred to as Phantom read.
A phantom read occurs when transaction A retrieves a set of rows satisfying a given condition, transaction B subsequently inserts or updates a row such that the row now meets the condition in transaction A, and transaction A later repeats the conditional retrieval. Transaction A now sees an additional row. This row is referred to as a phantom.
In spring, transactional annotation contains isolation-level property, such as DEFAULT, READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, and SERIALIZABLE. But Spring Transaction uses DEFAULT isolation level. It means the default isolation level of the database.
LEVELS
DEFAULT
default isolation level of the database.
Isolation isolation() default Isolation.DEFAULT;
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
method setTransactionIsolation
).
READ_UNCOMMITTED
This is the lowest level of isolation and allows many simultaneous accesses. It cannot prevent problems with concurrency.
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_UNCOMMITTED)
public Product getById(Integer id) {
return productRepository.findById(id).orElse(null);
}
READ_COMMITTED
2nd level isolation stop. DIRTY READ prevents it. But other problems of synchronicity can occur.
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
public Product getById(Integer id) {
return productRepository.findById(id).orElse(null);
}
REPEATABLE_READ
3rd level is isolation. Prevents DIRTY READ and NON-REPEATABLE READ.
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.REPEATABLE_READ)
public Product getById(Integer id) {
return productRepository.findById(id).orElse(null);
}
SERIALIZABLE
It’s the highest level of isolation. It prevents all concurrency problems. Executes concurrent calls sequentially. It is expensive in terms of performance.
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE)
public Product getById(Integer id) {
return productRepository.findById(id).orElse(null);
}
Isolation Level | Transactions | Dirty Reads | Non-Repeatable Reads | Phantom Reads |
---|---|---|---|---|
TRANSACTION_NONE | Not supported | Not applicable | Not applicable | Not applicable |
TRANSACTION_READ_COMMITTED | Supported | Prevented | Allowed | Allowed |
TRANSACTION_READ_UNCOMMITTED | Supported | Allowed | Allowed | Allowed |
TRANSACTION_REPEATABLE_READ | Supported | Prevented | Prevented | Allowed |
TRANSACTION_SERIALIZABLE | Supported | Prevented | Prevented | Prevented |
Post a Comment