Closed deadcoder0904 closed 9 months ago
The error "database is locked" or "SQLITE_BUSY" is not exclusive to better-sqlite3 but sqlite3 itself.
I would guess that something within all of these uncontrollable steps and tools is doing some concurrent access to the same database file without properly closing it or finishing started transactions before the next process is trying to do its stuff.
Have you tried to limit next.js to only use 1 Process while building https://docs.uniform.dev/sitecore/deploy/how-tos/how-to-control-nextjs-threads/ ?
Besides just enabling WAL, you may want to adjust the busy_timeout
PRAGMA.
But as @neoxpert states, this is (expected!) SQLite behavior.
@neoxpert thanks for the idea, i'll test it out. i didn't know that i could do it.
@mceachen yeah, i did see that somewhere online but didn't know it was a PRAGMA i can set. what's the catch though? is it ok to set? is it recommended?
bdw, can you guys also answer why docker + sqlite wal mode gives data loss issues as stated in my readme. i'm using windows (wsl2) + docker (without docker desktop). i'm assuming its something to do with windows filesystem?
is it recommended?
That's up to you. I linked to the SQLite docs.
i'm using windows (wsl2) + docker
I've not had success with SQLite on WSL. It assumes a well-behaved local POSIX filesystem, which is a bit much for WSL to claim.
You may avoid WSL filesystem madness by not using WAL and disabling SHM, but at some point it's better to just run linux and route around 30 years of technical debt.
cool.
i think this worked as i haven't faced any issues so far:
/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
// recommended to solve https://github.com/WiseLibs/better-sqlite3/issues/1155
workerThreads: false,
cpus: 1,
},
.
.
.
}
I face this error randomly once in a while.
And idk how to debug it. I tried making a singleton.
I wanted to see if WAL mode solves it & I tried WAL mode as recommended at https://til.simonwillison.net/sqlite/enabling-wal-mode
But for some reason, I faced data loss issues which I've described in the README of the linked project below. However, I solved the data loss issues by adding Litestream as a SQLite Database Backup solution with Cloudflare R2 so whenever data is lost, Litestream recovers it with its own WAL mode. The full problem is explained well in the README. I was doing something nasty that probably won't happen in production but yeah it did have some problems.
Reproduction -> https://github.com/deadcoder0904/easypanel-nextjs-sqlite
Anyways, how can I solve this error permanently?
I've looked into it & there is one answer that you must close your database but
better-sqlite3
doesn't allow to close database.I've thought that 2 processes access the same database when in
api/
route in Next.js so I tried addingexport const dynamic = "force-dynamic"
on every api route but that didn't work.I tried using a default export for
db
instead of a named export but that didn't work.I tried using a singleton with the help of https://github.com/epicweb-dev/remember but that didn't work either.
The problem with this error is it happens randomly once in a while. But for the last few days, it has been happening 3-4 times sometimes which is why I opened the issue now.
I'm not doing anything different. Just my big project has like 10 api routes but only 5 routes importing the
db
but I still get that error when thenext build
appears inDockerfile
on this line:The error looks like:
This happens when I try
make build-production
in the Terminal.I solve it by running
make build-production
3 or 4 times but I am really annoyed by this error because its happening a lot of times now.Any ideas?
My intuition is when
next build
runs, all routes in api folder access the same file which open's sqlite database , i.e,./db/index.ts
& it throws this error. They all access it in milliseconds but somehow still fail.What's the solution?