Transaction issue with knexjs, typescript and mariadb

前端 未结 2 1314
野趣味
野趣味 2021-01-15 12:32

I want to have a transaction, in typescript, to refresh some data in a table. To do so the following steps need to apply:

  1. Truncate all the records from a table
相关标签:
2条回答
  • 2021-01-15 13:07

    Don't do it that way. Instead:

    CREATE TABLE new LIKE real;
    populate `new`
    RENAME TABLE real TO old, new TO real;
    DROP TABLE old;
    

    If the CREATE or populate fail, then abandon the task; no harm done. (Well, need to DROP TABLE real.)

    The RENAME TABLE is very fast, and atomic, and unlikely to fail.

    This approach has the advantage that, to other queries, the real table is available and fully populated the entire time.

    0 讨论(0)
  • 2021-01-15 13:26

    With mysql all schema altering DDL queries does implicit commit and they cannot be rolled back. So you need to change your implementation to guarantee integrity of db in some other way.

    https://dev.mysql.com/doc/refman/8.0/en/cannot-roll-back.html https://dev.mysql.com/doc/refman/8.0/en/implicit-commit.html

    0 讨论(0)
提交回复
热议问题