Database locked in WAL mode with only readers

前端 未结 1 857
礼貌的吻别
礼貌的吻别 2021-02-08 03:27

Using System.Data.Sqlite 1.0.86.0 (including SQLite 3.7.17) in Write-Ahead Logging mode, I\'m experiencing database locks while reading concurrently, which shouldn\'t be the cas

1条回答
  •  一生所求
    2021-02-08 03:46

    After investigation, it's not reading that locks the database, but simply opening a connection. As I understand it after reading the WAL documentation again, even readers must have write access to the WAL file. The simple fact of opening a connection has a much greater cost than in non-WAL mode. This operation apparently includes acquiring an exclusive lock on the WAL file, even if it's for a very short period.

    A simple solution is to enable pooling (Pooling=True in the connection string). It doesn't have any effect in the sample since all connections are opened at the same time, but in a real world application there isn't any lock anymore since existing connections are reused. Most simple queries went from 5ms to less than 1ms (on a SSD) and the "database is locked" messages are entirely gone.

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