Commit 2224d1a
authored
Improve concurrent database access (#361)
## Summary
Reduces lock contention under concurrent access by avoiding unnecessary
write locks for read-only queries.
1. **Skip the wrapper transaction for SELECT statements.** A standalone
SELECT translates to a single SQLite query, so the implicit autocommit
transaction provides sufficient consistency — no explicit
`BEGIN`/`COMMIT` needed.
2. **Use deferred `BEGIN` for other read-only wrapper transactions.**
SHOW and DESCRIBE queries still use a wrapper transaction (they may
translate to multiple SQLite queries), but now use a deferred `BEGIN`
(SHARED lock) instead of `BEGIN IMMEDIATE` (RESERVED lock).
### Background
The wrapper transaction previously used `BEGIN IMMEDIATE` for all
queries, including read-only SELECTs. This acquired a RESERVED (write)
lock on every query — but SQLite only allows one RESERVED lock at a
time. Under concurrent load (e.g., multiple PHP-FPM workers handling
requests simultaneously), all processes competed for the single write
lock. When the 10-second busy timeout was exceeded, SQLite returned
`SQLITE_BUSY` ("database is locked", error 5).
SHARED locks, acquired by a deferred `BEGIN`, are compatible with
RESERVED locks — multiple readers can proceed concurrently alongside an
active writer.
Fixes #3181 parent 1e4a19a commit 2224d1a
2 files changed
Lines changed: 158 additions & 3 deletions
File tree
- packages/mysql-on-sqlite
- src/sqlite
- tests
Lines changed: 22 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
883 | 883 | | |
884 | 884 | | |
885 | 885 | | |
| 886 | + | |
886 | 887 | | |
887 | 888 | | |
888 | 889 | | |
889 | 890 | | |
890 | 891 | | |
891 | 892 | | |
| 893 | + | |
| 894 | + | |
| 895 | + | |
| 896 | + | |
| 897 | + | |
| 898 | + | |
| 899 | + | |
| 900 | + | |
| 901 | + | |
| 902 | + | |
| 903 | + | |
| 904 | + | |
| 905 | + | |
| 906 | + | |
| 907 | + | |
| 908 | + | |
| 909 | + | |
| 910 | + | |
| 911 | + | |
| 912 | + | |
| 913 | + | |
892 | 914 | | |
893 | 915 | | |
894 | 916 | | |
| |||
1366 | 1388 | | |
1367 | 1389 | | |
1368 | 1390 | | |
1369 | | - | |
1370 | 1391 | | |
1371 | 1392 | | |
1372 | 1393 | | |
| |||
1444 | 1465 | | |
1445 | 1466 | | |
1446 | 1467 | | |
1447 | | - | |
1448 | 1468 | | |
1449 | 1469 | | |
1450 | 1470 | | |
1451 | 1471 | | |
1452 | 1472 | | |
1453 | 1473 | | |
1454 | | - | |
1455 | 1474 | | |
1456 | 1475 | | |
1457 | 1476 | | |
| |||
Lines changed: 136 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
0 commit comments