用ajax get 方法模拟 1000个并发(同时)请求,后端是一个计数器+1,
但是1000个并发这么会导致计数器只加1而不是加1000,加1000的程序这么写呢
testsql.php
==========<?php
require 'db.php';// $sql="Begin";
// $result=mysql_query($sql);// $sql="SELECT * FROM adplan WHERE id=1 for update";
// $result=mysql_query($sql);$sql="Begin;SELECT * FROM adplan WHERE id=1 for update;update adplan set show_limit=show_limit+1 where id=1;Commit";
$result=mysql_query($sql);// $sql="Commit";
// $result=mysql_query($sql);
//$sql="Begin;";
if($result){
echo ('ok');
}
else{
echo('fail');
}?>testsql.html
==========
<!DOCTYPE html>
<html lang="cn">
<head>
<meta charset="utf-8">
<title>111111</title>
<script src="http://cdn.bootcss.com/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$('#bt').click(function(){
for(var i=0; i<100; i++){
get();
}
});
var get =function(){
$.get("testsql.php", function(result){
console.log('a:'+result);
});
}
});
</script>
</head>
<body>
<button id="bt" type="button">start</button>
</body>
</html>
但是1000个并发这么会导致计数器只加1而不是加1000,加1000的程序这么写呢
testsql.php
==========<?php
require 'db.php';// $sql="Begin";
// $result=mysql_query($sql);// $sql="SELECT * FROM adplan WHERE id=1 for update";
// $result=mysql_query($sql);$sql="Begin;SELECT * FROM adplan WHERE id=1 for update;update adplan set show_limit=show_limit+1 where id=1;Commit";
$result=mysql_query($sql);// $sql="Commit";
// $result=mysql_query($sql);
//$sql="Begin;";
if($result){
echo ('ok');
}
else{
echo('fail');
}?>testsql.html
==========
<!DOCTYPE html>
<html lang="cn">
<head>
<meta charset="utf-8">
<title>111111</title>
<script src="http://cdn.bootcss.com/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$('#bt').click(function(){
for(var i=0; i<100; i++){
get();
}
});
var get =function(){
$.get("testsql.php", function(result){
console.log('a:'+result);
});
}
});
</script>
</head>
<body>
<button id="bt" type="button">start</button>
</body>
</html>
解决方案 »
- 这是淘宝转动的实时搜索的静态代码
- 请教下apache安装。。
- 兄弟们,请教大家一个二级域名指向的问题!
- 求助:多语言CMS系统推荐
- parser error "Class name excepted afer keyword 'class'"
- 请教关于 winxp+apache不能执行php文件问题
- php生成excel问题
- ecshop后台添加商品相册的时候bmp格式的无法生成缩略图
- 现在有没有像淘宝一样的手机商城,移动端的,店铺能入驻,就是有些像大众点评的本地化商城的系统啊
- thinkphp5 include 方法那样引入 A.PHP引入B.PHP方法
- PHP导出数据到CSV 如何排版
- 新人求助
能这么写么?
你直接写mysql的update语句试试,不要用事务,让mysql自己解决锁问题。