高并发多线程共用同一个全局连接实例读写数据会有问题吗(Redis/MySQL)? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
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
xsx
V2EX    MySQL

高并发多线程共用同一个全局连接实例读写数据会有问题吗(Redis/MySQL)?

  •  
  •   xsx 2019-06-03 22:55:49 +08:00 5477 次点击
    这是一个创建于 2374 天前的主题,其中的信息可能已经有所发展或是发生改变。

    全局变量 gMySQLClient=new MySQLClient(127.0.0.1:3306),gRedisClient =new RedisClient(127.0.0.1:6379);

    场景 1-MySQL: 线程 A: resA=gMySQLClient.Exec("Select * From A WHERE id=1").GetExecResult(); 线程 B: resB=gMySQLClient.Exec("Select * From B WHERE id=1").GetExecResult(); 该场景有没可能发生 resA 是线程 B 返回的结果?假设线程 B 的结果比线程 A 先返回 
    场景 2-MySQL: 线程 A: resA1=gMySQLClient.Exec("BEGIN").GetExecResult(); resA2=gMySQLClient.Exec("Update A SET a='a1' WHERE id=1").GetExecResult(); sleep(5*Time.Second);//t1 时刻,resA2 执行成功,resA3 暂未执行 resA3=gMySQLClient.Exec("Update B SET b='b1' WHERE id=1").GetExecResult(); resA4=gMySQLClient.Exec("COMMIT").GetExecResult(); 线程 B: resB1=gMySQLClient.Exec("BEGIN").GetExecResult(); resB2=gMySQLClient.Exec("Update C SET c='c1' WHERE id=1").GetExecResult() resA3=gMySQLClient.Exec("COMMIT").GetExecResult(); 该场景有没可能发生线程 B 执行的 COMMIT 恰好在 t1 时刻执行,然后把线程 A 提前 COMMIT 生效了? 
    场景 3-Redis: 线程 A: resA=gRedisClient.Get("keyA") 线程 B: resB=gRedisClient.Get("keyB") 该场景有没可能发生 resA 是线程 B 返回的结果?假设线程 B 的结果比线程 A 先返回 

    不太清楚它们底层的处理方式,恳请大佬解答,谢谢~

    10 条回复    2019-06-13 14:58:01 +08:00
    NewDraw
        1
    NewDraw  
       2019-06-03 23:23:13 +08:00 via Android
    这些都是同步阻塞调用,不会出现你说的问题,高并发情况下,你应该考虑的是性能是否满足需求。
    billlee
        2
    billlee  
       2019-06-03 23:46:36 +08:00
    场景二 mysql 事务是肯定不行的,两个事务会交织到一起
    其它两个场景要看实现,一般你用的库会说明。
    xsx
        3
    xsx  
    OP
       2019-06-03 23:57:35 +08:00
    @NewDraw 意思是在执行第二个命令之前需要等第一个命令执行返回结果吗?
    xsx
        4
    xsx  
    OP
       2019-06-04 00:01:47 +08:00
    @billlee 场景二 MySQL 能区分出两个事务并分别按顺序正确执行吗?
    sagaxu
        5
    sagaxu  
       2019-06-04 00:06:26 +08:00 via Android
    一般都不行,只有少数库可以
    xsx
        6
    xsx  
    OP
       2019-06-04 00:15:25 +08:00
    @sagaxu 所以一般需要使用连接池分配给不同的线程吗?
    wisefree
        7
    wisefree  
       2019-06-04 07:10:33 +08:00
    每个线程用不同的 connection
    xsx
        8
    xsx  
    OP
       2019-06-04 10:18:56 +08:00
    @wisefree 从连接池取不同的 connection?
    billlee
        9
    billlee  
       2019-06-05 23:34:45 +08:00
    @coderqbc #4 显然是不行的。一般有两种方案

    1. 使用连接池,每次要用的时候从连接池获取一个连接,只在同一个线程中使用,一般就是很紧凑的局部代码块中使用。用完释放。
    2. 用 thread-local 语义,每个线程创建一个连接。许多 web 相关的框架会使用这种方案。
    chenhongron
        10
    chenhongron  
       2019-06-13 14:58:01 +08:00 via Android
    同一个 session 肯定有问题的,会出现很多异常
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     3038 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 23ms UTC 13:03 PVG 21:03 LAX 05:03 JFK 08:03
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86