The concept of transaction : It is a sequence of database operations defined by the user. These operations are either all done or not done at all, which is an inseparable unit of work. A transaction can be an SQL statement, a group of SQL statements, or an entire program. Generally speaking, a program will contain multiple transactions. The start and end of the transaction can be controlled by the user. If the user does not explicitly define the transaction, the DBMS will automatically divide the transaction according to the default. In SQL, there are three statements that define a transaction: BEGIN TRANSACTION / COMMIT / ROLLBACK.
Four characteristics of transactions :
Atomicity: A transaction is a logical unit of work in the database, and the operations included in the transaction are either all done or not done at all.
Consistency: The result of transaction execution must be to bring the database from one consistent state to another. A database is said to be in a consistent state if all operations are successfully committed during a transaction operation. If the database system fails during operation, some transactions are not completed and are forced to abort. Some of the modifications made to the database by these unfinished transactions have already been written to the physical database, which means the database is in an incorrect state. It can be seen that consistency and atomicity are closely related.
Isolation: The execution of a transaction cannot be interfered with by other transactions. Transactions executing concurrently cannot interfere with each other.
Persistence (durability): Once a transaction is committed, its changes to the data in the database should be permanent.
Operation of transactions : Transactions can be executed serially one by one, that is to say, only one transaction can be run at a time, and other transactions can only run after this transaction is completed. However, during the execution of transactions, different resources may be required, sometimes CPU, sometimes database access, sometimes I/O, and sometimes communication. If transactions are executed serially, many system resources will be is idle, so in order to fully utilize system resources, multiple transactions should be allowed to run in parallel.
Parallel execution of transactions (single processor): The parallel execution of transactions is essentially the parallel operations of these parallel transactions running alternately in turn. In a multiprocessor system, true simultaneous concurrent execution can be achieved.
Note: A transaction is the basic unit of concurrency control . During the parallel execution of transactions, multiple transactions may be involved in accessing the same piece of data at the same time, which may result in data corruption. Hence the emergence of locking technology.
Blocking (locking) : It is a very important technique for implementing concurrency control. (For example, before a transaction A operates on a data object such as a table or record, it first sends a request to the system and locks it. After locking, transaction A has certain control over the data object, and transaction A has no control over the data object. Before the release, other transactions should update the data object) It is divided into write locks and read locks.
Write lock : If a write lock is added to a data object in a transaction A, only A is allowed to read and modify the data, and other transactions cannot lock the data object until A releases the lock , other transactions can operate on it. (Popular point: if a transaction adds a write lock, no other transaction can lock her).
Read lock : If transaction A has a read lock on a piece of data, then only A is allowed to read the data and cannot perform modification operations. Other transactions can also perform read lock operations on the data, but cannot add write lock operations.
Livelock : Transaction A locks data X, then transaction B requests to lock data X, and then transaction C requests to lock data X. When A releases the lock of data X, the system hands data X to the transaction first. C, and then transaction D requests to lock data X again. When transaction B is used up, the system hands over data X to D, so that transaction B is always in a waiting state, which is the so-called livelock state. This method can be solved by using a first-come, first-served strategy.
Deadlock : When transaction A locks data X, it needs data Y, and transaction B needs data X after locking data Y. Neither of them releases their own part of the resources, and they are both in a state of waiting for a transition. This situation is a deadlock.
Several solutions to deadlock :
1. Deadlock Prevention
1.1 One-time blocking method: lock all the data required by transaction A, otherwise it cannot be executed. Several features exposed by this method: when the amount of data is too large, it will reduce the concurrency of the bear;
1.2 Sequential locking method: prescribe a blocking order for the data, and all transactions must be blocked in this order.
2. Diagnosis and removal of deadlock:
2.1. Timeout method: specify a time, if a transaction exceeds the specified time, it is considered a deadlock.
2.2 Waiting graph method
Serializability of concurrent scheduling : the concurrent execution of multiple transactions is correct if and only if the result is the same as the result of executing these transactions serially in a certain order, this scheduling strategy is called serializable .
Two -end lock protocol (a common rule for data locking) : The transaction is divided into two stages. The first stage is to obtain the blockade. In this stage, the transaction can apply for any type of lock on any data item, but cannot release any Lock. The second phase is to release the blockade, in this phase, the transaction can release any type of lock on any data item, but can no longer apply for any locks.
Programmatic transaction : Programmatic transaction requires you to directly add transaction processing logic in the code. You may need to explicitly call beginTransaction(), commit(), rollback() and other transaction management related methods in the code, such as executing a method When you need transaction processing, you need to start the transaction at the beginning of the a method, and then process it. At the end of the method, close the transaction.
Declarative transaction : The practice of declarative transaction is to add annotations around the a method or define it directly in the configuration file. The a method requires transaction processing. In spring, it will be intercepted before and after the a method through the configuration file, and transactions will be added.
两者区别:编程式事务侵入性比较强,但处理粒度更细.
--------------------------------------------------------------Java中的事务分类(下面为转载)------------------------------------------------------------------------
转自:http://lavasoft.blog.51cto.com/62575/53815/
内容如下: