Difference between SET autocommit=1 and START TRANSACTION in mysql (Have I missed something?)

前端 未结 4 1926
清酒与你
清酒与你 2020-11-28 02:10

I am reading up on transactions in MySQL and am not sure whether I have grasped something specific correctly, and I want to be sure I understood that correctly, so here goes

4条回答
  •  情歌与酒
    2020-11-28 02:54

    https://dev.mysql.com/doc/refman/8.0/en/lock-tables.html

    The correct way to use LOCK TABLES and UNLOCK TABLES with transactional tables, such as InnoDB tables, is to begin a transaction with SET autocommit = 0 (not START TRANSACTION) followed by LOCK TABLES, and to not call UNLOCK TABLES until you commit the transaction explicitly. For example, if you need to write to table t1 and read from table t2, you can do this:

    SET autocommit=0;
    LOCK TABLES t1 WRITE, t2 READ, ...;... do something with tables t1 and t2 here ...
    COMMIT;
    UNLOCK TABLES;
    

提交回复
热议问题