Understanding of MySQL transaction commit and rollback errors

Source: Internet
Author: User

Understanding of MySQL transaction commit and rollback errors

I. Cause

Begin or start transaction to START a TRANSACTION

Rollback transaction rollback
Commit transaction validation

The transaction is interpreted as follows: a transaction is composed of one or more SQL statements as a separate unit. If one of the statements cannot be completed, the entire unit will be rolled back (UNDO ), all affected data will be returned to the status before the start of the transaction. Therefore, the transaction can be successfully executed only when all statements in the transaction are successfully executed.

There is no problem with this sentence. The problem is that I understood it incorrectly. The problem in my test is described as follows:

There are two insert statements in the mysql transaction. The second statement is incorrect. After the transaction is run, the first statement is still inserted. The Code is as follows.

// Create a table:
Create table 'test _ tab '(
'F1' int (11) not null,
'F2' varchar (11) default null,
Primary key ('f1 ')
)
ENGINE = InnoDB

// Execute the transaction:
Start transaction;
Insert into test_tab VALUES (1, '2 ');
Insert into test_tab VALUES (1, '3 ');
COMMIT;

(Error: This is what I thought at the beginning.) At the beginning, I thought that as long as the transaction is written and committed with commit, the database will automatically determine whether all these statements are successfully executed, if yes, all the data will be inserted into the database. If one fails, the system will automatically roll back to the original state! Obviously, I think it is wrong.

The result after I run the preceding statement is:

[SQL] START TRANSACTION;

Affected rows: 0

Time: 0.000 s

[SQL]

Insert into test_tab VALUES (1, '2 ');

Affected rows: 1

Time: 0.001 s

[SQL]

Insert into test_tab VALUES (1, '3 ');

[Err] 1062-Duplicate entry '1' for key 'primary'

We can see that the insert into test_tab VALUES (1, '3') statement fails because of a primary key conflict, so the following commit statement is not executed.

Note that a transaction has been started and a correct insert statement has been executed, although it is not reflected in the database, however, if a commit, begin, or start transaction is executed in the connection later (a new transaction commits other uncommitted transactions in the link, which is equivalent to commit !) You will find that you have written the insert into test_tab VALUES (1, '2'); INTO the database.

Therefore, the transaction rollback is not so understandable. The correct understanding should be that if all SQL statements in the transaction are correctly executed, You need to manually submit the commit; otherwise, any execution error occurs, you need to submit a roolback by yourself. In this case, all operations will be rolled back, rather than the commit will automatically judge and roll back.

Solution 2
1. C ++ call method: (simple example)

First define a variable flag bool m_flag = true;

Execute the transaction and SQL statement as follows: (execute is a self-written function)

Execute (m_sqlCon, "begin ");

Execute (m_sqlCon, "insert into test_tab VALUES (1, '2 ')");

Execute (m_sqlCon, "insert into test_tab VALUES (1, '3 ')");

If an error occurs in any statement during execution, set m_flag to false.

In this case, you should not submit the commit statement. Instead, you should use a function to determine whether the flag is false. If the flag is false, the system will execute roolback if any of the SQL statements executed fail. Otherwise, all the statements are correct, execute commit. The following commit_transaction () method.

The code is roughly as follows, which needs to be modified if used!

Privat void execute (MYSQL m_sqlCon, string sqlStatement)
{
R = mysql_real_query (m_sqlCon, sqlStatement, (unsigned long) strlen (sqlStatement ));
If (r)
M_flag = false; // mark the error
}
Public bool commit_transaction ()
{
Int ret_error = 0;
If (! M_flag)
{
Cancel_transaction ();
Return false;
}
Else
{
If (! (Ret_error = mysql_commit (m_sqlCon )))
{
Cancel_transaction ();
Return true;
}
}
Return true;
}

2. Using Stored Procedures:

Create procedure PRO2 ()
BEGIN
DECLARE t_error INTEGER;
Declare continue handler for sqlexception set t_error = 1;

Start transaction;
Insert into test_tab VALUES (1, '2 ');
Insert into test_tab VALUES (1, '3 ');

IF t_error = 1 THEN
ROLLBACK;
ELSE
COMMIT;
End if;
END

CALL PRO2 ()

Mysql can be used to decide whether to roll back or submit the job.

This article permanently updates the link address:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.