Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change sqlx-sqlite to check for uniqueness error instead of checking …
…for existence Currently, the sqlx-sqlite store has an issue with databases running using the WAL journal if multiple requests come in at the same time, causing a "database is locked" error. The sequence is roughly this: 1. Two connections call `SqliteStore::create`, at the same time. 2. Concurrently, they both call `SqliteStore::id_exists`. Both lock the database for reading and sets their "end mark" to the current database state. 3. Connection 1 calls `save_with_conn` first and commits its transaction. This upgrades its lock to a write lock and advances the database state - connection 2's end mark is still before this update. 4. Connection 2 calls `save_with_conn`. It attempts to upgrade its read lock to a write lock, but fails since the database has been modified since the read lock was acquired, and sqlite returns a "database is locked" error. This patch fixes the issue by removing the `if_exists` check and instead attempts to insert the ID, check if the insert fails with a uniqueness error, and regenerate the ID if so. With this, all connections immediately lock the database for writing and will wait for each other to finish before inserting. It should also be more performant in the common case of the generated ID being unique.
- Loading branch information