MySQL - Transaction Management

What problems can arise when creating, updating, reading, deleting are not controlled?

 Data needs to be updated in time after purchase to avoid errors.

What is a transaction?

A transaction is a set of database manipulation statements (DML), a set either all succeed or all fail.

A transaction is an entirety of multiple operations that implement a thing.

A MySQL database is definitely not a transaction running, so when there is a conflict, what should be done with the transactions that execute half of the SQL statements?

Remark:

sql statement:

  • DDL (define database objects, tables and columns),
  • DML (for manipulating record data in database tables),
  • DQL (Query Data),
  • DCL (Define Access Rights and Security Levels)

Therefore, a transaction cannot only be a combination of SQL statements, but also needs to have the following attributes.

  • Atomicity : All operations in a transaction are either completed or not completed, and will not end in a certain link in the middle. If an error occurs during the execution of the transaction, it will be rolled back (Rollback) to the state before the transaction started, as if the transaction was never executed.
  • Consistency : The integrity of the database is not violated before the transaction begins and after the transaction ends. This means that the written data must fully comply with all the preset rules, including the accuracy of the data, the concatenation and the subsequent database can spontaneously complete the predetermined work.
  • Isolation : The ability of the database to allow multiple concurrent transactions to read, write and modify its data at the same time. Isolation can prevent data inconsistency due to cross execution when multiple transactions are executed concurrently. Transaction isolation is divided into different levels, including Read uncommitted, read committed, repeatable read, and Serializable
  • Persistence : After the transaction is completed, the modification of the data is permanent and will not be lost even if the system fails.

Why does business happen

Transactions are designed to simplify our programming model when an application accesses a database.

Version support for transactions

Transactions are only supported by databases or tables using the innodb database engine in MySQL, but not by myisam.

Transaction commit method

There are two common ways to commit transactions:

  • auto commit
  • Submit manually

View transaction submission methods

 Use SET to change MySQL's autocommit mode, remember to change it back

 

 Common business operations

Create test table

Prove transaction start and rollback

 

 

Transaction isolation level

In the database, in order to ensure that the transaction execution process is not disturbed, there is isolation.

In the database, the transaction is allowed to be disturbed to different degrees, and there is an isolation level.

isolation level

  • Read Uncommitted [Read Uncommitted]: At this isolation level, all transactions can see the execution results that are not committed by other transactions.
  • Read Committed: This isolation level is the default isolation level for most databases (not MySQL default). It satisfies the simple definition of isolation: a transaction can only see changes made by other committed transactions.
  • Repeatable Read [Repeatable Read]: This is the default isolation level of MySQL, which ensures that the same transaction will see the same data row when reading and operating data multiple times during execution. But there will be phantom reading problems.
  • Serializable [Serializable]: This is the highest isolation level of transactions, which solves the problem of phantom reads by forcing transaction ordering so that they cannot conflict with each other.

Isolation is basically achieved through locks.

View global isolation level

 View the current session global isolation level

 Set current session or global isolation level syntax

SET [SESSION | GLOBAL] TRANSACTION ISOLATION LEVEL {READ UNCOMMITTED | READ COMMITTED |REPEATABLE READ | SERIALIZABLE}

Set the current session isolation level to serialized

Let's look at the session isolation level again and the change has occurred

 

read uncommitted

 open transaction

Open another terminal B and access it through B

 When a transaction is executing, it reads the data updated (or other operations) but not committed by another executing transaction. This phenomenon is called dirty read.

read commit

repeatable read

serialize

Isolation level comparison

Guess you like

Origin blog.csdn.net/qq_59392324/article/details/121953651