Codeigniter框架的更新事务(transaction)BUG及解决方法,codeigniter框架
Codeigniter框架的更新事务(transaction)BUG及解决方法,codeigniter框架
由于ci事务判断出错回滚的条件是语句是否执行成功,而更新操作时,就算影响的条数为0,sql语句执行的结果过仍然为1,因为它执行成功了,只是影响的条数为0。
下面介绍解决这个问题的方法:
对于一次要执行许多的语句的事务
只需在更新操作下根据影响条数是否为0来决定是否会滚即可,下面假设第二条语句为更新操作。
复制代码 代码如下:
//采用 Codeigniter 事务的手动模式
$this->db->trans_strict(FALSE);
$this->db->trans_begin();
$this->db->query('SELECT ...');//SELECT 操作无需特殊处理
$this->db->query('INSERT ...');//INSERT 出错会有 Codeigniter 自动处理
$this->db->query('UPDATE ...');
if (!$this->db->affacted_rows()) {//上面的 UPDATE 失败则回滚
$this->db->trans_rollback();
//@todo 异常处理部分
exit();//需要终止或跳出,以免下面的 SQL 代码继续执行!
}
$this->db->query('DELETE ...');
if (!$this->db->affacted_rows()) {//上面的 DELETE 失败则回滚
$this->db->trans_rollback();
//@todo 异常处理部分
exit();//需要终止或跳出,以免下面的 SQL 代码继续执行!
}
$this->db->query('SELECT ...');//SELECT 操作无需特殊处理
$this->db->query('INSERT ...');//INSERT 出错会有 Codeigniter 自动处理
if ($this->db->trans_status() === TRUE) {
$this->db->trans_commit();
} else {
$this->db->trans_rollback();
//@todo 异常处理部分
}
如果一次执行的语句不是很多,可以在最后做一下判断来决定回滚
若语句中没有更新操作,用自动事务就可以。
并发数和服务器架构有主要关系,但是程序的优化也有很大关系,比如缓存应用得怎么样什么的。当然和框架没有太大关系
昨天刚看了CodeIgniter手册,在CodeIgniter URLs这一章有说明方法的:
就是通过在.htaccess文件中添加规则来处理,如下
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











As Apple's WWDC conference 2024 came to a successful conclusion, not only macos15 was announced, but the update of Apple's new iOS18 system attracted the most attention. Although there are many new features, as the first version of Apple's iOS18, people inevitably wonder whether it is necessary to upgrade Apple. iOS18, what kind of bugs are there in the latest release of Apple iOS18? After real use evaluation, the following is a summary of Apple iOS18 bugs, let’s take a look. Currently, many iPhone users are rushing to upgrade to iOS18. However, various system bugs are making people uncomfortable. Some bloggers said that you should be cautious when upgrading to iOS18 because "there are so many bugs." The blogger said that if your iPhone is

What do game bugs mean? During the process of playing games, we often encounter some unexpected errors or problems, such as characters getting stuck, tasks being unable to continue, screen flickering, etc. These abnormal phenomena are called game bugs, that is, faults or errors in the game. In this article, we'll explore what game bugs mean and the impact they have on players and developers. Game bugs refer to errors that occur during the development or operation of the game, causing the game to fail to run normally or to behave unexpectedly. These errors may be due to

How to implement custom middleware in CodeIgniter Introduction: In modern web development, middleware plays a vital role in applications. They can be used to perform some shared processing logic before or after the request reaches the controller. CodeIgniter, as a popular PHP framework, also supports the use of middleware. This article will introduce how to implement custom middleware in CodeIgniter and provide a simple code example. Middleware overview: Middleware is a kind of request

Lockwaittimeoutexceeded;tryrestartingtransaction - How to solve the MySQL error: transaction wait timeout. When using the MySQL database, you may sometimes encounter a common error: Lockwaittimeoutexceeded;tryrestartingtransaction. This error indicates that the transaction wait timeout. This error usually occurs when

1. Introduction to PDO PDO is an extension library of PHP, which provides an object-oriented way to operate the database. PDO supports a variety of databases, including Mysql, postgresql, oracle, SQLServer, etc. PDO enables developers to use a unified API to operate different databases, which allows developers to easily switch between different databases. 2. PDO connects to the database. To use PDO to connect to the database, you first need to create a PDO object. The constructor of the PDO object receives three parameters: database type, host name, database username and password. For example, the following code creates an object that connects to a mysql database: $dsn="mysq

CodeIgniter Middleware: Accelerating Application Responsiveness and Page Rendering Overview: As web applications continue to grow in complexity and interactivity, developers need to use more efficient and scalable solutions to improve application performance and responsiveness. . CodeIgniter (CI) is a lightweight PHP-based framework that provides many useful features, one of which is middleware. Middleware is a series of tasks that are performed before or after the request reaches the controller. This article will introduce how to use

Introduction to the method of using the database query builder (QueryBuilder) in the CodeIgniter framework: CodeIgniter is a lightweight PHP framework that provides many powerful tools and libraries to facilitate developers in web application development. One of the most impressive features is the database query builder (QueryBuilder), which provides a concise and powerful way to build and execute database query statements. This article will introduce how to use Co

MySQL transaction processing: the difference between automatic submission and manual submission. In the MySQL database, a transaction is a set of SQL statements. Either all executions are successful or all executions fail, ensuring the consistency and integrity of the data. In MySQL, transactions can be divided into automatic submission and manual submission. The difference lies in the timing of transaction submission and the scope of control over the transaction. The following will introduce the difference between automatic submission and manual submission in detail, and give specific code examples to illustrate. 1. Automatically submit in MySQL, if it is not displayed
