SQLite - Database is locked - Jon Galloway

SQLite - Database is locked

Summary:SQLite is a great, free, embedable database. It's fast, tiny, and reliable, and has some cool features like support for NTFS compression of the database file. It's multithreaded, but its coarse locking can be a problem.

I first read about SQLite over at EggHeadCafe researching ASP.NET database cache dependencies. It's small, fast, free, and there's a good SQLite ADO.NET provider on SourceForge. More about it here (PPT). I decided to use it for an RSS aggregator pet project. It worked well for a while, but I ran into problems when I began using it in a multi-threaded application (one thread downloading feeds, another marking a feed read when viewed).

SQLite is technically thread safe, but a write operation locks the entire database (more):
- Any resultset being step()'d through uses a shared read-only lock. 
- Any insert/update being executed requires an exclusive write lock. 

You would think the provider would wrap this with some retries or a reader / writer lock (the Python Sqlite wrapper handles retries), but it just throws the Database is Locked exception. I've filed a bug on this, but I'm not too hopeful - it may be viewed as "that's just how SQLite works".

I contemplated wrapping my database access with a TimedLock, but in the end I decided this was a good time to give SharpHSQL a shot. I'm not certain it will handle the multithreaded access, either, but the idea of a pure c# embeded database in my c# app appealed to me.

I think SQLite with the SQLite ADO.NET provider is great for applications with simple data needs, but watch out if you're planning to perform concurrent updates or inserts.

Published Friday, July 08, 2005 4:51 AM by Jon Galloway
Filed under: ,

Comments

# re: SQLite - Database is locked

Nice...

Thursday, November 29, 2007 2:44 AM by Kleanthe

# re: SQLite - Database is locked

Nice

Thursday, November 29, 2007 8:41 AM by Ivan

# re: SQLite - Database is locked

I encountered this problem too. I notice your post is an old one, but if you haven't figured out the solution yet, sqlite.org has this to say about it:

When SQLite tries to access a file that is locked by another process, the default behavior is to return SQLITE_BUSY. You can adjust this behavior from C code using the sqlite3_busy_handler() or sqlite3_busy_timeout() API functions.

www.sqlite.org/.../busy_handler.html

www.sqlite.org/.../busy_timeout.html

Good luck,

Ron

Wednesday, March 26, 2008 3:49 AM by Ron

# re: SQLite - Database is locked

What about from Perl code?

Thursday, February 05, 2009 7:55 PM by Phil Goetz

# re: SQLite - Database is locked

I came up with a great solution for this problem if all you want to do is read something like firefox history. Firefox locks the database when the browser is open, so just make a copy of the file programmatically and then read it.

Tuesday, April 21, 2009 10:20 AM by David

# re: SQLite - Database is locked

> Tuesday, April 21, 2009 10:20 AM by David

> I came up with a great solution for this problem...

David, man, thanks a ton!

/HS

Friday, June 25, 2010 11:27 PM by Harry

# re: SQLite - Database is locked

David thanks a lot, closing firefox solved the problem for me.

Wednesday, August 18, 2010 3:25 AM by Nick

# re: SQLite - Database is locked

what about sqlite database locked in c# application...? please help...

Friday, April 15, 2011 3:47 PM by sachin

# re: SQLite - Database is locked

I did another thing..

I retried it 3 times after one mili second, and I noticed that it tried upto 2 times max and the app work smothly  

Thursday, May 12, 2011 2:28 AM by Akbar

# re: SQLite - Database is locked

You can write a wrapper in Perl to catch the error and try again until the lock is gone, but it isn't practical.  The lock lasts for about 30 seconds, even if the other process just connects, writes one row, and immediately disconnects.  This is a very serious problem with SQLite.

Friday, February 03, 2012 3:05 PM by Phil Goetz

Leave a Comment

(required) 
(required) 
(optional)
(required)