未加星标

Using MySQL Deadlocks To Avoid Overselling

字体大小 | |
[数据库(mysql) 所属分类 数据库(mysql) | 发布者 店小二05 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

When developing an e-commerce application, unless you work at United Airlines , you generally want to avoid overselling.

Instead of punching your customers in the face why not use mysql Deadlocks? (Turns out this is a feature not a bug!)

First attempt, creating deadlocks

MySQL has 4 transaction isolation levels : SERIALIZABLE, REPEATABLE READ, READ UNCOMMITTED, READ COMMITTED .

In the following proof of concept, where we have 50 of the same product in stock, and we run seige to represent concurrent customers buying the same product at the same time, we expect 50 “Success!” messages in our log files.

When we use any of REPEATABLE READ, READ UNCOMMITTED, or READ COMMITTED we oversell. (boo!)

When we use SERIALIZABLE we do not oversell (yay!) but some users get deadlock errors while others do not. (SQLSTATE[40001]: Serialization failure: 1213 Deadlock found when trying to get lock; try restarting transaction) <php
error_reporting(E_ALL | E_STRICT); // Development
/*
SQL:
CREATE DATABASE `deadlocktest` COLLATE 'utf8_general_ci';
CREATE TABLE `products` ( `id` int NOT NULL AUTO_INCREMENT PRIMARY KEY, `inventory` int NOT NULL );
INSERT INTO `products` (`id`, `inventory`) VALUES ('123', '50');
USEFUL linux COMMANDS:
$ rm log.txt; touch log.txt; chmod 777 log.txt
$ seige http://host/file.php
*/
// ------------------------------------------------------------------
// Config
// ------------------------------------------------------------------
$mysqlIsolation = 'SERIALIZABLE'; // ( SERIALIZABLE, REPEATABLE READ, READ UNCOMMITTED, READ COMMITTED )
$productId = 123;
$logFile = __DIR__ . '/log.txt';
$host = '127.0.0.1';
$db = 'deadlocktest';
$user = 'root';
$pass = '';
$charset = 'utf8';
$opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
// ------------------------------------------------------------------
// Functions
// ------------------------------------------------------------------
/**
* Simulate time it takes to call the payment gateway and do stuff
*/
function doPaymentGatewayStuff()
{
usleep(500000); // Wait for 0.5 seconds
}
/**
* Simulate buying a product from our inventory
*
* @param PDO $pdo
* @param int $productId
* @return int
* @throws Exception
*/
function buyProduct(PDO $pdo, $productId)
{
$pdo->beginTransaction();
$selectStmt = $pdo->prepare('SELECT inventory FROM products WHERE id = :id ');
$selectStmt->execute(['id' => $productId]);
$res = $selectStmt->fetch();
if ($res['inventory'] <= 0) {
throw new Exception("Oh no! Sorry we're out inventory!");
}
$newInventory = $res['inventory'] - 1;
$updateStmt = $pdo->prepare('UPDATE products SET inventory = :inventory WHERE id = :id ');
$updateStmt->execute(['inventory' => $newInventory, 'id' => $productId]);
doPaymentGatewayStuff();
$pdo->commit();
return $newInventory;
}
// ------------------------------------------------------------------
// Procedure
// ------------------------------------------------------------------
$uniqueUser = uniqid();
try {
// Set up DB driver
$pdo = new PDO("mysql:host={$host};dbname={$db};charset={$charset}", $user, $pass, $opt);
$pdo->query("SET TRANSACTION ISOLATION LEVEL {$mysqlIsolation} ");
// Simulate buying a product and decreasing inventory
$newInventory = buyProduct($pdo, $productId);
// No exceptions were thrown, we consider this successful
$successMsg = "{$uniqueUser} - Success! Product {$productId} inventory has been decreased to {$newInventory}" . PHP_EOL;
file_put_contents($logFile, $successMsg, FILE_APPEND);
echo "$successMsg";
}
catch (Exception $e) {
if (isset($pdo) && $pdo->inTransaction()) {
$pdo->rollBack();
}
$errorMsg = "{$uniqueUser} - Error! " . $e->getMessage() . PHP_EOL;
file_put_contents($logFile, $errorMsg, FILE_APPEND);
echo "$errorMsg";
} Second attempt, handling deadlocks

The above code has good intentions but many users get the dreaded deadlock message.

Turns out deadlocks are OK! You just have to handle them somehow.

Here’s a fixed proof of concept:

<?php
// ------------------------------------------------------------------
// Config
// ------------------------------------------------------------------
$mysqlIsolation = 'SERIALIZABLE';
$productId = 123;
$logFile = __DIR__ . '/log.txt';
$host = '127.0.0.1';
$db = 'deadlocktest';
$user = 'root';
$pass = '';
$charset = 'utf8';
$opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
// ------------------------------------------------------------------
// Functions
// ------------------------------------------------------------------
/**
* Check if $e is of type MySQL deadlock
*
* @param PDO $pdo
* @param mixed $e
* @return bool
*/
function isDeadlock(PDO $pdo, $e)
{
return (
$e instanceof PDOException &&
$pdo->getAttribute(PDO::ATTR_DRIVER_NAME) == 'mysql' &&
$e->errorInfo[0] == 40001 &&
$e->errorInfo[1] == 1213
);
}
/**
* Simulate time it takes to call the payment gateway and do stuff
*/
function doPaymentGatewayStuff()
{
usleep(500000); // Wait for 0.5 seconds
}
/**
* Simulate buying a product from our inventory
*
* @param PDO $pdo
* @param int $productId
* @return int
* @throws Exception
*/
function buyProduct(PDO $pdo, $productId)
{
$pdo->beginTransaction();
$selectStmt = $pdo->prepare('SELECT inventory FROM products WHERE id = :id ');
$selectStmt->execute(['id' => $productId]);
$res = $selectStmt->fetch();
if ($res['inventory'] <= 0) {
throw new Exception("Oh no! Sorry we're out inventory!");
}
$newInventory = $res['inventory'] - 1;
$updateStmt = $pdo->prepare('UPDATE products SET inventory = :inventory WHERE id = :id ');
$updateStmt->execute(['inventory' => $newInventory, 'id' => $productId]);
doPaymentGatewayStuff();
$pdo->commit();
return $newInventory;
}
// ------------------------------------------------------------------
// Procedure
// ------------------------------------------------------------------
$uniqueUser = uniqid();
$retry = true;
while ($retry)
{
try {
// Set up DB driver
$pdo = new PDO("mysql:host={$host};dbname={$db};charset={$charset}", $user, $pass, $opt);
$pdo->query("SET TRANSACTION ISOLATION LEVEL {$mysqlIsolation} ");
// Simulate buying a product and decreasing inventory
$newInventory = buyProduct($pdo, $productId);
// No exceptions were thrown, we consider this successful
$successMsg = "{$uniqueUser} - Success! Product {$productId} inventory has been decreased to {$newInventory}" . PHP_EOL;
file_put_contents($logFile, $successMsg, FILE_APPEND);
echo "$successMsg";
$retry = false;
}
catch (Exception $e) {
if (isset($pdo) && isDeadlock($pdo, $e)) {$retry = true;
} else {$retry = false;if (isset($pdo) && $pdo->inTransaction()) { $pdo->rollBack();}$errorMsg = "{$uniqueUser} - Error! " . $e->getMessage() . PHP_EOL;file_put_contents($logFile, $errorMsg, FILE_APPEND);echo "$errorMsg";
}
}
}

Huge gaping caveat:With 15 concurrent users the 15th user would be waiting in their browser for quite a long time. Patches welcome.

本文数据库(mysql)相关术语:navicat for mysql mysql workbench mysql数据库 mysql 存储过程 mysql安装图解 mysql教程 mysql 管理工具

主题: SQLMySQLRIAPHPTIAUUTCTIAUTFU
分页:12
转载请注明
本文标题:Using MySQL Deadlocks To Avoid Overselling
本站链接:http://www.codesec.net/view/558455.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 数据库(mysql) | 评论(0) | 阅读(32)