我正在测试从 python/django 锁定我的 MySQL 数据库。
我有一张桌子,我正在测试:
CREATE TABLE `test` (
`id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into test (id) values (1), (2);
commit;
我有 3 个会话: - 2 个 mysql 控制台 - 1 个 django 视图
控制台 1:
mysql> begin; select t.id from test as where id = 1 t FOR UPDATE;
Query OK, 0 rows affected (0.00 sec)
+----+
| id |
+----+
| 1 |
+----+
1 row in set (0.00 sec)
控制台 2:
mysql> set @@session.innodb_lock_wait_timeout = 1;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> select t.id from test as t FOR UPDATE;
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
mysql> select t.id from test as t where id = 1 FOR UPDATE;
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
Django视图:
@db.transaction.commit_manually
def Test(request):
c = db.connection.cursor()
c.execute('set @@session.innodb_lock_wait_timeout = 1')
c.execute('select t.id from test as t FOR UPDATE')
logging.error(c.fetchall())
c.execute('select t.id from test as t where id = 1 FOR UPDATE')
logging.error(c.fetchall())
db.transaction.rollback()
return render_to_response(
'test.html',
context_instance=template.RequestContext(request, {})
)
调用此视图后,我预计会出现异常,但没有任何反应,它返回一个空结果集。
任何想法为什么?
版本:
- 蟒蛇:2.7.3
- 姜戈:1.4.3
- MySQL:5.5
谢谢,丹尼尔