当前位置:网站首页>Transaction processing in PDO
Transaction processing in PDO
2022-06-13 03:14:00 【wks19891215】
The benefits of using transactions :
for instance : Bank users A To the user B Transfer accounts 100 element , This operation is divided into two steps :
(1)A Your account balance has been deducted 100.
(2)B The account balance of has increased 100.
If you don't use transactions , hypothesis (1) Execution succeeded , and (2) failed , The user B No receivables have been received , And users A The loss was in vain .
After using transactions , Regardless of (1) perhaps (2) Which step failed , All can be rolled back , That is, the account balance of both parties is restored to the previous state .
Not all databases provide transaction support , Such as mysql Medium Myisam The engine does not support transactions , But the new version defaults to innoDB The engine provides support for transactions .(RDBMS Most support transactions ,NoSQL Databases generally do not support transactions )
Let's take a look at the specific code :
<?php
$dsn = "mysql:dbname=pdo;host=localhost";
$user = "root";
$password = "root";
$dbh = new PDO($dsn, $user, $password);
// Before using transactions , Turn off auto submit first . If you don't close it , When an exception occurs, it cannot be rolled back .
// According to the manual description ,ATTR_AUTOCOMMIT Property only in mysql,OCI(oracle),firebird Available in three databases
$dbh->setAttribute(PDO::ATTR_AUTOCOMMIT, 0);
$cash = 100;
try {
$dbh->beginTransaction();
// user A Account deduction 100
$sqlcmd = "update transaction set useraccount=useraccount - {$cash} where username ='A'";
$affected_rows = $dbh->exec($sqlcmd);
if ($affected_rows > 0) {
echo " user A Account deduction succeeded " . "<br>";
} else {
throw new Exception(" user A Account deduction failed ");
}
// user B Account increase 100
$affected_rows = $dbh->exec("update transaction set useraccount=useraccount+{$cash} where username ='B'");
if ($affected_rows > 0) {
echo " user B Account increase succeeded " . "<br>";
} else {
throw new Exception(" user B Account addition failed ");
}
echo " Transfer succeeded ";
// If the previous two steps are successful , Then commit the transaction
$dbh->commit();
}
catch (PDOException $e) // If an exception occurs in the previous two steps , Then roll back
{
echo $e->getMessage();
$dbh->rollback();
}
// After the use of things , Turn auto submit back on
$dbh->setAttribute(PDO::ATTR_AUTOCOMMIT, 1);
?>
边栏推荐
- 二叉樹初始化代碼
- [JVM Series 5] performance testing tool
- Ijkplayer source code ---packetqueue
- Hash table: the time complexity of insert, delete and random access is O (1)
- Mvcc and bufferpool (VI)
- Add Yum source to install php74
- C simple understanding - arrays and sets
- Ijkplayer source code - remuxing
- Six special GPU products for domestic aircraft passed the appraisal and review
- 2022 qianle micro cloud technology learning task plan
猜你喜欢

Available types in C #_ Unavailable type_ C double question mark_ C question mark point_ C null is not equal to

技术博客,经验分享宝典
![Loading process of [JVM series 3] classes](/img/a7/707c5cb95de71d95bf6ad9b2f69afa.jpg)
Loading process of [JVM series 3] classes

Prometheus node_ Exporter installs and registers as a service

Six special GPU products for domestic aircraft passed the appraisal and review

Prometheus node_exporter安装并注册为服务

Open source - campus forum and resource sharing applet

Introduction to redis (using redis, common commands, persistence methods, and cluster operations)
![Data Governance Series 1: data governance framework [interpretation and analysis]](/img/d9/1476d0ee2c82f5cdd70b4cffaba423.jpg)
Data Governance Series 1: data governance framework [interpretation and analysis]

AAR packaging and confusion
随机推荐
IOS interview · full bat interview record of an IOS programmer (including the true interview questions of Baidu + Netease + Alibaba)
Using linked list to find set union
2019 - sorting out the latest and most comprehensive IOS test questions (including framework and algorithm questions)
Delete the number of a range in the linked list
Entity framework extends the actual combat, small project reconfiguration, no trouble
Review notes of RS data communication foundation STP
[JVM Series 7] garbage collector
The extra money we made in those years
Flutter reports an error type 'Int' is not a subtype of type 'string' wonderful experience
Summary of the latest IOS interview questions in June 2020 (answers)
Ijkplayer source code -- mnatemediaplayer of ijkmediaplayer
Level II C test skills
HEAP[xxx.exe]: Invalid address specified to RtlValidateHeap( 0xxxxxx, 0x000xx)
Keil removes annoying st link update tips
JMeter quick start
Linked list: adding numbers in the linked list
产品需求文档如何编写
Exercise 8-3 rotate array right
Mongodb index -index
JS deconstruction assignment