question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support setting a timeout for SQLite

See original GitHub issue

Problem

SQLite queries fail immediately if the DB is locked.

I blindly tried passing arguments similarly to how the docs show for PostgreSQL, needless to say, it didn’t work.

datasource db {
  provider = "sqlite"
  url      = "file:../db.sqlite?timeout=5000"
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:9
  • Comments:73 (31 by maintainers)

github_iconTop GitHub Comments

6reactions
internalfxcommented, Jul 13, 2020

@pimeys thank you for taking the time to respond.

SQLite is not really meant for multi-user use cases. It’s much better if you just have one writer to the database.

Agreed. But let me preface our future discussion by saying I’m not just using SQLite for the fun of it. I’ve used it in the past and have studied it’s capabilities and limitations.

Every connection locks the whole thing while reading or writing, so you only want one of them to that database.

That is not entirely correct, SQLite can handle multiple readers quite well.

That error comes directly from the database. We really can’t do anything to it, if SQLite decides to error out while holding multiple connections into it.

I think you might be mistaken here. SQLite directly supports a “busy timeout”. See docs

Also the sqlite3 npm package supports the timeout as well. See docs

I can only assume that prisma could do something similar.

If you need more connections, I’d consider trying out PostgreSQL.

I work with small companies, usually developing in-house software to be used by at most 5-10 people. SQLite is more than capable of keeping up. And in “production” prisma is the only process that will be communicating with the DB in the vast majority of all cases.

However, if I need to do any maintenance of any kind. I can cause the DB to start throwing BUSY errors. It’s not a problem for the DB to just wait a few seconds. Hence, why I am trying to set a timeout.

The program having to wait is very acceptable in this case, but the busy errors are troublesome.

Is there any other way I can set the timeout?

4reactions
JedWatsoncommented, Oct 21, 2020

I also thought it worth sharing that I’ve found this issue as well through a different path. I tried out Blitz on the weekend and spun up a demo project with SQLite and it was mostly a really good experience.

However the first thing I did was set up a database seed file per their guide here

And of course, I had a few dozen things to create to I wrapped them in an await Promise.all(...) statement, which would have tried to run multiple concurrent operations through the same Prisma Client (this is all in a single thread running on my dev machine)

And as soon as you get to about three things happening in parallel (specifically, create statements) the database throws with Operation timed out (SQLite database is busy)

I mention this mainly because it’s a weird/hard block to hit in the first experience, given I was following the documented “happy path” for both Prisma and Blitz.

I was able to work around the problem by rewriting my code to create entries in the database sequentially (using a for ... loop and async await db.thing.create() call in each iteration) but ideally either Prisma Client or the SQLite settings would sort this out for me.

cc/ @flybayer because it impacts Blitz getting started experience

Read more comments on GitHub >

github_iconTop Results From Across the Web

Set A Busy Timeout - SQLite
This routine sets a busy handler that sleeps for a specified amount of time when a table is locked. The handler will sleep...
Read more >
Increase the lock timeout with sqlite, and what is the default ...
The sqlite3 command line shell has the normal Sqlite default of 0; that is to say, no timeout. If the database is locked,...
Read more >
How do I specify a timeout in sqlite3 from the command-line?
You can do this by using an init file. init.sql (note that the timeout value is in milliseconds - 1 is rather short):...
Read more >
.timeout - Using SQLite [Book] - O'Reilly
The .timeout command is used to set a retry timer. If a timer value is set and a locked database is encountered, rather...
Read more >
SQLite3::busyTimeout - Manual - PHP
The milliseconds to sleep. Setting this value to a value less than or equal to zero, will turn off an already set timeout...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found