views:

44

answers:

1

Folks I am implementing a file based queue (see my earlier question) using sqlite. I have the following threads running in background:

  1. thread-1 to empty out a memory structure into the "queue" table (an insert into "queue" table).
  2. thread-1 to read and "process" the "queue" table runs every 5 to 10 seconds
  3. thread-3 - runs very infrequently and purges old data that is no longer needed from the "queue" table and also runs vacuum so the size of the database file remains small.

Now the behavior that I would like is for each thread to get whatever lock it needs (waiting with a timeout if possible) and then completing the transaction. It is ok if threads do not run concurrently - what is important is that the transaction once begins does not fail due to "locking" errors such as "database is locked".

I looked at the transaction documentation but there does not seem to be a "timeout" facility (I am using JDBC). Can the timeout be set to a large quantity in the connection?

One solution (untried) I can think of is to have a connection pool of max 1 connection. Thus only one thread can connect at a time and so we should not see any locking errors. Are there better ways?

Thanx!

A: 

If it were me, I'd use a single database connection handle. If a thread needs it, it can allocate it within a critical section (or mutex, or similar) - this is basically a poor man's connection pool with only one connection in the pool:) It can do its business with the databse. When done, it exits the critical section (or frees the mutex or ?). You won't get locking errors if you carefully use the single db connection.

-Don

Don Dickinson
Thanx Don, that is what I might end up doing though setting it via a db pool would be more "standard" way of doing it (and less confusing since you are treating the sqlite like other databases in the code.
serverman