Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sqlite database connection/locking question

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!

like image 815
serverman Avatar asked Jul 09 '10 19:07

serverman


1 Answers

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

like image 173
Don Dickinson Avatar answered Oct 17 '22 21:10

Don Dickinson