因为最近在做公司的一个秒杀项目,就是现在一些购物网站最常见的那种。但是考虑到并发的一些问题(也许并发不是主要的,主要的是现在有秒杀就会出现秒杀工具之类的)导致被秒出去的商品比实际库存还要多,所以就上网看了一下mysql的锁,公司用的是innodb引擎,thinkphp3.2框架,根据网上的相关资料,应该用的是行级锁(对于mysql,本人菜鸟,懂的不是很深入)。
<code> public function test_sql(){ set_time_limit(0); $model = D('Liren/GroupPurchase'); $row = $model->lock(true)->where(array('id'=>1))->find(); if($row){ dump($row); sleep(10); } }</code>
test_sql 方法是带锁查询了一条数据,之后延时了十秒,在这同时,我开了另一个进程:
<code> public function test_lock(){ $model = D('Liren/GroupPurchase'); $info = $model->find(1); if($info){ dump($info); } }</code>
在test_sql没有结束之前test_lock一直在等待。而且就算我在test_lock方法中查询的不是主键(id)为1的数据,也同样要等到test_sql结束之后才能执行。这样的话是不是整个表都锁住了,是不是需要继承thinkphp的AdvModel才能真正实现行级锁?
还有一个问题。事务和锁之间存在关系吗?
<code>public function test_sql(){ set_time_limit(0); $model = D('Liren/GroupPurchase'); $model->startTrans(); $row = $model->lock(true)->where(array('id'=>1))->find(); // echo $model->getLastSql(); if($row){ $ret = $model->lock(true)->save(array('id'=>1,'is_show'=>0)); echo $model->getLastSql(); } if($ret){ $model->commit(); sleep(10); echo 'success'; } } </code>
虽然事务是提交了,数据库的状态也一早就改变了,但还是必须等到test_sql进程结束之后 test_lock 方法才能输出数据,如果能在事务结束的时候表锁就能结束,这样是不是好一点。
小弟愚钝,望指点!
回复内容:
因为最近在做公司的一个秒杀项目,就是现在一些购物网站最常见的那种。但是考虑到并发的一些问题(也许并发不是主要的,主要的是现在有秒杀就会出现秒杀工具之类的)导致被秒出去的商品比实际库存还要多,所以就上网看了一下mysql的锁,公司用的是innodb引擎,thinkphp3.2框架,根据网上的相关资料,应该用的是行级锁(对于mysql,本人菜鸟,懂的不是很深入)。
<code> public function test_sql(){ set_time_limit(0); $model = D('Liren/GroupPurchase'); $row = $model->lock(true)->where(array('id'=>1))->find(); if($row){ dump($row); sleep(10); } }</code>
test_sql 方法是带锁查询了一条数据,之后延时了十秒,在这同时,我开了另一个进程:
<code> public function test_lock(){ $model = D('Liren/GroupPurchase'); $info = $model->find(1); if($info){ dump($info); } }</code>
在test_sql没有结束之前test_lock一直在等待。而且就算我在test_lock方法中查询的不是主键(id)为1的数据,也同样要等到test_sql结束之后才能执行。这样的话是不是整个表都锁住了,是不是需要继承thinkphp的AdvModel才能真正实现行级锁?
还有一个问题。事务和锁之间存在关系吗?
<code>public function test_sql(){ set_time_limit(0); $model = D('Liren/GroupPurchase'); $model->startTrans(); $row = $model->lock(true)->where(array('id'=>1))->find(); // echo $model->getLastSql(); if($row){ $ret = $model->lock(true)->save(array('id'=>1,'is_show'=>0)); echo $model->getLastSql(); } if($ret){ $model->commit(); sleep(10); echo 'success'; } } </code>
虽然事务是提交了,数据库的状态也一早就改变了,但还是必须等到test_sql进程结束之后 test_lock 方法才能输出数据,如果能在事务结束的时候表锁就能结束,这样是不是好一点。
小弟愚钝,望指点!
既然是秒杀功能为什么还要用MySQL呢?为什么不考虑redis等内存缓存数据库呢?
毕竟内存的IO效率和磁盘的IO效率之间大概相差了中美之间经济实力那么多吧
那么谈谈锁的问题:
相比题主现在对概念应该还有些模糊,我先明确概念:
读锁->共享锁 (S)
写锁 -> 排它锁 (X)
兼容性:
<code> X S X 不兼容 不兼容 S 不兼容 兼容 </code>
还有一种叫乐观锁/悲观锁
这份回答很好,我直接拿来了,总结来说就是:
△乐观锁是通过逻辑实现,本质上并没有给数据库加锁
悲观锁是通过真实的数据库锁机制来完成的。
最后回到你的问题:
test_sql()函数: 首先要确认,在对表获取行锁的时候,要尽量的使用索引检索纪录,如果没有使用索引访问,那么即便你只是要更新其中的一行纪录,也是全表锁定的。要确保sql是使用索引来访问纪录的,必要的时候,请使用explain检查sql的执行计划,判断是否按照预期使用了索引。
由于mysql的行锁是针对索引加的锁,不是针对纪录加的锁,所以虽然是访问不同行的纪录,但是如果是相同的索引键,是会被加锁的。事务和锁没什么关系,锁的机制与存储引擎才有关
1.innodb 引擎支持行锁,但是不指定唯一索引键就会锁表
test_sql 中,使用了悲观锁,也就是 select where for update
当 where 条件指定了唯一索引键时---行锁
当非唯一索引键时---表锁
2.在事务中
select 操作共享锁
update,delete,insert 排它锁
commit 会把锁给取消
InnoDB引擎下,执行的SQL使用行级锁,还是全表锁。跟mysql采用的隔离级别、sql会使用到的索引、mysql自身针对这个sql的执行优化都有关系。
所以怎么理解mysql的锁,需要根据你在用的mysql的具体配置有关。有一篇博客讲的特别透彻,希望对你有用。http://blog.sae.sina.com.cn/archives/2127

PHPidentifiesauser'ssessionusingsessioncookiesandsessionIDs.1)Whensession_start()iscalled,PHPgeneratesauniquesessionIDstoredinacookienamedPHPSESSIDontheuser'sbrowser.2)ThisIDallowsPHPtoretrievesessiondatafromtheserver.

The security of PHP sessions can be achieved through the following measures: 1. Use session_regenerate_id() to regenerate the session ID when the user logs in or is an important operation. 2. Encrypt the transmission session ID through the HTTPS protocol. 3. Use session_save_path() to specify the secure directory to store session data and set permissions correctly.

PHPsessionfilesarestoredinthedirectoryspecifiedbysession.save_path,typically/tmponUnix-likesystemsorC:\Windows\TemponWindows.Tocustomizethis:1)Usesession_save_path()tosetacustomdirectory,ensuringit'swritable;2)Verifythecustomdirectoryexistsandiswrita

ToretrievedatafromaPHPsession,startthesessionwithsession_start()andaccessvariablesinthe$_SESSIONarray.Forexample:1)Startthesession:session_start().2)Retrievedata:$username=$_SESSION['username'];echo"Welcome,".$username;.Sessionsareserver-si

The steps to build an efficient shopping cart system using sessions include: 1) Understand the definition and function of the session. The session is a server-side storage mechanism used to maintain user status across requests; 2) Implement basic session management, such as adding products to the shopping cart; 3) Expand to advanced usage, supporting product quantity management and deletion; 4) Optimize performance and security, by persisting session data and using secure session identifiers.

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 English version
Recommended: Win version, supports code prompts!

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.
