开发者

"Lock wait timeout exceeded; try restarting transaction" on simple table

开发者 https://www.devze.com 2023-01-27 04:38 出处:网络
I have created database table. But there is a strange thing I can insert row into table, but when I want to delete or update database row it throws an error:

I have created database table. But there is a strange thing I can insert row into table, but when I want to delete or update database row it throws an error:

/* SQL Error开发者_如何学编程 (1205): Lock wait timeout exceeded; try restarting transaction */

In other tables there is no errors, and everything works pretty fine.

Here is a table structure:

CREATE TABLE `cruises` (
    `id` BIGINT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    `enable_text` ENUM('true','false') NOT NULL,
    `enable_file` ENUM('true','false') NOT NULL,
    `title` VARCHAR(256) NOT NULL,
    `full_text` MEDIUMTEXT NOT NULL,
    `description` VARCHAR(256) NULL,
    `date_of_departure` DATE NOT NULL,
    `number_of_nights` INT(10) UNSIGNED NOT NULL,
    `point_of_departure` VARCHAR(256) NOT NULL,
    `cruise_type` BIGINT(10) UNSIGNED NOT NULL,
    `cruises_document_id` BIGINT(10) UNSIGNED NOT NULL,
    `price` FLOAT(5,0) UNSIGNED NOT NULL,
    PRIMARY KEY (`id`),
    INDEX `FK_cruises_cruises_types` (`cruise_type`),
    INDEX `FK_cruises_cruises_documents` (`cruises_document_id`),
    CONSTRAINT `FK_cruises_cruises_documents` FOREIGN KEY (`cruises_document_id`) REFERENCES `cruises_documents` (`id`),
    CONSTRAINT `FK_cruises_cruises_types` FOREIGN KEY (`cruise_type`) REFERENCES `cruises_types` (`id`)
)
ENGINE=InnoDB
ROW_FORMAT=DEFAULT

I have tried this thing on 2 different databases, but the same problem appears all the time. Any ideas?


This is problem of lock contention, which ultimately result in a time-out on one of the lock. Here are a few suggestions:

  • Make sure you have the correct indexes which result in row-level locks not table-level lock. This will reduce the contention.
  • Make sure you have indexes on the foreign key constraints. To check the relational constraints during insert or update, some database lock the whole referenced table if there is no such index (don't know if this is the case of MySQL)
  • If problem is still here, try to make the transaction faster/smaller. Again, this will reduce the contention on the database.
  • Increase the timeout but keep the value reasonable

Also check if you are obtaining two connections. Because of that two transaction will get mixed and you will get above error!!!

Also check this link.. this might help you...

0

精彩评论

暂无评论...
验证码 换一张
取 消