› MySQL 5.5 Community Server
› MySQL 5.6 Community Server
› Percona Configuration Wizard
› XtraBackup 搭建主从复制
Great Sites on MySQL
› Percona
› MySQL Performance Blog
› Severalnines
推荐管理工具
› Sequel Pro
› phpMyAdmin
推荐书目
› MySQL Cookbook
MySQL 相关项目
› MariaDB
› Drizzle
参考文档
› http://mysql-python.sourceforge.net/MySQLdb.html
rateltalk
V2EX  ›  MySQL

Mysql 查询写入时对表级所的疑问

  •  
  •   rateltalk ·
    shangdev · Feb 3, 2023 · 2592 views
    This topic created in 1334 days ago, the information mentioned may be changed or developed.

    业务:第三方回调,同时会 post 多条过来,为防重复插入,加事务表级锁。

    假设有表如下:a, b, c

    处理函数如下。结果在查表 c 时,报错 1100 Table 'c' was not locked with LOCK TABLES,事务不是在 fun2 里就已经处理完毕了吗,为何还会出现这个提示?

    function fun()
    {
    	if (xxx) {
        	fun2();
        }
        
        // 查表 c
        $db->query("select * from c");
    }
    
    // 事务查询,表级锁
    function fun2()
    {
    	$db->startTrans();
        $db->execute("LOCK TABLE a WRITE, b WRITE, c READ;");
        $db->query("select * from a");
        $db->query("select * from b");
        $db->execute("update a set name = 'xx' where ...");
        $db->commit();
    	$db->execute("UNLOCK TABLES;");
    }
    
    6 replies  •  2023-02-03 17:36:51 +08:00
    opengps
        1
    opengps  
       Feb 3, 2023
    注意:同时会 post 多条过来,假设是 AB 等多条
    那就意味着,A 的 fun2 完了,但是 B 的 fun2 还没开始。锁表却是个全局的,部分你 ABC 。。。。
    xuxixk
        2
    xuxixk  
       Feb 3, 2023
    防重复插入就用唯一索引,不要自创别的方法
    rateltalk
        3
    rateltalk  
    OP
       Feb 3, 2023
    @xuxixk 设置唯一索引会报错,假设 AB 同时进来,A 已经 create 数据了,B 也会 create 数据,B 就会报索引唯一错误。
    ttwxdly
        4
    ttwxdly  
       Feb 3, 2023
    一楼说得对。
    lookStupiToForce
        5
    lookStupiToForce  
       Feb 3, 2023
    一楼说得对。
    唯一索引报错就报错啊,你处理一下不报错就成了啊
    hhjswf
        6
    hhjswf  
       Feb 3, 2023 via Android
    op 不用唯一索引的意思,我想大概是不希望接口出现重复异常,让人觉得程序有问题不够健壮😂如果性能要求不高,尝试分布式锁,把请求串行化
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   5200 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 02:13 · PVG 10:13 · LAX 19:13 · JFK 22:13
    ♥ Do have faith in what you're doing.