When the developer of a justifiably famous SQL engine whacks himself on the head and tells you to get the fix, you’d be wise to do what you’re told.
SQLite is well-known for its small size, SQL92 compatibility, ease of use, licensing… as in whatever… and price… as in free. It’s used in many commercial products, like the X Multimedia System (XMMS), DragonDB, and Core Data (which Apple ships with its Tiger OS).
The lite in SQLite is a bit of a misnomer. Vivisimo, the producers of Clusty the search engine, and a company that services sites that manage millions of documents, is a user. In fact, SQLite version 3 can grapple with databases as large as 2 terabytes. That’s pretty heavy in most folks’ books.
So what’s the problem?
In a word, multithreading. It hasn’t been SQLite’s strong suit.
Last March, Kervin Pierre, project manager for Open Connector, the open-source Microsoft Outlook connector project, expressed the frustration of many SQLite multithreading implementors, when he wrote:
I’ve been pulling my hair out over concurrency problems with SQLite 3. SQLITE_BUSY, SQLITE_SCHEMA, SQLITE_MISUSE, I get them all.Over the last few weeks, I’ve been strictly debugging SQLite problems in my application. No program logic besides fixing ( and apparently creating in the process ) SQLite errors.
Vivisimo’s Chris Palmer gave a more detailed description of his multithreading challenges, outlining a problem with lock contention:
I am running version 3.2.1 of sqlite and I have applied the patch to fix a previously reporting multithreading problem (to do with the global recover) …I have a multithreaded (2 threads) application that does a large number of tiny updates (each update is a seperate transaction). After running for awhile the application ends up getting stuck with both threads unable to acquire a lock on the database.
That is, one thread acquires a shared lock, another thread piggy backs on this shared lock and then eventually the second thread (which did not itself do the file locking for the shared lock) tries to change the lock which is not valid.
Until now, SQLite’s creator, D. Richard Hipp, has provided scholarly descriptions of how to work with locking and multithreading properly. To safely multithread, Hipp wrote:
- Make sure you’re compiling SQLite with -DTHREADSAFE=1.
- Make sure that each thread opens the database file and keeps its own sqlite structure.
- Make sure you handle the likely possibility that one or more threads collide when they access the db file at the same time: handle SQLITE_BUSY appropriately.
- Make sure you enclose within transactions the commands that modify the database file, like INSERT, UPDATE, DELETE, and others.
In addition, when multithreading while using temporary tables, Hipp noted, “The main database is not locked, so, for instance, one thread can do read operations on the temporary table at the same time as another thread is doing write operations on a table in the main database. This feature can often be used to great advantage when having multithreaded access to the database. By creating a temporary table containing the results of a large query for processing, rather than processing it directly out of the main database, you greatly reduce lock contentions.”
When the whole backend was re-written for SQLite version 3.3.0 (released 11 January 2006), many hoped the multithreading problems were behind them. They were wrong.
Exactly one month later, the author issued SQLite version 3.3.4, along with a terse, but telling statement: “This release fixes several bugs, including a blunder that might cause a deadlock on multithreaded systems. Anyone using SQLite in a multithreaded environment should probably upgrade.”
So what are you waiting for? Do what the man says.
fyi: There are versions of SQLite version 3 for most operating systems, including BSD, Linux, OSX, Windows, etc. In addition, wrappers are available to interface with Java, Perl, PHP, Python, Ruby on Rails and many other languages. If you prefer working from the command prompt, you can do that, too. Right out of the box… But of course, there is no box.

8 comments
Comments feed for this article
February 13th, 2006 at 7:25 pm
bog
This is a little bit off-topic, but helped me. SQLite Tutorial by Mike Chirico: This article explores the power and simplicity of sqlite3, starting with common commands and triggers. It then covers the attach statement with the union operation, introduced in a way that allows multiple tables, in separate databases, to be combined as one virtual table, without the overhead of copying or moving data. Next, I demonstrate the simple sign function and the amazingly powerful trick of using this function in SQL select statements to solve complex queries with a single pass through the data, after making a brief mathematical case for how the sign function defines the absolute value and IF conditions.
http://freshmeat.net/articles/view/1428/
February 15th, 2006 at 10:00 am
Roberto Ciampi
Thanks for SQLite tip. I am reviewing MSQL and MySQL moreover. SQLite has very more favorable licensing.
February 15th, 2006 at 10:26 am
Frank Wilson
Hi,
I installed MySQL in place of SQLite awhile ago, hoping that my server would run a bit quicker. But it created actually more problems, like whenever a new item was requested there was a 10 second pause or so. So I tried going back to SQLite, and now… all seems a lot faster. Any ideas how SQLite got faster then MySQL?
February 15th, 2006 at 7:28 pm
Rudy
The author puts this software out there for free, fixes and updates it, and then makes a point to let everybody know he messed up and to be sure to update. Wouldn’t it be great if the purveyors of pay-to-play software vendors were as conscientious?
February 15th, 2006 at 10:52 pm
BJ Gillette
I’ve never seen a word from Hipp over selling SQLite. He sticks to standard SQL and tries to avoid overreach, which makes documentation and support one heckuva lot easier. It has (almost) always performed exactly as represented. You gotta love him, don’t you?
February 15th, 2006 at 11:21 pm
C Thom
@Frank Wilson: i bet u rebuilt sql tables + queries btwn installs and did a better job the 2nd time. mysql will run better next time too cause you’re smarter.
March 28th, 2006 at 9:34 am
stephan beal
FYI: the SpiderApe project (http://spiderape.sourceforge.net) has a basic JavaScript binding for SQLite3.
November 17th, 2009 at 12:06 am
Jeevitha
Hi whether two applications can access the same sqlite database at the same time and if so , what will happen if one application does an update inside an transaction and doesnt commit it and the other application tries to select the same data..?