search
HomeBackend DevelopmentPHP TutorialDesign flaws and solutions for database transaction processing in the CodeIgniter framework, codeigniter framework_PHP tutorial

Design flaws and solutions for database transaction processing in CodeIgniter framework, codeigniter framework

Cause:

In one of our online businesses, we use an older version of the CodeIgniter framework. In the DB class, there is a design flaw in the DB transaction processing part, which may not be considered a flaw. But it affected our production environment and caused a chain reaction. It has a great impact on the business and is difficult to troubleshoot. I reported this problem to Hex, the webmaster of codeigniter China, in mid-March this year. After that, I forgot about it. Until today, our online business once again thought of this problem, which caused me to investigate again. For the specific reasons, please listen to me slowly. (This problem also exists in the latest version Version 2.1.0)

Analysis:

Taking CodeIgniter framework Version 2.1.0 as an example, there is a $_trans_status attribute on line 58 of the CI_DB_driver class in systemdatabaseDB_driver.php.

Copy code The code is as follows:

//systemdatabaseDB_driver.php
var $trans_strict = TRUE;
var $_trans_depth = 0;
var $_trans_status = TRUE; // Used with transactions to determine if a rollback should occur
var $cache_on = FALSE;

At the same time, the query method of this class contains code for assigning this attribute, see lines 306 and 307 of the file

Copy code The code is as follows:

// This will trigger a rollback if transactions are being used
$this->_trans_status = FALSE;

A comment is also given here to tell us that if transaction processing is used, then this attribute will become a rollback decision condition.

In the transaction submission method trans_complete on line 520, the code is as follows:

Copy code The code is as follows:

/**
 * Complete Transaction
 *
 * @access public
 * @return bool
 */
function trans_complete()
{
if ( ! $this->trans_enabled)
{
return FALSE;
}

// When transactions are nested we only begin/commit/rollback the outermost ones
if ($this->_trans_depth > 1)
{
$this->_trans_depth -= 1;
return TRUE;
}

// The query() function will set this flag to FALSE in the event that a query failed
if ($this->_trans_status === FALSE)
{
$this->trans_rollback();

// If we are NOT running in strict mode, we will reset
// the _trans_status flag so that subsequent groups of transactions
// will be permitted.
if ($this->trans_strict === FALSE)
{
$this->_trans_status = TRUE;
}

log_message('debug', 'DB Transaction Failure');
return FALSE;
}

$this->trans_commit();
return TRUE;
}

In line 535, if the _trans_status attribute is false, then rollback will occur and false will be returned.

In our business code, due to the programmer's negligence, he did not judge whether the trans_complete() method was executed correctly and directly told the user that the operation was successful. However, in fact, the program had issued a rollback instruction to the DB and did not successfully update the DB record. . When the user performs the next operation, the program finds that the corresponding record has not been updated, reminds the user that the previous operation was not completed, and notifies the user to perform it again. Repeatedly...

The troubleshooting process is also quite interesting. At first, from the PHP code, I couldn't always determine the problem, and I didn't focus on the return of the trans_complete() method. It was not until later strace packet capture analysis that I found out that the rollback was caused by this attribute.

Copy code The code is as follows:

