Recent projects related to SQLite concurrent read and write problems, referring to some documents and combined with their own practice, the sqlite3 concurrency problems summed up a few points:
sqlite3 Locks and transaction typesSqlite3 a total of three transaction types: BEGIN [deferred/immediate/exclusive] transcation, five kinds of locks, press the level of the lock in turn: unlocked/shared/reserverd/pending /exclusive. When performing a select-as-read operation, you need to obtain a shared lock (share lock), and when performing a insert/update/delete operation (that is, a memory write operation), you need to obtain a further reserverd lock (reserved lock) when a commit operation ( In the case of a disk write operation), further acquisition of the exclusive lock (exclusive lock) is required. For reserverd locks, Sqlite3 guarantees that only one connection at a time can acquire a reserved lock, that is, only one connection can write the database (memory) at the same time, but other connections can still acquire a shared lock. That is, other connections can still be read (it can be assumed that a write operation modifies only one copy of the disk's data and does not affect the read operation). For the exclusive lock, is more stringent than the lock lock, the need to write the changes to the disk is a commit need to retain the lock/pending lock on the basis of a further acquisition of an exclusive lock, as the name implies, exclusive lock to repel any other type of lock, even if the shared lock is not, so, When a commit is made to a connection, the other connection is not able to do anything (including reading). A pending lock (that is, a pending lock) is a special kind of lock that allows a transaction that has been acquired to a shared lock to continue, but does not allow another connection to acquire a shared lock when the existing shared lock is freed (the transaction execution completes). A transaction that holds a pending lock can be a chance for a commit. Sqlite3 Use this lock to prevent writer starvation (write starved).
the deadlock situationDeadlock situation: When two connections start a transaction using BEGIN TRANSACTION, the first connection performs a select operation (acquired to a shared lock), the second connection performs an insert operation (the Reserverd lock has been acquired), At this point the first connection needs to be insert/update/delete (need to get to the reserverd lock), the second connection wants to execute a commit (need to get to the exclusive lock), because the second connection has acquired the Reserverd lock, Depending on the reserverd lock, only one connection can be acquired at a time, the first connection acquires the Reserverd lock and the operation must fail, and the second connection is bound to fail because the first connection has acquired the shared lock and the next one wants to obtain the exclusive lock further. Causes the transaction to deadlock.usage principles for transaction typesWhen you explicitly open a transaction with "BEGIN TRANSACTION", the default transaction type is deferred, the state of the lock is unlocked, that is, no lock is acquired, and if there is no other connection in the database in use, begin with. If you have multiple connections that need to write to the database, you have to start the transaction using begin immediate/exclusive. The benefits of using transactions are: 1. All operations of a transaction are equivalent to one atomic operation, and if one of the steps fails, all previous operations can be undone by rolling back, and the commit is guaranteed only when all operations are successful, and the atomic characteristics of the operation are ensured; 2. For multiple database operations, if we want to increase the speed of data query , you can explicitly open a transaction before you start the operation, and then commit all of the modifications or end transactions by one commit after all operations have been performed.The treatment of Sqlite_busyWhen there are multiple connections that write to the database at the same time, based on the principle of the transaction type, we start the transaction with begin immediate in each connection, that is, when multiple connections try to acquire a reserved lock, and the other connection will get failed, depending on the attribute that only one connection can acquire at the same moment in the retention lock , that is, the transaction begins to fail, in which case the sqlite3 will return a sqlite_busy error, and if we do not want the operation to return on this failure, we have to deal with the sqlite_busy situation, Sqlite3 provides sqlite3_busy_ Handler or sqlite3_busy_timeout to handle sqlite_busy, for Sqlite3_busy_handler, we can specify a busy_handler to process, and you can specify the number of failed retries. The sqlite3_busy_timeout is automatically performed by Sqlite3 and retries, and when sleep accumulates longer than the specified time-out, the sqlite_busy is eventually returned. It is important to note that both functions can use only one, and subsequent calls overwrite the previous call. In terms of usage, sqlite3_busy_timeout is easier to use, just to specify a total timeout, and SQLite will decide how often to retry and retry, until the total timeout is reached and eventually returns SQLITE_BUSY. And, once the two functions are called, it is very convenient for all subsequent database operations to be effective. Reference: http://www.sqlite.org/lockingv3.htmlhttp://www.cnblogs.com/hustcat/archive/2009/03/01/1400757.html
Sqlite3 concurrency reading and writing considerations