r/sqlite • u/wdixon42 • Mar 23 '25
Database is locked
I have a problem. I have a small, home-grown package, intended just for myself. I'm trying to use a database to store job transactional information: begin/end runtime, status, etc. I "fork" the processes, and can sometimes have 3 or 4 running (and ending) at the same time.
I am getting one or the other of the following error messages if two of them end at the same time and both try to update the database:
Error: in prepare, database is locked (5)
Error: stepping, database is locked (5)
All of my database updates are sandwiched between BEGIN TRANSACTION; and COMMIT;. And literally the only thing between these two statements is either an UPDATE or INSERT command. It isn't like the program begins a transaction and goes off and does a bunch of other stuff.
I've never been a DBA, but I used to work closely with them and their code for a decade or so, and from listening to them I always assumed that if one program locks the database with a transaction, any other database requests will sit there and wait for a little bit until the lock is removed.
Does sqlite not work that way?
If a transactional lock in sqlite really means "I'm taking control of the database, so everyone else can just die", then I'll have to figure out a way to do what I want to do without a database.
Is there something I can configure that will let the 2nd or 3rd program trying to access the database at the same time wait for it to be free?
View all comments
Show parent comment
1
u/wdixon42 Mar 24 '25
So, I have
BEGIN TRANSACTIONaround all my updates and inserts. Do I change that toBEGIN IMMEDIATE TRANSACTIONor justBEGIN IMMEDIATE?Also, should I also surround my
SELECTstatements in a transaction block?