Issues related to transactions in MySQL

affairs

1. Overview of the business:

1. Transaction processing (transaction operation): Ensure that all transactions are executed as a unit of work, and even if a failure occurs, this execution method cannot be changed. When multiple operations are performed in one transaction, either all transactions are committed (commit), then these modifications are permanently saved; or the database management system will discard all modifications made, and the entire transaction is rolled back (rollback) to initial state.

  • Commit: Indicates that all operations in the transaction are determined to be executed
  • Rollback (rollback): means to restore the data to before the transaction execution
  • In MySQL, one SQL statement exclusively occupies one transaction, and is automatically committed by default
  • In MySQL, if a transaction is not committed or rolled back, it will be rolled back by default
  • Transactions are only valid for DML (addition, deletion and modification), not valid for DDL

2. ACID properties of transactions:

  • Atomicity : It means that a transaction is an indivisible unit of work, and the operations in the transaction either all occur or none occur.
  • Consistency: A transaction must transform the database from one consistent state to another.
  • Isolation: The execution of a transaction cannot be interfered by other transactions, that is, the operations and data used within a transaction are isolated from other concurrent transactions, and the concurrently executed transactions cannot interfere with each other.
  • Durability: Once a transaction is committed, its changes to the data in the database are permanent, and other subsequent operations and database failures should not have any impact on it.

2. Open and end transactions

There are two ways to start a transaction:

  • Set autocommit = false; When using this method, this method is for all operations in the current connection, that is, after setting "set autocommit = false;", all operations must be manually set to commit or rollback
  • start transaction; start a short transaction, each transaction will start with "start transaction;" and end with commit or rollback; subsequent operations are still automatically committed

3. Transaction isolation level

1. Transaction concurrency issues

For multiple transactions running concurrently, when these transactionsaccess the same data in the database, if the necessary isolation mechanisms are not adopted, various concurrency issues can result:

  • Dirty read : For two transactions T1, T2, T1 reads fields that have been updated by T2 but not yet committed . After that, if T2 rolls back, the content read by T1 is temporary and invalid.
  • Non-repeatable read : For two transactions T1, T2, T1 reads a field, and then T2 updates the field. Afterwards, T1 reads the same field again, and the value is different.
  • Phantom read : For two transactions T1, T2, T1 reads a field from a table, and then T2 inserts/deletes some new rows in that table . Afterwards, if T1 reads the same table again, there will be more/less rows.

2. Transaction isolation level

Isolation of database transactions : The database system must have the ability to isolate and run various transactions concurrently, so that they will not affect each other and avoid various concurrency problems. ** The degree to which a transaction is isolated from other transactions is called the isolation level. **The database specifies a variety of transaction isolation levels. Different isolation levels correspond to different interference levels. The higher the isolation level, the better the data consistency, but the weaker the concurrency.

There are 4 transaction isolation levels provided by the database:

isolation level describe
read-uncommitted read uncommitted Transaction A is allowed to read uncommitted and committed data of other transactions. Dirty reads, non-repeatable reads, and phantom reads will occur
read-committed read committed Only transaction A is allowed to read data submitted by other transactions. Dirty reads can be avoided, but non-repeatable reads and phantom reads still occur
repeatable-read repeatable read (default) Ensures that a transaction can read the same value from a field multiple times. During the duration of this transaction, other transactions are prohibited from updating this field. Dirty reads and non-repeatable reads can be avoided. But the phantom read problem still exists. Note: MVCC multi-version control technology is used in mysql. Phantom reading can also be avoided at this level, that is, in the current transaction, you can only see the results executed in your own transaction
serializable serialization Make sure a transaction can read the same row, the same record, from a table. During the duration of this transaction, other transactions are prohibited from performing insert, update, and delete operations on the table. All concurrency problems can be avoided, but performance is very poor.

3. Set and view the isolation level

Set the isolation level of the database must be before starting the transaction
Every time a mysql program is started, a separate database connection will be obtained. Each database connection has a variable @@transaction_isolation, which represents the transaction isolation level of the current connection. The mysql service also has a global variable @@global.transaction_isolation, indicating the default transaction isolation level for all connections.

  • View the isolation level of the current mysql connection:

mysql 5 is: select @@tx_isolation;
mysql 8 is: select @@transaction_isolation;

  • View the global isolation level:

mysql 5 是:select @@global.tx_isolation;
mysql 8 是:select @@global.transaction_isolation;

  • Set the isolation level of the current mysql connection:

mysql 5 是:
set tx_isolation =‘read-uncommitted’;
set tx_isolation =‘read-committed’;
set tx_isolation =‘repeatable-read’;
mysql 8 是:
set transaction_isolation =‘read-uncommitted’;
set transaction_isolation =‘read-committed’;
set transaction_isolation =‘repeatable-read’;

  • Set the global isolation level of the database system:

mysql 5:set global tx_isolation =‘read-committed’;
mysql 8 : set global transaction_isolation =‘read-committed’;

Note: The isolation level here is a minus sign, not an underscore.

  • Set the isolation level in JDBC:
    insert image description here
    The isolation level setting is only valid for the current connection. For the MYSQL command window, a window is equivalent to a connection. The isolation level set for the current window is only valid for the transaction in the current window. For JDBC operations As far as the database is concerned, a Connection object is equivalent to a connection, and the isolation level set for the Connection object is only valid for the Connection object, and has nothing to do with other connection Connection objects

Guess you like

Origin blog.csdn.net/qq_52370789/article/details/129626573