On 23 Sep 2015, at 5:01pm, Michael Schlenker <msc at contact.de> wrote:

> i just wondered if there is an API to detect if a sqlite database file
> is already opened by another process.

Thanks for your use-case and assumptions which makes this far simpler to answer.

I don't know of any simple way to know if another process has a connection to 
the process.  Having a connection doesn't modify the file in any way.  And in 
WAL mode you cannot check the journal file to see if a connection exists.

You can tell if the database file is locked for changes.  Make a connection and 
execute "BEGIN EXCLUSIVE".  If this fails something else has the database 
locked in such a way that you should not be modifying it.  If it succeeds it's 
okay to make changes and you should do "END" when you're finished.

You can misuse this to make your server the ultimate database hog by having it 
keep the database locked.  As soon as the server opens the database it should 
execute "BEGIN EXCLUSIVE".  When it wants to flush a set of changes to disk it 
should do "END" and then immediately another "BEGIN EXCLUSIVE".  And just 
before it closes the connection it should do another "END".

This database-hogging is extremely unusual use of SQLite and people may hate 
you for it.  But it might be a usable solution to your particular problem.

Hmm.  I wonder if this would actually work in WAL mode.  Would other processes 
just work on their own private copy of the database until the server does "END" 
?

Simon.

Reply via email to