1. SQLite Server

I'm thinking of setting up an SQLite database and have a question about concurrency. I want to make sure I deal with clashes properly so the database doesn't get broken.

I'm setting it up like this:

  1. Apache receives the request (e.g., "https://mywebsite.com/api_call.ex?param1=3&param2=Bill&param3=form_001")
  2. api_call.ex is run and needs to access the SQLite database. What happens here if the database is unavailable? Can I enable some kind of "wait state" so that it waits until the DB is available before finishing? Can I specify a time-out length?


Will it work this way? Do I need to write a server instead, where each instance of api_call.ex calls a server app that manages database hits? It seems in the setup described above, Apache is acting as the server, so this should work, so long as I can wait for the database to be available.

new topic     » topic index » view message » categorize

2. Re: SQLite Server

Good news! SQLite v3 handles all of this for you. Just carry on doing your thing and the library will automatically lock/wait/unlock between processes as necessary. It's like magic!

You will probably experience delays if you're trying to write from multiple processes at the same time. Multiple processes reading from the same database should be effectively delay-free.

See also:

Edit: Sorry, I guess this doesn't happen automatically. You'll have to call sqlite3_busy_timeout() as indicated here: Set A Busy Timeout. Then, as long as your writes stay under the timeout, the library will automatically wait for a slot to open and write the database.

-Greg

new topic     » goto parent     » topic index » view message » categorize

3. Re: SQLite Server

Hi

When I was using sqlite for my veterinary database it ran for multiple users, but I had to write a 'only one person can write a time' lock.

Sqlite can handle multiple reads on the database as the same time (although there would have to be high volume / long queries for this to happen), but it can only handle one write at a time, so, as I said, the way I handled this was to create a lock file when someone wanted to write, write the data, and delete the lock file. If the lock file was present, then the program had to wait until it had been deleted by the originator. There were one or two occasions where I had to deal with the lock file (glitches / power downs) but these were rare and easily surmountable.

There is an sqlite server out there somewhere (not sure if it's free or paid for) which handles these sort of things.

and this is why I wrote my own write locking system

https://sqlite.org/faq.html#q5

Cheers

Chris

new topic     » goto parent     » topic index » view message » categorize

4. Re: SQLite Server

ChrisB said...

The way I handled this was to create a lock file when someone wanted to write, write the data, and delete the lock file.

Thanks for that info, Chris! That's what I'm going to do... smile

new topic     » goto parent     » topic index » view message » categorize

5. Re: SQLite Server

ghaberek said...

Good news! SQLite v3 handles all of this for you. Just carry on doing your thing and the library will automatically lock/wait/unlock between processes as necessary. It's like magic!

You will probably experience delays if you're trying to write from multiple processes at the same time. Multiple processes reading from the same database should be effectively delay-free.

Thanks, Greg! I'm going to have multiple processes possibly trying to even write to the DB, so I'm going to have to write some coordinating process. Chris posted a simple "locked if file exists" algorithm I'm going to try.

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu