超过锁定等待超时;尝试重新启动事务 - 插入语句

Posted

技术标签:

【中文标题】超过锁定等待超时;尝试重新启动事务 - 插入语句【英文标题】:Lock wait timeout exceeded; try restarting transaction - Insert statement 【发布时间】:2020-04-18 09:54:03 【问题描述】:

这是我的用户表方案:

-- auto-generated definition
CREATE TABLE Tb_Users (
    user_id             bigint UNSIGNED AUTO_INCREMENT
        PRIMARY KEY,
    username            varchar(30)                           NULL,
    mobile              varchar(11)                           NULL,
    password            varchar(100)                          NULL,
    name                varchar(100)                          NULL,
    family              varchar(150)                          NULL,
    email               varchar(100)                          NULL,
    national_id         varchar(10)                           NULL,
    company_national_id varchar(11)                           NULL,
    company_eco_code    varchar(12)                           NULL,
    company             varchar(250)                          NULL,
    sex                 tinyint                DEFAULT 1      NULL,
    tel                 varchar(20)                           NULL,
    marital_status      tinyint(1)             DEFAULT 0      NOT NULL,
    picture             varchar(255)                          NULL,
    birthday            datetime                              NULL,
    wedding_date        datetime                              NULL,
    wife_birthday       datetime                              NULL,
    description         varchar(255)                          NULL,
    is_active           tinyint(1)             DEFAULT 0      NULL,
    two_factor_enabled  tinyint(1)             DEFAULT 0      NULL,
    address             text                                  NULL,
    lang                varchar(5)                            NULL,
    type                enum ('real', 'legal') DEFAULT 'real' NULL,
    remember_token      varchar(100)                          NULL,
    country_id          int                                   NULL,
    province_id         int                                   NULL,
    city_id             int                                   NULL,
    level_id            int UNSIGNED           DEFAULT 1      NOT NULL,
    supervisor_id       bigint UNSIGNED                       NULL,
    activator_id        bigint UNSIGNED                       NULL,
    creator_id          bigint UNSIGNED                       NULL,
    updator_id          bigint UNSIGNED                       NULL,
    last_logined_at     datetime                              NULL,
    latitude            decimal(10, 8)                        NULL,
    longitude           decimal(10, 8)                        NULL,
    activated_at        datetime                              NULL,
    referral_user_id    bigint UNSIGNED                       NULL,
    created_at          datetime                              NULL,
    updated_at          datetime                              NULL,
    deleted_at          datetime                              NULL,
    personal_code       bigint UNSIGNED                       NULL,
    CONSTRAINT Tb_Users_personal_code_uindex
        UNIQUE (personal_code),
    CONSTRAINT users_company_eco_code_uindex
        UNIQUE (company_eco_code),
    CONSTRAINT users_company_national_id_uindex
        UNIQUE (company_national_id),
    CONSTRAINT users_email_uindex
        UNIQUE (email),
    CONSTRAINT users_mobile_uindex
        UNIQUE (mobile),
    CONSTRAINT users_national_id_uindex
        UNIQUE (national_id),
    CONSTRAINT users_username_uindex
        UNIQUE (username),
    CONSTRAINT Tb_Users_Tb_Users_activator_fk
        FOREIGN KEY (activator_id) REFERENCES Tb_Users (user_id)
            ON UPDATE SET NULL ON DELETE SET NULL,
    CONSTRAINT Tb_Users_Tb_Users_creator_fk
        FOREIGN KEY (creator_id) REFERENCES Tb_Users (user_id)
            ON UPDATE CASCADE ON DELETE CASCADE,
    CONSTRAINT Tb_Users_Tb_Users_refferal_user_id_fk
        FOREIGN KEY (referral_user_id) REFERENCES Tb_Users (user_id)
            ON UPDATE SET NULL ON DELETE SET NULL,
    CONSTRAINT Tb_Users_Tb_Users_supervisor_fk
        FOREIGN KEY (supervisor_id) REFERENCES Tb_Users (user_id)
            ON UPDATE SET NULL ON DELETE SET NULL
)
    ENGINE = InnoDB
    COLLATE = utf8mb4_unicode_ci;

CREATE INDEX Tb_Users_is_active_index
    ON Tb_Users (is_active);

CREATE INDEX users_club_levels_level_id_fk
    ON Tb_Users (level_id);

但是当我想插入新记录时,我在查询 50 秒后收到此错误消息:

超过锁定等待超时;尝试重启事务

我的插入查询:

INSERT
INTO
    Tb_Users (user_id, username, mobile, password, name, family, email, national_id, company_national_id, company_eco_code, company, sex, tel, marital_status, picture, birthday, wedding_date, wife_birthday, description, is_active, two_factor_enabled, address, lang, type, remember_token, country_id, province_id, city_id, level_id, supervisor_id, activator_id, creator_id, updator_id, last_logined_at, latitude, longitude, activated_at, referral_user_id, created_at, updated_at, deleted_at, personal_code)
VALUES
(8473, '9120882602', '09120882602', null, 'ali', 'ghodrati', null, null, null, null, null, 0, null, 0, null, '1993-02-28 00:00:00', '1970-01-01 03:30:00', '1970-01-01 03:30:00', null, 1, 0, null, 'fa', 'real', null, 102, null, null, 1, 8402, 8402, 8402, null, null, null, null, '2019-12-29 08:59:20', null, '2019-12-14 21:38:59', null, null, 123)

它没有显示任何错误,我可以找出问题!

【问题讨论】:

表被另一个事务阻塞(插入/更新/索引重建/...) 不,因为我可以通过另一个 Web 服务读写。问题是我的查询,但我找不到问题。 因此,您可能有未明确关闭的未结交易。 BEGIN TRAN ... INSERT INTO ... COMMIT 或相当于您的应用程序代码 Java/C#/php 【参考方案1】:

解决了。

我重新启动了服务器。有效 !!!

【讨论】:

以上是关于超过锁定等待超时;尝试重新启动事务 - 插入语句的主要内容,如果未能解决你的问题,请参考以下文章

保存/更新模型时,Django“超过锁定等待超时;尝试重新启动事务”

MySQL 错误:超过锁定等待超时;尝试重新启动事务查询

超过锁定等待超时;尝试重新启动事务 MYSQL Python

超过锁定等待超时;从表中删除数据时尝试重新启动事务

Python3 - '超过锁定等待超时;尝试重新启动事务'并且只在数据库上处理

使用 C# 在 Mysql 上死锁 - “超过锁定等待超时;尝试重新启动事务”