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

Leave a Comment

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