我无法锁定我的MySQL表

时间:2011-10-09 20:22:09

标签: mysql sql database transactions

我有一个(对我来说)奇怪的问题。我尝试为我正在尝试的事务锁定一组表。但出于某种原因,我的桌子中至少有一张不会锁定。

我的代码看起来像这样,请记住它只是我目前遇到问题的锁,但所有评论都表示赞赏:)

出于某种原因ba_flight没有被锁定或者这是我最常见的问题,但是如果我只是打电话: LOCK TABLES ba_flight它完全没问题。

-- SESSION A


-- Part 1 ba_flight

-- UNLOCK TABLES;
START TRANSACTION;

LOCK TABLES ba_booking WRITE,
            ba_paid_booking WRITE,
            ba_passenger WRITE,
            ba_contact WRITE,
            ba_weekday_factor READ,
            ba_plane READ,
            ba_flight READ,
            ba_flight AS ba_f READ,
            ba_weekly_schedule AS ba_ws READ,
            ba_weekly_schedule READ;

-- ba_flight does not get locked


-- 1: Create the booking.
CALL new_booking(1, 6);

SHOW ERRORS;

COMMIT;

SET @bid = get_uncomplete_booking();

SELECT id
  FROM ba_booking
 WHERE contact_id IS NULL;

SELECT @bid AS "BID";

INSERT INTO ba_passenger(booking_id,
                         ssn,
                         fname,
                         lname)
VALUES (@bid,
        12341234,
        'Göran',
        'Greenleaf');

INSERT INTO ba_passenger(booking_id,
                         ssn,
                         fname,
                         lname)
VALUES (@bid,
        12351235,
        'Adam',
        'Jönsson');

INSERT INTO ba_passenger(booking_id,
                         ssn,
                         fname,
                         lname)
VALUES (@bid,
        12361236,
        'Niklas',
        'of Gondor');

INSERT INTO ba_passenger(booking_id,
                         ssn,
                         fname,
                         lname)
VALUES (@bid,
        12371237,
        'Erik',
        'Grey');

INSERT INTO ba_passenger(booking_id,
                         ssn,
                         fname,
                         lname)
VALUES (@bid,
        12381238,
        'Johan',
        'Baggins');

INSERT INTO ba_passenger(booking_id,
                         ssn,
                         fname,
                         lname)
VALUES (@bid,
        12381238,
        'Elof',
        'Baggins');

-- IF THIS IS FALSE THEN WE HAVE SOMETHING STRANGE GOING ON
-- WITH OUR BOOKING AND SHOULD ROLLBACK.

SELECT check_booked_passengers(@bid);

CALL new_contact(@bid,
                 'Göran',
                 'Greenleaf',
                 'haldir@gmail.com',
                 '+9973565677');

-- RETURNS TRUE IF BOOKING IS CORRECT

SELECT is_correct_booking(@bid);

COMMIT;
UNLOCK TABLES;

-- Part 3

-- START TRANSACTION;

LOCK TABLES ba_booking WRITE,
            ba_paid_booking WRITE,            
            ba_passenger WRITE,
            ba_flight WRITE,
            ba_flight AS ba_f WRITE,
            ba_contact WRITE,
            ba_weekday_factor WRITE,
            ba_weekly_schedule READ,
            ba_weekly_schedule AS ba_ws READ,
            ba_plane READ;

CALL pay_booking(@bid, 987654331);

COMMIT;
UNLOCK TABLES;

这是学校的一个学校,所以你知道,我真的很想知道为什么我有这个问题。

1 个答案:

答案 0 :(得分:1)

来自mysql documentation关于锁定:

如果会话发出LOCK TABLES语句以在已经持有锁时获取锁,则在授予新锁之前会隐式释放其现有锁。

您正在同一命令上获取“ba_flight”上的双重锁定。另据我所知,无论如何你都无法在同一张桌子上获得2个写锁。

请尝试删除同一桌面上的重复锁定,然后重试。

另外,您最好检查this documentation for locking inside transactions是否有正确的锁定表格。