What is a transaction
A transaction is a logical set of actions that are either all successful or fail altogether and do not allow partial success.
Attributes of a transaction
Transaction four attributes after the transaction has been defined
of atomic
A transaction is an indivisible unit, and this set of operations in a transaction either occurs or does not occur.
Consistency
Consistency is the implementation of the transaction must be consistent before and after the disappearance of the situation, typically, such as bank transfer operations, a to B transfer, if the first two people have a total of 100 yuan, the transfer completed after the two people have a total of 100 yuan.
Isolation of
When multiple users have concurrent access to the database, a user's transaction cannot be interfered with by another user's office. Data is isolated from each other by multiple concurrent transactions. For example transaction 1,c to a transfer, at this time the transaction 2,a to B transfer. Then two transactions are to modify the balance of a account, an increase, a decrease, How to ensure that the data is correct after they have been changed. This is the requirement of isolation.
Persistence of
Once a transaction is committed, the changes to the database are persistent. There should be no impact even if the database fails.
Isolation level of the transaction
Why should there be isolation levels, because if there is no isolation level, when two transactions are operating on a record at the same time, there may be several scenarios that are often heard.
1 Dirty Reads
Dirty reading means that when a transaction is accessing the data and the data has been modified, and the modification has not yet been submitted to the database, then another transaction accesses the data and then uses the data.
Transaction 1: Update a piece of data
-------> Transaction 2: Read the record for the transaction 1 update
Transaction 1: Call Commit for Commit
Since transaction 2 uses transaction 1 and has not yet committed a record, if transaction 1 is finally properly committed, it's fine, but if transaction 1 is not committed, it is rolled back. Then the operation of transaction 2 is problematic because the data he uses is wrong. That's dirty reading.
2 Non-REPEATABLE READ
In the same transaction, read the same data two times to get different content
Transaction 1: Querying a record
————? > Transaction 2: Update the records of a transaction 1 query
————? > Transaction 2: Call commit for Commit
Transaction 1: Querying the last record again
Transaction 1 has to do two queries to do something like show or use, but the two query events are updated by transaction 2, so transaction 1 has a problem that cannot be read again.
3 Phantom Reads
In the same transaction, read two times with the same action, resulting in a different number of records
Transaction 1: All records in the query table
————? > Transaction 2: Inserting a record
————? > Transaction 2: Call commit for Commit
Transaction 1: Requery all records in the table
At this point the transaction 12 times query to the record is not the same, called the Phantom Read
The focus of phantom reading is new or deleted, because another transaction has been added or deleted in the table, the current transaction every time to see a different number of bars, like the illusion of the same, check a more than one, and then check again, found that there is no.
To this end, the concept of the isolation level is introduced into the transaction, guaranteed by the database
Default uses the isolation level set by the database (defaults), which determines the isolation level by the DBA default setting.
Dirty read, read_uncommitted read, Phantom read (lowest isolation level, high concurrency performance)
Read_committed read, read-only problems (lock rows being read)
Repeatable_read reads (locks all rows Read)
SERIALIZABLE ensure that all conditions do not occur (lock the table)
As you can see, these four kinds of performance are getting worse from top to bottom, and the supportability is getting higher.
To solve the problem of phantom reading, for example, serializable directly to the lock table, then the publication of Phantom read to the table insert and delete can not operate, can only query. So there's no problem.
The propagation behavior of a transaction
The propagation behavior of a transaction is mainly to solve the problem of nested calls, such as the use of transaction operations in a method, and the use of transaction operations in the B method, when a calls B. How does this work?
1 REQUIRED Business method needs to run in a transaction, if the method is running in a transaction, then join the transaction, or create a new transaction yourself. This is the spring default propagation behavior.
2 SUPPORTS If a business method is invoked within a transaction scope, the method becomes part of the transaction, and if the business method is invoked outside the scope of the transaction, the method executes in a transaction-free environment.
3 Mandatory can only be executed in an existing transaction, the business method cannot initiate its own transaction, and if the business method is invoked in an environment without transactions, the exception is thrown
4 The Requires_new business method always initiates a new transaction for itself, if the method is already running in a transaction, the original transaction is suspended, the new transaction is created, until the method ends and the new transaction ends, and the original transaction resumes execution.
5 The Not_supported declaration method requires a transaction, and if the method is not associated with a transaction, the container does not open the transaction for it. If the method is invoked in a transaction, the transaction is suspended, and the original transaction resumes execution after the method call has ended.
6 The NEVER declaration method must never be executed within the scope of a transaction, and if the method executes within a transaction scope, the container throws an exception. Execution is performed only if it is not associated with a transaction.
7 NESTED If an active transaction exists, is run in a nested transaction. If there are no active transactions, the required property executes. It uses a separate transaction that has multiple guaranteed points that can be rolled back. Internal transaction rollback does not affect external transactions. It works only for the Datasourcetransactionmanager transaction manager.
A total of 7, 1,4,7 most important. 1 means that A and B will be in a transaction. and 4 is B. A new transaction will be opened until the end of a, and A's transaction will continue to run.