On Thu, Jun 30, 2011 at 8:59 AM, Simon Slavin <slav...@bigfraud.org> wrote:

>
> On 30 Jun 2011, at 1:48pm, Cecil Westerhof wrote:
>
> > I am writing a desktop application in which I want to have exclusive
> rights.
> > In this way I do not need to check if the data has changed when the user
> of
> > my program wants to change records. Is this possible?
>
> You can open a transaction as
>
> BEGIN EXCLUSIVE
>
> <http://www.sqlite.org/lang_transaction.html>
>
> "After a BEGIN EXCLUSIVE, no other database connection except for
> read_uncommitted connections will be able to read the database and no other
> connection without exception will be able to write the database until the
> transaction is complete."
>
> This is how you do that sort of thing.  So what you do is …
>
> 1) BEGIN EXCLUSIVE
> 2) Check the result code from step 1 and make sure you got the lock.
> 3) Do SELECTs to check the conditions for your data change and decide what
> to do.
> 4) If the results of step suggest changes, make them.
> 5) END
>
> It is extremely common to see programs where step 3 of the above is done
> before step 1.  Obviously, from your question, you understand this.
>
> Note to SQLite experts: it's not clear to me at what level the exclusive
> lock is maintained on the file.  If an app crashes in while an EXCLUSIVE
> lock is held, is the lock released ?  Does this vary by platform ?
>

The BEGIN EXCLUSIVE...COMMIT technique shown above only gives you exclusive
access in rollback mode.  If you select WAL mode, then BEGIN EXCLUSIVE makes
you the exclusive writer, but other processes can continue to read.  To get
exclusive access in WAL mode, use PRAGMA locking_mode=EXCLUSIVE.

   http://www.sqlite.org/pragma.html#pragma_locking_mode

SQLite usually employs robust locks - meaning that if the application
crashes, the locks are automatically released.  Exceptions to this rule
occur when you select an alternative VFS such as "unix-dotfile".  The
"unix-dotfile" VFS uses classic dot-file locks, which can go stale on you
and require manual intervention to unlock.  But as long as you avoid exotic
VFSes, the locks will be released automatically following a application
crash or other abnormal termination.



>
> Simon.
> _______________________________________________
> sqlite-users mailing list
> sqlite-users@sqlite.org
> http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
>



-- 
D. Richard Hipp
d...@sqlite.org
_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to