Matters needing attention when operating transactions in mysql
Operating transactions is a very common process, and whenever there are interdependencies on database modifications, transactions should be used to resolve them. Pay attention to the following points when operating transactions:
1.Try to use Exception to catch the exception of MYSQL.
So the first step: To set the exception mode of PDO to PDO::ERRMODE_EXCEPTION, PDO::ATTR_ERRMODE, there are the following three values:
PDO::ERRMODE_SILENT: Default mode, do not actively report errors, need Take the initiative to obtain error information in the form of $pdo->errorInfo().
PDO::ERRMODE_WARNING: E_WARNING error is raised, and the error is actively reported.
PDO::ERRMODE_EXCEPTION: exceptions are actively thrown, and error information needs to be output with try{}cath(){}.
We use the following when connecting to MYSQL:
Write the above code in try catch to catch the exception.Note that PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION must be written in new pdo. Otherwise, the mysql connection error information will not be captured. The class in catch is limited to PDOException.If there is a namespace, please add the top-level namespace in front, otherwise the exception will not be caught.
2.Turn off AUTOCOMMIT before the MYSQL transaction starts
Note that beginTransaction has a return value.It is best to judge the return value when calling the transaction beginTransaction.If it returns false, then there is no need to roll back later.
3.Operation transaction and rollback
Remember to call the rollBack method before all returns or before ending the transaction.The rollBack method also has a return value.It is best to judge the return value and retry the rollback twice to prevent an exception from a rollback and cause the transaction to be locked..
4.Execute automatic submission
This step is very critical.You must perform this step whether you execute commit or rollBack.The meaning of automatic submission is whether each SQL statement you execute directly executes MYSQL.The default MYSQL is automatically submitted, that is, if you execute a SQL statement, it Just change to the database.If MYSQL is not submitted automatically, all MYSQL statements you execute are stuck there waiting to be executed.
The most common phenomenon when the transaction is abnormal is that the front-end browser requests that involve locked tables or rows are waiting, and finally may report an error mysql innodb Lock wait timeout exceeded; try restarting transaction problem, at this time:
First: You can query the error message through SHOW ENGINE INNODB STATUS\G;.The amount of information is too large and difficult to troubleshoot.
Second: Use select * from information_schema.innodb_trx \G; to query the list of running tasks.
Third, you can check the mysql bin log: MYSQL log file: show variables like general_log_file;
Find the log file, directly view the binlog file, there may be garbled characters.
But the above command can only decode and display the existing log files, without distinguishing the databases inside.If the log files are large, stop cooking. The following command can specify the database start time:
But the above commands are not good enough, the following one is the best. The function of simulating tail -f can be realized. And can specify the database, very easy to use.
0 Comments