如何在MySQL中为测试目的造成死锁 [英] How can I Cause a Deadlock in MySQL for Testing Purposes

查看:215
本文介绍了如何在MySQL中为测试目的造成死锁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让我的Python库使用MySQLdb能够检测死锁,然后重试。我相信我已经编码了一个好的解决方案,现在我想测试它。

I want to make my Python library working with MySQLdb be able to detect deadlocks and try again. I believe I've coded a good solution, and now I want to test it.

任何想法最简单的查询我可以运行使用MySQLdb创建死锁条件

Any ideas for the simplest queries I could run using MySQLdb to create a deadlock condition would be?

系统信息:



  • 客户端5.1.11

  • Windows XP

  • Python 2.4 / MySQLdb 1.2.1 p2

推荐答案

下面是我在PHP中如何做的一些伪代码:

Here's some pseudocode for how i do it in PHP:

脚本1:

START TRANSACTION;
INSERT INTO table <anything you want>;
SLEEP(5);
UPDATE table SET field = 'foo';
COMMIT;

脚本2:

START TRANSACTION;
UPDATE table SET field = 'foo';
SLEEP(5);
INSERT INTO table <anything you want>;
COMMIT;

执行脚本1,然后立即在另一个终端执行脚本2。如果数据库表中已经有一些数据,那么你会得到一个死锁(换句话说,在你第二次尝试这个数据库之后,它会开始死锁)。

Execute script 1 and then immediately execute script 2 in another terminal. You'll get a deadlock if the database table already has some data in it (In other words, it starts deadlocking after the second time you try this).

如果mysql不会遵守SLEEP()命令,那么在应用程序本身中使用Python的等效项。

Note that if mysql won't honor the SLEEP() command, use Python's equivalent in the application itself.

这篇关于如何在MySQL中为测试目的造成死锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