Codeigniter framework update transaction (transaction) BUG and solution, codeigniter framework
Since the CI transaction judgment error rollback condition is whether the statement is executed successfully, and during the update operation, even if the number of items affected is 0, the result of the SQL statement execution is still 1, because it was executed successfully, and only the affected items are 1. The number of items is 0.
Here’s how to solve this problem:
For transactions that need to execute many statements at one time
You only need to decide whether to roll based on whether the number of affected items is 0 during the update operation. It is assumed below that the second statement is an update operation.
Copy code The code is as follows:
//Adopt manual mode of Codeigniter transaction
$this->db->trans_strict(FALSE);
$this->db->trans_begin();
$this->db->query('SELECT ...');//SELECT operation requires no special processing
$this->db->query('INSERT ...');//If an INSERT error occurs, Codeigniter will automatically handle it
$this->db->query('UPDATE ...');
If (!$this->db->affacted_rows()) {//If the above UPDATE fails, rollback
$this->db->trans_rollback();
//@todo exception handling part
exit();//Need to terminate or exit to prevent the following SQL code from continuing to execute!
}
$this->db->query('DELETE ...');
If (!$this->db->affacted_rows()) {//If the above DELETE fails, rollback
$this->db->trans_rollback();
//@todo exception handling part
Exit();//Need to terminate or exit to prevent the following SQL code from continuing to execute!
}
$this->db->query('SELECT ...');//SELECT operation requires no special processing
$this->db->query('INSERT ...');//If an INSERT error occurs, Codeigniter will automatically handle it
If ($this->db->trans_status() === TRUE) {
$this->db->trans_commit();
} else {
$this->db->trans_rollback();
//@todo exception handling part
}
If there are not many statements executed at one time, you can make a judgment at the end to decide to roll back
If there is no update operation in the statement, automatic transactions can be used.
The number of concurrency is mainly related to the server architecture, but the optimization of the program also has a lot to do with it, such as how well the cache is used. Of course it has nothing to do with the framework
I just read the CodeIgniter manual yesterday, and the method is explained in the CodeIgniter URLs chapter:
It is processed by adding rules in the .htaccess file, as follows
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
http://www.bkjia.com/PHPjc/848801.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/848801.htmlTechArticleCodeigniter framework update transaction (transaction) BUG and solution, codeigniter framework due to ci transaction judgment error rollback conditions It is whether the statement is executed successfully, and when updating, it...