What is database concurrent access
- Uncontrolled concurrent access to the same data by multiple users may result in incorrect data being accessed and stored
- DBMS must provide concurrency control mechanism
- Transaction is the basic unit of concurrency control and recovery
affairs
- concept
- From the user's point of view, some operations on the database should be a whole , that is, an independent work unit, inseparable
- Example: Electronic funds transfer (transfer a sum of money from account A to account B) may be considered by the customer as an independent operation, while DBS is composed of several operations
- definition
- A transaction is a sequence of database operations defined by the user . These operations are either done or not done. It is an indivisible unit of work.
- Transaction and program are two concepts
- In a relational database, a transaction can be a single SQL statement, a set of SQL statements, or an entire program
- An application usually consists of multiple transactions
- Transactions are the fundamental unit of recovery and concurrency control
- Two ways of defining transactions
- explicit way
- The start of a transaction is explicitly controlled by the user or automatically implied by the DBMS
- The end of the transaction is explicitly controlled by the user
- implicit way
- When the user does not explicitly define a transaction, the DBMS automatically divides the transaction by default
- explicit way
- Explicitly define transactions
BEGIN TRANSACTION ←—— 事务开始 SQL语句1 SQL语句2 ...... COMMIT ←—— 事务结束 BEGIN TRANSACTION ←—— 事务开始 SQL语句1 SQL语句2 ...... ROLLBACK ←—— 事务结束
- transaction end statement
- COMMIT
- Transaction ended normally
- Commit all operations of the transaction (read+update)
- All updates to the database in the transaction take effect permanently
- ROLLBACK
- transaction terminated abnormally
- A failure occurred during the operation of the transaction, and it cannot continue to execute, and all update operations of the transaction are rolled back
- The transaction is rolled back to the state it was in when it started
- COMMIT
- ACID properties
- Atomicity _ _
- A transaction is a logical unit of work for a database
- All operations included in the transaction are either done or not done
- A transaction is a logical unit of work for a database
- Consistency _ _
- The result of transaction execution must be to change the database from one consistent state to another consistent state
- Consistent state: the database contains only the results of successful transaction commits
- Inconsistent state: the database contains the results of failed transactions
- Consistency and atomicity are closely related.
Example: bank transfer: withdraw 10,000 yuan from account A and deposit it into account B.
Define a transaction that includes two operations. The
first operation is to subtract 10,000 from account A The
second operation is to add 10,000 yuan to account B.
These two operations can be done entirely or not at all, and
the database is in a consistent state.
If only one operation is performed, the user will logically An error occurred, 10,000 yuan was missing, and the database was in an inconsistent state
- The result of transaction execution must be to change the database from one consistent state to another consistent state
- Isolation _ _
- The execution of a transaction cannot be interfered with by other transactions
- The operations and data used within a transaction are isolated from other concurrent transactions
- Transactions executed concurrently cannot interfere with each other
- The execution of a transaction cannot be interfered with by other transactions
- Durability _ _
- permanence also known as permanence
- Once a transaction is committed, its changes to the data in the database should be permanent
- Subsequent other operations or failures should not have any effect on its execution results
- permanence also known as permanence
- Ensuring transaction ACID properties is an important task in transaction processing
- Factors that destroy the ACID properties of transactions
- When multiple transactions are running in parallel , the operations of different transactions are executed cross-executed
- The DBMS must ensure that the cross-running of multiple transactions does not affect the ACID properties of these transactions, especially atomicity and isolation
- The transaction was forcibly stopped during operation
- The DBMS must ensure that a transaction that is forcibly terminated has no impact on the database or other transactions
- When multiple transactions are running in parallel , the operations of different transactions are executed cross-executed
- Atomicity _ _
- Multi-transaction execution mode
- Transaction serial execution
- Only one transaction runs at a time, and other transactions must wait until the end of this transaction before they can run
- Cannot make full use of system resources and give full play to the characteristics of database shared resources
- cross concurrency
- The parallel execution of transactions is that the parallel operations of these parallel transactions run alternately
- It is a concurrency method in a single processor system, which can reduce the idle time of the processor and improve the efficiency of the system
- Simultaneous mode
- In a multi-processor system, each processor can run one transaction, and multiple processors can run multiple transactions at the same time, realizing the real parallel operation of multiple transactions
- The most ideal way of concurrency, but subject to the hardware environment
- Transaction serial execution
- Problems caused by concurrent execution of transactions
- Uncontrolled operation of multiple users concurrently accessing the same data may access and store incorrect data, destroying transaction isolation and database consistency
- DBMS must provide concurrency control mechanism
- The concurrency control mechanism is one of the important indicators to measure the performance of a DBMS
Inconsistency between concurrent operations and data
Causes:
① Caused by concurrent operations of two transactions
② In the case of concurrent operations, the scheduling of the operation sequence of the two transactions is random
③ If the above scheduling sequence is executed, the transaction modification will be discarded
④ Because of the fourth step After the transaction modifies A and writes it back, it overwrites the modification of the transaction
- Data inconsistency caused by concurrent operations
- lost modification
- non-repeatable read
- read-update
- Transaction 1 reads a certain data
- Transaction 2 modified it
- When transaction 1 reads the data again, it gets a different value from the previous one
- read-delete
- Transaction 1 reads certain data records from the database according to certain conditions
- Transaction 2 deleted some of the records
- When transaction 1 reads data again according to the same conditions, it is found that some records disappear mysteriously
- read-insert
- Transaction 1 reads certain data records from the database according to certain conditions
- Transaction 2 inserted some records
- When transaction 1 reads data again according to the same conditions, it finds some more records
- read-update
- read "dirty" data
- lost modification
Scheduling of Concurrent Operations
- The computer system's scheduling of parallel operations in parallel transactions is random, and different scheduling may produce different results
- The scheduling strategy that serializes all transactions must be the correct scheduling strategy
- If there are no other transactions running at the same time during the running of a transaction, that is to say, it is not disturbed by other transactions, then it can be considered that the running result of the transaction is normal or expected
serializable scheduling
- Serial execution of transactions in a different order may also produce different results but since it does not put the database in an inconsistent state, all can be considered correct
- Parallel execution of several transactions is correct
- iff the result is the same as executing them serially in some order. This parallel scheduling strategy is called serializable scheduling
- Serializability is the only criterion for the correctness of
parallel transactions Example: Now there are two transactions, including the following operations:
transaction 1: read B; A=B+1; write back to A;
transaction 2: read A; B=A +1; Write back to B;
Assume that the initial value of A is 2, and the initial value of B is 2.
non-serializable schedule
serializable scheduling
Serializable Scheduling Policy
- In order to ensure the correctness of parallel operations, the parallel control mechanism of DBMS must provide certain means to ensure that the scheduling is serializable
- Theoretically speaking, the scheduling strategy that prohibits the execution of other transactions when a certain transaction is executed must be serializable scheduling, which is also the simplest scheduling strategy
- But this approach is practically infeasible because it prevents users from fully sharing database resources
practice questions
A method to ensure the correctness of concurrent operation scheduling
-
block method
-
Two-stage lock (2PL) protocol
-
-
time stamping method
-
optimistic approach