search
HomeDatabaseMysql TutorialAbout Mysql transactional

About Mysql transactional

Oct 13, 2017 am 10:30 AM
mysqlabout

1》What does transaction refer to?
         A transaction can be regarded as a business logic processing unit, this unit is either executed or not executed;
2》ACID principle:
 (1) Atomicity(Automicity)
 (2) Consistency (Consistency)
A account has 3,000 yuan, B account 2000 fast,
-A --500-"B
A account and B account is 5000 fast
Before the implementation and after the execution , this sum and this state should be consistent.
(3) isolation (LSOLATION)
A account from 3000-"500-& gt; B account 2500
update (1) Transfer of transfers
select Sun () a b Jianhe
(4) Durability
Guaranteed persistence:
1 & gt before handling:
Crossing the data to the disk before. What if you want to withdraw it? Data recalled from disk? Very slow? it's painful? what to do?
2 & gt; combined with the transaction aspiration to complete the
things log, there is also a disk on the disk. What is the difference between it and the data file?
            Difference: The transaction log generates sequential I/O, while the disk data file is random I/O. Each operation of the transaction log is written sequentially to a continuous storage block on the disk, and our data file it's not true.
(1) Things Log: Sequence IO
(2) Data file: Random Io
Write the transaction into the transaction log. After a while, the background or related process of the thing will be Implemented into data files, this way we ensure the persistence of the data operated by things.

3》The status of the transaction.
     (1) Either an active transaction
         The transaction is being executed
 (2) Partially committed transaction
             We know that the transaction is either executed or not executed , what is partial submission?
          That is, the transaction has been executed, part of it has been written to the disk, and the other part is being executed, and the last execution statement is in the process of being written. We call it partial submission
        (3) Failed transaction
                The transaction is normal Submitted, but did not achieve the purpose, it is called a failed transaction
       (4) Aborted transaction
               Aborted transaction is called not submitted, or it ends prematurely halfway, which is called abort.
(5) The transaction submitted
Health submitted by the health, our affairs must be a certain middle of these 5 states. Once the transaction is submitted, it cannot be revoked.

4 "Equipment and scheduling of the transaction
Affairs:
1 & gt; increase throughput and resource utilization rate
2 & gt; reduce waiting time
Affairs to reduce each other Between the effects, we need to isolate the transaction, what if it is isolated? This depends on transaction scheduling. Any database system itself has such complexity in transaction scheduling. The scheduling method integrates many strategies to complete the scheduling. Generally speaking, in order to ensure the isolation and atomicity of transactions, they need to use transaction scheduling. To complete, there are two general strategies for scheduling:

transaction scheduling:
1 & gt; recovery scheduling
Generally, when the two transactions are scheduled between each other, the cross execution of any two transactions will not lead to the change of another transaction. We Call it as: recovery scheduling

# 2 & gt; No -level joint adjustment
to restore scheduling and step by step means that after the end of the transaction, they have no impact on each other, especially in affairs, in affairs, in affairs It will still be affected when rolling back, and it will still be affected when the transaction is rolled back. Therefore, in order to avoid the impact during rollback, you can also implement what is called: cascade-free scheduling.

5》Isolation of Transactions
We know about transaction scheduling. A transaction may involve multiple operations. These operations can be cross-executed. The existence of transaction scheduling is to better schedule the order so that these cross-executions can be interconnected with each other. It will not have an excessive impact, so in order to further reduce the impact between transactions, we have so-called isolation levels. There are four isolation levels for transactions. Which four?
1 & gt; Read Read UNCOMMITTED
at the isolation level. All transactions can see the execution results of other unprepared transactions. How much is the level? Reading the unsurmitted data is also becoming: Dirty Read (Dirty Read)
2 & gt ), which satisfies the simple definition of isolation: a transaction can only see changes made by transactions that have been committed; When multiple instances read data concurrently, they will see the same data rows. However, in theory, this leads to another problem: phantom read (Phantom read). Simply put: phantom read refers to reading a certain range as a user. When the data row is in this range, another transaction inserts a new row in this range. When the user reads the data row in this range, he will find a new "phantom row"
. Innodb and Falcon engines use multi-version concurrency control (MVCC) mechanism solves this problem.

