我应该使用哪个 MySql InnoDB 事务隔离级别?

Which MySql InnoDB Transaction Isolation Level Should I use?

我正在使用 .NET MySql客户端 v6.6.4.0 针对 MySql 服务器 v5.7.19

Show table Status 

显示所有 table 都在使用 InnoDB 引擎

SHOW VARIABLES LIKE 'tx_isolation';

returns

tx_isolation    REPEATABLE-READ

我的问题很简单

我有一个进程每分钟执行以下操作

Open a database connection
Begin a Transaction (using default isolation mode of RepeatableRead)
Delete all rows from a table
Insert a bunch of rows - at least 50 or 60
Commit transaction
Close connection

我还有多个单独的进程可以随时查询 table。

我期望如果 table 在事务范围内写入时被查询,reader 将阻塞直到事务完成,然后取回所有50 或 60 行。

我发现 reader 不会阻塞并接收到那时为止已写入的任何内容。

我需要什么隔离级别才能在填充 table 时阻止 reader 并接收全套数​​据?

因此我不会更改事务隔离级别。我宁愿使用明确的 table write lock:

The session that holds the lock can read and write the table.

Only the session that holds the lock can access the table. No other session can access it until the lock is released.

Lock requests for the table by other sessions block while the WRITE lock is held.

由于锁定和解锁 table 语句在给定会话中提交任何打开的事务,因此您需要小心使用它们。 Mysql在上面link中提供了下面的例子:

SET autocommit=0;
LOCK TABLES t1 WRITE;
... do something with table t1 here ...
COMMIT;
UNLOCK TABLES;