I can't lock my MySQL table
I have a (for me) strange problem. I try to lock a set of tables for a transaction that I am trying to do. But for some reason at least one of my tables wont lock.
The code I have looks like this, please remeber it's only the locks that I have problems with currently but all comments are appreciated :)
For some reason ba_flight doesnt get locked or thats the problem i most commonly have, however if I call just: LOCK TABLES ba_flight it goes perfectly fine.
-- 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 Gondo开发者_C百科r');
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;
This is an assignement for school just so that you know, I would really like to understand why I have this problem.
from mysql documentation about locking :
If a session issues a LOCK TABLES statement to acquire a lock while already holding locks, its existing locks are released implicitly before the new locks are granted.
you are acquiring double lock on "ba_flight" on the same command. Also as far as I know you can't acquire 2 write locks on the same table anyway.
Please try to remove duplicated locks on same table and try again.
Also you would better check this documentation for locking inside transactions for the right way to lock tables.
精彩评论