Detailed explanation of mysql transaction management operations

Detailed explanation of mysql transaction management operations

This article describes the MySQL transaction management operation. Share with you for your reference, the details are as follows:

In this article:

  • What is transaction management
  • Transaction management operations
  • Rollback Point
  • Default transaction management

Release date: 2018-04-18


What is transaction management:

  • A series of operations to be performed can be called a transaction, and transaction management is to manage these operations to be either fully executed or not executed at all (a classic example is: A wants to transfer money to B, first A's money is reduced, but suddenly the database loses power, resulting in the inability to add money to B, and then due to data loss, B does not acknowledge receiving A's money; here the transaction is to ensure that both adding money and subtracting money are fully executed or not executed at all. If adding money fails, then subtracting money will not occur).
  • The significance of transaction management: ensuring the integrity of data operations.
  • Not all data engines in MySQL support transaction management, only InnoDB supports transaction management.

Transaction management features:

  • Atomicity: The entire operation of a transaction is a whole and cannot be divided. Either all of it succeeds or all of it fails.
  • Consistency: The data in the data table does not change before and after the transaction operation.
  • Isolation: Transaction operations are isolated from each other and are not affected.
  • Persistence: Once the data is submitted, it cannot be changed, and the data table data is permanently changed.

Transaction management operations:

  • Enable transaction management: After enabling it, the following SQL statements will not be executed immediately and the results will not be written to the table, but will be written to the transaction log.
    • start transaction;
  • Rollback operation: Rollback will clear the content written to the transaction log after the transaction management is started, that is, restore to the state before the transaction management is started.
    • Syntax: rollback;
    • Note: The rollback operation only rolls back the "written" content, and cannot roll back the ordinary table read select statement.
  • Transaction commit: write the results of the SQL statement to the data table.
    • Syntax: commit:

Experimental table:

create table bankaccount(id int primary key auto_increment,name varchar(15),money int);
insert into bankaccount(name,money) values("Jobs",2000);
insert into bankaccount(name,money) values("Bill",3000); 

image

image

Replenish:

  • When a commit or rollback statement is executed, the transaction is automatically closed (future changes are implicitly committed).
  • Lock mechanism: When a transaction operates a table, if an index is used to retrieve a value, the corresponding row will be locked; if an index is not used to retrieve a value, the entire table will be locked. After locking, other connections cannot operate on the specified row or table.

Rollback point:

  • The rollback point can specify the rollback location. For example, if you have typed 100 commands and found that the 81st command was typed incorrectly, you can save a lot of time if you roll back to a point before command 81 instead of rolling back to before the transaction was started. 】
  • grammar:
    • Create a rollback point: savepoint rollback point name;
    • Rollback to the rollback point: rollback to rollback point name;

image

Replenish:

  • The rollback point becomes invalid after the transaction management is closed (after rollback or commit). Do not use the rollback point outside of the transaction.

Default transaction management:

  • By default, MySQL transaction management is turned off (automatic transaction), and the results of the statement are written to the data table immediately.
    • You can use show variable like 'autocommit' to check whether automatic transactions are enabled. A value of 1 means that automatic transactions are enabled, and a value of 0 means that they are disabled.
  • Turn off automatic transactions: set autocommit = 0; [After turning it off, commit is required to execute each statement, which is equivalent to starting transaction management]
    • However, please note that set autocommit is for session variables, so this setting is only effective in this session connection.

Readers who are interested in more MySQL-related content can check out the following topics on this site: "Summary of MySQL transaction operation skills", "Summary of MySQL index operation skills", "Summary of MySQL commonly used functions", "Summary of MySQL log operation skills", "Summary of MySQL stored procedure skills" and "Summary of MySQL database lock-related skills".

I hope this article will be helpful to everyone's MySQL database design.

You may also be interested in:
  • Detailed explanation of long transaction examples in MySQL
  • Detailed explanation of MySQL transactions and MySQL logs
  • Detailed explanation of the implementation principle of MySQL transactions and Spring isolation levels
  • MySQL transaction concepts and usage in-depth explanation
  • Example of viewing and modifying MySQL transaction isolation level
  • MySQL transaction, isolation level and lock usage example analysis
  • Detailed example of how to implement transaction commit and rollback in mysql
  • MYSQL transaction tutorial Yii2.0 merchant withdrawal function
  • How to find out uncommitted transaction information in MySQL
  • Detailed explanation of the implementation principle of ACID transaction in Mysql
  • MySQL cross-database transaction XA operation example
  • In-depth understanding of MySQL long transactions

<<:  JavaScript data visualization: ECharts map making

>>:  How to run the springboot project in docker

Recommend

Causes and solutions for MySQL too many connections error

Table of contents Brief summary At noon today, th...

Learn Vue middleware pipeline in one article

Often when building a SPA, you will need to prote...

Vue uses Amap to realize city positioning

This article shares the specific code of Vue usin...

mysql group_concat method example to write group fields into one row

This article uses an example to describe how to u...

The implementation of event binding this in React points to three methods

1. Arrow Function 1. Take advantage of the fact t...

Several ways to implement "text overflow truncation and omission" with pure CSS

In our daily development work, text overflow, tru...

Understanding flex-grow, flex-shrink, flex-basis and nine-grid layout

1. flex-grow, flex-shrink, flex-basis properties ...

Summary of the data storage structure of the nginx http module

Starting from this section, we will explain the i...

Use javascript to create dynamic QQ registration page

Table of contents 1. Introduction 1. Basic layout...

React dva implementation code

Table of contents dva Using dva Implementing DVA ...

Detailed explanation of the application of CSS Sprite

CSS Sprite, also known as CSS Sprite, is an image...

Steps for Docker to build a private warehouse Harbor

Harbor Harbor is an open source solution for buil...

CSS3 implementation example of rotating only the background image 180 degrees

1. Mental Journey When I was writing the cockpit ...

Web Design Teaching or Learning Program

Section Course content Hours 1 Web Design Overvie...