Explicit locking mechanism in SQLite

I did not find explicit sqlite locking commands before inserting or updating rows in a table. Does sqlite use a locking mechanism on its own? The pager module described in http://sqlite.org/lockingv3.html handles the locking mechanism. But I'm not sure if there are any commands that the user can use to explicitly lock tables. Please advice.

thanks

+6
source share
2 answers

As far as I know, there are no special sqlite commands to control locking. However, you can force sqlite to lock the database using create transaction . For instance:

BEGIN IMMEDIATE TRANSACTION; ... COMMIT TRANSACTION; BEGIN EXCLUSIVE TRANSACTION; ... COMMIT TRANSACTION; 

If you read a document related to a document, you should better understand the difference between IMMEDIATE and EXCLUSIVE transactions.

It might be worth noting that sqlite locks apply to the entire database, and not just to individual tables, unlike the LOCK TABLE statement in other SQL databases.

+13
source

SQLite makes any lock necessary to implement the transaction scheme that your SQL statements describe. In particular, if you do not describe, then you get an automatic execution of the behavior with a lock held for each statement, and then discarded at the end of the instruction. If you need longer transactions (often true!), You request them explicitly with BEGIN TRANSACTION (often abbreviated to BEGIN ) and end with COMMIT TRANSACTION (or ROLLBACK TRANSACTION ). Transaction processing is often completed for you by your language interface (since this greatly simplifies the transition by associating the transaction lifetime with a code block or method call), but at the basic level it comes down to BEGIN / COMMIT / ROLLBACK .

In short, you have transactions. Locks are used for transactions. You do not have raw castles (this is good, they are much more difficult to achieve than you might think at first glance).

+8
source

Source: https://habr.com/ru/post/917661/


All Articles