22:54:08.380085 write(9, "_ 22:54:08.380089 read(9, ": 22:54:08.381791 write(9, "21 22:54:08.381891 read(9, "7 22:54:08.382186 poll([{fd=9, events=POLLIN|POLLPRI}], 1, 0) = 0
22:54:08.382258 write(9, "v 22:54:08.382343 read(9, "7 22:54:08.382631 poll([{fd=9, events=POLLIN|POLLPRI}], 1, 0) = 0
22:54:08.382703 write(9, "22 22:54:08.401954 write(9, "v 22:54:08.402043 read(9, "7 22:54:08.417773 write(9, "v 22:54:08.417872 read(9, "7 22:54:08.418256 write(9, "[ 22:54:08.418363 read(9, "0 22:54:08.430212 write(9, "v 22:54:08.430314 read(9, "7 22:54:08.430698 write(9, "B 22:54:08.430814 read(9, "0 22:54:08.432130 write(9, "v 22:54:08.432231 read(9, "7 22:54:08.432602 write(9, "244 22:54:08.432743 read(9, "0 22:54:08.433517 write(9, "v 22:54:08.433620 read(9, "7 22:54:08.433954 write(9, "t 22:54:08.434041 read(9, "7 22:54:08.434914 write(9, "v 22:54:08.434999 read(9, "7 22:54:08.435342 write(9, "21 22:54:08.435430 read(9, "7 22:54:08.436923 write(9, "1


As you can see, at 22:54:08.380085, the update SQL statement command was sent, and the returned result was read at 22:54:08.380089, and an SQL execution error was obtained. The field "cfc4n_user_lock" does not exist; 22:54 At two time points: 08.381791 and 22:54:08.382703, PHP sends the instructions to stop "auto-commit" and "start transaction processing", and at 22:54:08.433954, it sends the "transaction rollback" instruction.

With the above code analysis, we can clearly know that the SQL execution error of "UPDATE `cfc4n_user_info` SET `cfc4n_user_lock` = 1 WHERE `cfc4n_user_id` = '6154′ AND `cfc4n_user_lock` = 0" resulted in $_trans_status The attribute is set to FALSE. When the code commits the transaction, it is judged by the trans_complete() method. It is believed that there is a SQL statement execution failure in the "previous transaction processing" (which will be analyzed in detail below), and it is decided to roll back the transaction without committing it.

I just mentioned "previous transaction processing". Some friends may not understand it. Let's go back to the code and continue to look at this attribute. Also in the trans_complete method, lines 542-545:




Copy code

The code is as follows:


// If we are NOT running in strict mode, we will reset
// the _trans_status flag so that subsequent groups of transactions
// will be permitted.
if ($this->trans_strict === FALSE)
{
$this->_trans_status = TRUE;
}

It can also be easily understood from the comments that the designer who set up the CI needs to handle it more rigorously. When there are multiple transactions in the same script, the relationship between the transactions is important. The trans_strict attribute here is a switch. When trans_strict is false, it is non-strict mode, which means that the relationship between multiple transactions is not important and does not affect each other. There is a SQL statement in the current transaction that fails to execute, which does not affect you. _trans_status is set to TRUE.
There is no doubt that this is a very thoughtful consideration. The relationship between multiple transactions is considered to ensure that the business runs on more rigorous code.

However, in our code, the wrong SQL statement is executed outside the transaction processing, not within the transaction. According to our understanding of transactions, we can clearly know that the SQL outside the transaction is more important than the SQL within the transaction. The SQL outside the transaction can be allowed to go wrong, but the SQL within the transaction must be Be correct and free from outside interference. But in the CI framework, because a statement other than a transaction fails to execute, it causes the entire transaction to be rolled back... Of course, our programmers did not make a judgment on the return of the transaction submission method, which is also a problem.

The problem is now very clear, so the solution must be very simple for you.
For example, in the trans_start method, assign a value to the _trans_status attribute, set it to TRUE, and ignore issues outside the transaction.

Copy code The code is as follows:

function trans_start($test_mode = FALSE)
{
if ($this->trans_strict === FALSE)
{
$this->_trans_status = TRUE; //When starting transaction processing, reset the value of this attribute to TRUE
}
//2012/05/01 18:00 After correction by CI Chinese community netizens http://codeigniter.org.cn/forums/space-uid-5721.html, the modification here is to add the trans_strict attribute to determine whether to reset_ trans_status as well.
if ( ! $this->trans_enabled)
{
return FALSE;
}

// When transactions are nested we only begin/commit/rollback the outermost ones
if ($this->_trans_depth > 0)
{
$this->_trans_depth += 1;
return;
}

$this->trans_begin($test_mode);
}

End:

You cannot blindly define the other party’s code evaluation without understanding the other party’s design intentions, regardless of the level of the program author. If you are stronger than yourself, you can't blindly worship them; if you are weaker than yourself, you can't make random accusations; it is a good habit to understand the design intentions and learn other people's excellent design ideas, coding styles, and algorithm efficiency. Of course the codeigniter framework is excellent.

For php CodeIgniter 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]

CodeIgniter China - PHP Framework CodeIgniter China Community

Click on the user manual. There is a "Table of Contents" button at the top of the page. Click to open a very good Chinese manual.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/848800.htmlTechArticleCodeIgniter framework database transaction processing design flaws and solutions, codeigniter framework origin: In one of our online businesses , using an older version of the CodeIgniter framework, where...
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
如何在CodeIgniter中实现自定义中间件如何在CodeIgniter中实现自定义中间件Jul 29, 2023 am 10:53 AM

如何在CodeIgniter中实现自定义中间件引言:在现代的Web开发中,中间件在应用程序中起着至关重要的作用。它们可以用来执行在请求到达控制器之前或之后执行一些共享的处理逻辑。CodeIgniter作为一个流行的PHP框架,也支持中间件的使用。本文将介绍如何在CodeIgniter中实现自定义中间件,并提供一个简单的代码示例。中间件概述:中间件是一种在请求

CodeIgniter中间件:加速应用程序的响应速度和页面渲染CodeIgniter中间件:加速应用程序的响应速度和页面渲染Jul 28, 2023 pm 06:51 PM

CodeIgniter中间件:加速应用程序的响应速度和页面渲染概述:随着网络应用程序的复杂性和交互性不断增长,开发人员需要使用更加高效和可扩展的解决方案来提高应用程序的性能和响应速度。CodeIgniter(CI)是一种基于PHP的轻量级框架,提供了许多有用的功能,其中之一就是中间件。中间件是在请求到达控制器之前或之后执行的一系列任务。这篇文章将介绍如何使用

在CodeIgniter框架中使用数据库查询构建器(Query Builder)的方法在CodeIgniter框架中使用数据库查询构建器(Query Builder)的方法Jul 28, 2023 pm 11:13 PM

在CodeIgniter框架中使用数据库查询构建器(QueryBuilder)的方法引言:CodeIgniter是一个轻量级的PHP框架,它提供了许多功能强大的工具和库,方便开发人员进行Web应用程序开发。其中一个令人印象深刻的功能是数据库查询构建器(QueryBuilder),它提供了一种简洁而强大的方法来构建和执行数据库查询语句。本文将介绍如何在Co

PHP开发:使用 CodeIgniter 实现 MVC 模式和 RESTful APIPHP开发:使用 CodeIgniter 实现 MVC 模式和 RESTful APIJun 16, 2023 am 08:09 AM

随着Web应用程序的不断发展,更加快速和高效地开发应用程序变得非常重要。并且,随着RESTfulAPI在Web应用程序中的广泛应用,对于开发人员来说,必须理解如何创建和实现RESTfulAPI。在本文中,我们将讨论如何使用CodeIgniter框架实现MVC模式和RESTfulAPI。MVC模式简介MVC(Model-Vie

php如何使用CodeIgniter5框架?php如何使用CodeIgniter5框架?Jun 01, 2023 am 11:21 AM

CodeIgniter是一个轻量级的PHP框架,采用MVC架构,支持快速开发和简化常见任务。CodeIgniter5是该框架的最新版本,提供了许多新的特性和改进。本文将介绍如何使用CodeIgniter5框架来构建一个简单的Web应用程序。步骤1:安装CodeIgniter5下载和安装CodeIgniter5非常简单,只需要遵循以下步骤:下载最新版本

如何使用PHP框架CodeIgniter快速搭建一个后台管理系统如何使用PHP框架CodeIgniter快速搭建一个后台管理系统Jun 27, 2023 am 09:46 AM

现今互联网时代,一款深受用户喜爱的网站必须具备简洁明了的前端界面和功能强大的后台管理系统,而PHP框架CodeIgniter则是一款能够让开发者快速搭建后台管理系统的优秀框架。CodeIgniter拥有轻量级、高效率、易扩展等特点,本文将针对初学者,详细说明如何通过该框架快速搭建一个后台管理系统。一、安装配置安装PHPCodeIgniter是一个基于PHP的

PHP实现框架:CodeIgniter入门教程PHP实现框架:CodeIgniter入门教程Jun 18, 2023 pm 10:43 PM

近年来,Web开发技术的进步和全球互联网应用的不断扩大,使得PHP技术应用面越来越广泛。作为一种快速开发的技术,其生态系统也在不断发展壮大。其中,CodeIgniter作为PHP开发领域中著名的框架之一,备受众多开发者的欢迎。本篇文章将介绍CodeIgniter框架的相关知识,以此为初学者提供一个入门的指引。一、什么是CodeIgniter框架?CodeIg

使用PHP框架CodeIgniter开发一个实时聊天应用,提供便捷的通讯服务使用PHP框架CodeIgniter开发一个实时聊天应用,提供便捷的通讯服务Jun 27, 2023 pm 02:49 PM

随着移动互联网的发展,即时通信变得越来越重要,越来越普及。对于很多企业而言,实时聊天更像是一种通信服务,提供便捷的沟通方式,可以快速有效地解决业务方面的问题。基于此,本文将介绍如何使用PHP框架CodeIgniter开发一个实时聊天应用。了解CodeIgniter框架CodeIgniter是一个轻量级的PHP框架,提供了一系列的简便的工具和库,帮助开发者快速

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)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

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.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool