Four characteristics of Java and database transaction isolation level

ACID Four properties

Atomicity (Atomicity)

       Atomicity refers to all operations in the transaction included either all succeed, or all fail rolled back. Failure to roll back the transaction operation, will not have any impact on the database

Consistency (Consistency)

       Consistency means that the transaction must transform the database from one consistent state to another consistent state, that is to say before and after a transaction execution and implementation must be in a consistent state. For example transfer operation: A Account B into account 1000 yuan, A little account of the 1000, B accounts more than 1000; after the end of the transaction the total amount remains unchanged.

Isolation (Isolation)

        Isolation refers to the transaction when multiple threads concurrently access the database, such as simultaneous access to a table, open the database for each user and can not be doing other office operations interference between multiple concurrent transactions, they should be isolated from each other.
For example, while there are two concurrent transactions T1 and T2, T1 from the point of view, or to T2 T1 has ended before the execution, execution or after completion of the start of T1. The isolated multiple transactions, each transaction can have access to the other state during the transaction operations.

Isolation levels and divided into four, the following description will do

Persistent (Durability)

Persistence refers to the operation of its services, once submitted, changes to the data in the database is permanent, even if the database failure can not be lost has been submitted to change the firm completed.

Transaction isolation level

Let's look at reading exceptions:

Dirty read: A transaction database is using a data but not yet committed, another transaction B also have access to this data, also uses this data, which will lead to the use of the data before transaction B A transaction did not submit.

Non-repeatable read: a plurality of operations in a transaction data A, in the middle of these two or more times to access the data, the data transaction B also operates, and its value has changed, which leads to the same transaction A the values ​​are different in the two operations when this data, which is non-repeatable read.

Magic Reading: A transaction re-executes a query returning a set of rows match the query, and found these lines because other transactions recently submitted and changed

Isolation level from low to high is divided into four levels:

1, Read uncommitted (read uncommitted): 

Write transaction to prevent other write transactions to avoid missing update. But it does not prevent other read transactions, may lead to dirty read

2, Read committed (read committed):

Read data transaction allows other transactions continue to access the rows of data, but write uncommitted transactions will prevent other transactions from accessing the row.

The isolation level to avoid dirty reads, but may appear non-repeatable read. A pre-read transaction data, transaction B immediately update the data and submit the transaction, while Transaction A reads the data again, the data has changed.

3, Repeatable read (repeatable read):

      Read data transaction would be prohibited by a write transaction (but allowing the read transaction), the write transaction to prohibit any other transactions.

Avoid non-repeatable reads and dirty reads, but sometimes may appear phantom reads. This "exclusive write locks" through "shared read locks" and implementation.

4, Serializable (serialization):

       Reading shared locks, plus exclusive write locks. Such transactions can be read concurrently, but between the read and write transactions are mutually exclusive, a transaction executed, before executing the next transaction serialization is the highest transaction isolation level, but the price is also the most expensive, the performance is very low, generally very little use, at that level, the branch order, not only to avoid dirty reads, non-repeatable read, but also to avoid the phantom read.

 

       The higher the isolation level, the more we can ensure the integrity and consistency of the data, but the impact on concurrent performance is also greater. For most applications, the database system can give priority to the isolation level to Read Committed. It is possible to prevent dirty reads, but also has better concurrent performance. Although it will lead to non-repeatable read, phantom read and II lost update these concurrency issues

     The default level for most of the database is Read committed, such as Sql Server, Oracle.
     Mysql default isolation level is Repeatable read.

 

Guess you like

Origin www.cnblogs.com/jiezai/p/11262229.html