【发布时间】:2014-09-18 03:30:07
【问题描述】:
我开发了一个 web 服务 (PHP/MySQL),它通过 JSON 字符串简单地输出优惠券代码。
工作原理: 应用程序接收 1 个参数(电子邮件),然后向数据库表发出请求以获取尚未分配的优惠券代码。然后请求更新此优惠券代码的行并将“1”放入指定的列。 (选择/更新例程)
之后,JSON 输出如下:
echo '{"couponCode": "'. $coupon_code . '"}';
就是这样。
问题是 web 服务在大约 1 分钟内收到 10000 个请求。这一天只发生一次。如果我查看 apache 的原始日志,我可以看到它每次都收到 10000 个请求,但在我的表中只有 984 行已更新(即:提供 984 个优惠券代码)。我多次测试它,每次都在 980 到 986 之间变化。由 web 服务创建的日志文件没有显示任何错误,并且准确地反映了数据库中已更新的内容,每次更新在 980 到 986 行之间。
我的问题是:丢失的请求是怎么回事?是服务器内存不足,无法在这么短的时间内处理如此多的请求吗? (当我使用 5000 个请求进行测试时,它工作正常)
如果有帮助,这里是获取新优惠券代码的功能:
function getNewCouponCode($email){
$stmt = $this->connector->prepare("SELECT * FROM coupon_code WHERE email = '' ORDER BY id ASC LIMIT 1");
$stmt2 = $this->connector->prepare("UPDATE coupon_code SET email = :email WHERE id = :id");
try{
$this->connector->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->connector->beginTransaction();
$this->connector->exec("LOCK TABLES coupon_code WRITE");
/*TRANSACTION 1*/
$stmt->execute();
$result["select"] = $stmt->fetch(PDO::FETCH_ASSOC);
/*TRANSACTION 1*/
/*TRANSACTION 2*/
$stmt2->bindParam(":email", $email);
$stmt2->bindParam(":id", $result["select"]["id"]);
$result["update"] = $stmt2->execute();
/*TRANSACTION 2*/
$this->connector->commit();
$this->connector->exec('UNLOCK TABLES');
return $result;
}catch(Exception $e) {
$this->connector->rollBack();
$this->connector->exec('UNLOCK TABLES');
$result["error"] = $e->getMessage();
return $result;
}
}
提前致谢。
【问题讨论】:
标签: php mysql apache web-services