Manually start, end, commit, or roll back a transaction
BEGIN [ DEFERRED | IMMEDIATE | EXCLUSIVE ] [TRANSACTION [name]] END [TRANSACTION [name]] COMMIT [TRANSACTION [name]] ROLLBACK [TRANSACTION [name]]
The TRANSACTION keyword is used in SQL commands to indicate transactions. QDB supports transactions with rollback and atomic commit. The optional transaction name is ignored. QDB currently doesn't allow nested transactions.
Any changes to the database must be made within a transaction. Any command that changes the database (basically, any SQL command other than SELECT) will automatically start a transaction if one is not already in effect. Automatically started transactions are committed at the conclusion of the command.
Transactions can be started manually using the BEGIN command. Such transactions usually persist until the next COMMIT or ROLLBACK command. But a transaction will also execute ROLLBACK if the database is closed or if an error occurs and a ROLLBACK conflict-resolution algorithm is specified. See the documentation on the ON CONFLICT clause for more information about the ROLLBACK conflict-resolution algorithm.
In QDB, transactions can be deferred, immediate, or exclusive. Deferred means that no locks are acquired on the database until it is first accessed. Thus, with a deferred transaction, the BEGIN statement doesn't lock anything—locks are not acquired until the first read or write operation. The first read operation creates a SHARED lock and the first write operation creates a RESERVED lock. Because the acquisition of locks is deferred until they are needed, it is possible that another thread or process could create a separate transaction and write to the database after the BEGIN on the current thread has executed.
With an immediate transaction, RESERVED locks are acquired on all databases as soon as the BEGIN statement is executed, without waiting for the database to be used. After a BEGIN IMMEDIATE statement, you are guaranteed that no other thread or process will be able to write to the database or do a BEGIN IMMEDIATE or BEGIN EXCLUSIVE. Other processes can continue reading from the database, however.
An exclusive transaction causes EXCLUSIVE locks to be acquired on all databases. After a BEGIN EXCLUSIVE statement, you are guaranteed that no other thread or process will be able to read from or write to the database until the transaction is complete.
The COMMIT command doesn't actually perform a commit until all pending SQL commands finish. Thus, if two or more SELECT statements are actively processing and a COMMIT is executed, the commit will not actually occur until all the SELECT statements finish.
An attempt to execute COMMIT might result in an SQLITE_BUSY return code. This indicates that another thread or process has a read lock on the database that prevented the database from being updated. When COMMIT fails in this way, the transaction remains active and the COMMIT can be retried later after the reader has had a chance to clear.