4 & GT; Serial serializable
This is the highest -level isolation mechanism. It can conflict with each other through mandatory transactions, so as to solve the problem of illusion reading. Simply put: it is in A shared lock is added to each read data row. At this level, it may lead to a large number of timeouts and lock competition. Concurrency control is implemented, and our real-time concurrency control

          technology relies on the following technologies:

                        (1). Lock. Read lock, write lock, exclusive lock, shared lock.
                                                                                                                                               . Timestamp, the start time and execution time of each transaction must be recorded
#              When a transaction applies for a type of lock, in order to achieve concurrency control, it needs this lock, but due to the results of transaction scheduling, it has never been able to obtain the lock. The result is:
                                                                                         . In addition, there is another kind of lock called: deadlock

6》Start transaction and rollback

Generally speaking, the operation of starting a transaction:         
                                                                                                                                                                                                                      Start transaction 2>Delete information in one of the student tables
    delete from student where num='4'
​ ​ 3>How to perform data recovery? Transaction rollback. Guessed it.
                                                                                                                                                                                                                                       
#                                                                                                                              select * from student;
                                                                                                           3>Transaction submission
                        commit ;
          4>Try to rollback again
                                                                                                                  rollback
                                                                select * from student; At that time, I found that the 75th one was wrong and the execution was wrong. Do I need to roll back the first 80? What should we do at this time? The role of save points comes out. For example: I save every 10 operations: ​​​Affairs
Start Transaction;
2 & GT; Delete the information of "Cuihua" in one Student table
Delete from Student Where Name = 'Cuihua'
Save a transaction point
Savepoint yya
3 & gt; Delete the information of "Ximen Blog" in one of the Student tables

Delete from Student Where Name = 'Ximenchuiniu'

Savepoint yyb
4 & GT; "Two" Information
                    delete from student where name='niuer'
                               savepoint                   ’’ ’     through ’s ’’s ’ through ’’s' through ’ through through through through through‐‐‐‐‐‐‐‐ ‐ to 5>                                 rollback to yya Think about it. What is the save point yya? ​ ​
​ ​ ​ 6>Check whether the recovery is successful
​ ​ ​ ​ ​select * from student;

6.3 Automatic submission of transactions
1 & gt; Query mysql transaction automatically submit
SELECT @@ AutoCommit;
MySQL & GT; Select @@ AutoCommit;
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----+
                                                                                                                                          ------+
                                         1 row in set (0.00 sec)

                                                                                                               >Test Verification
Delete from student where name = 'ximenchuniu'
Rollback;
Does it return? And we didn't explicitly start a transaction? Transactions support DML statement rollback. DDL is a hidden test commit, so unfortunately it is not supported.

Verify that read is uncommitted:
                                                                                                                                                                          together  together  together  out    Query the isolation level select @@tx_isolation; Affairs
Window One Window Two Common Operation:
1.Select @@ tx_isolation;
2.Set tx_isolation = 'Read_uncommittd';

## Window 1 Operation:

1.start transaction; Start a cross transaction
        2.update student set age='70' where d_id='1010';

Window 2 operation
        1.start transaction;  Start a cross transaction
2.Select * from Student; You can see that the 1 window is changed to 70, and the window transaction is not submitted. It is seen by 2, which is called

verification read and submitted
, open In the two windows, the two parties crossed a transaction
window 1 and window 2 common operation:
1.Select @@ tx_isolation;
2.Set tx_isolation = 'read_committd'
## window window 1 Operation:
1.start Transaction; Start a cross -crossing transaction
2.Update Student Set Age = '70 'where d_id =' 1010 ';

3.Commit; The third step of the window will see the data I submitted again


2 Operation
1. Start Transaction; Start a cross transaction
2.Select * from Student; Updated data
3.Select * from Student; When a window A executes 3 steps, you can see the update data and avoid dirty reading

Verification can be re -read
A MySQL, open two windows, and the two parties crossed a transaction
Phantom Reading:
definition: the same transaction T1, in two different time periods, such as 1 2 execution, execute On the same day, the query statements get the records of the records are differently called Phantom Reading

Window 1 and Window 2 Common operation:
1.Select @同 同 同 同 同 同';

window 1 Operation
1.start transaction; Start a cross -crossing transaction
2.Update Student Set Age = '60' where num = '10 ';
3.Commit

2 Operation
1. Start Transaction; Start a cross transaction
2.Select * from Student; ; 1commit;Still not see the updated data in window 1
In the two windows, the two parties cross the business of a transaction
两 1 and window 2 common operation:
1.Select @@ tx_isolation;

2.Set tx_isolation = 'Serializable';

## 两1 operation
                1.start transaction; start a cross transaction
              2.update student set age='60' where num='10'; cannot be executed. It can only be executed after Window 2 submits the operation!

2 Operation
1.start Transaction; Start a cross -crossing transaction
2.Commit; Submit here, the Update of the 1 window can be successfully executed. This is serializable!
Reason:
Two transactions to cross the same data at the same time, one party must submit first. Otherwise, reading and writing will block each other.

The above is the detailed content of About Mysql transactional. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
MySQL's Place: Databases and ProgrammingMySQL's Place: Databases and ProgrammingApr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL: From Small Businesses to Large EnterprisesMySQL: From Small Businesses to Large EnterprisesApr 13, 2025 am 12:17 AM

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

What are phantom reads and how does InnoDB prevent them (Next-Key Locking)?What are phantom reads and how does InnoDB prevent them (Next-Key Locking)?Apr 13, 2025 am 12:16 AM

InnoDB effectively prevents phantom reading through Next-KeyLocking mechanism. 1) Next-KeyLocking combines row lock and gap lock to lock records and their gaps to prevent new records from being inserted. 2) In practical applications, by optimizing query and adjusting isolation levels, lock competition can be reduced and concurrency performance can be improved.

MySQL: Not a Programming Language, But...MySQL: Not a Programming Language, But...Apr 13, 2025 am 12:03 AM

MySQL is not a programming language, but its query language SQL has the characteristics of a programming language: 1. SQL supports conditional judgment, loops and variable operations; 2. Through stored procedures, triggers and functions, users can perform complex logical operations in the database.

MySQL: An Introduction to the World's Most Popular DatabaseMySQL: An Introduction to the World's Most Popular DatabaseApr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

The Importance of MySQL: Data Storage and ManagementThe Importance of MySQL: Data Storage and ManagementApr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system suitable for data storage, management, query and security. 1. It supports a variety of operating systems and is widely used in Web applications and other fields. 2. Through the client-server architecture and different storage engines, MySQL processes data efficiently. 3. Basic usage includes creating databases and tables, inserting, querying and updating data. 4. Advanced usage involves complex queries and stored procedures. 5. Common errors can be debugged through the EXPLAIN statement. 6. Performance optimization includes the rational use of indexes and optimized query statements.

Why Use MySQL? Benefits and AdvantagesWhy Use MySQL? Benefits and AdvantagesApr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

Describe InnoDB locking mechanisms (shared locks, exclusive locks, intention locks, record locks, gap locks, next-key locks).Describe InnoDB locking mechanisms (shared locks, exclusive locks, intention locks, record locks, gap locks, next-key locks).Apr 12, 2025 am 12:16 AM

InnoDB's lock mechanisms include shared locks, exclusive locks, intention locks, record locks, gap locks and next key locks. 1. Shared lock allows transactions to read data without preventing other transactions from reading. 2. Exclusive lock prevents other transactions from reading and modifying data. 3. Intention lock optimizes lock efficiency. 4. Record lock lock index record. 5. Gap lock locks index recording gap. 6. The next key lock is a combination of record lock and gap lock to ensure data consistency.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools