英文:
Will SQLite database be locked when some suspend function to operate it in Android?
问题
I use SQLite with Room in my Android Studio project.
我在我的Android Studio项目中使用SQLite与Room。
There are some suspend functions to operate a SQLite database, some suspend functions in background service to modify records.
有一些暂停函数来操作SQLite数据库,还有一些在后台服务中用于修改记录的暂停函数。
Will the SQLite database maybe locked?
SQLite数据库可能会被锁定吗?
英文:
I use SQLite with Room in my Android Studio project.
There are some suspend functions to operate a SQLite database, some suspend functions in background service to modify records.
Will the SQLite database maybe locked?
答案1
得分: 1
Will the SQLite database maybe locked?
SQLite数据库会被锁定吗?
Yes.
是的。
Typically Room will use WAL (Write-ahead logging) rather than Journal Mode. i.e. by default API 16+ will utilise WAL and under API 16 will utilise journal mode.
通常情况下,Room会使用WAL(写前日志)而不是日志模式。即默认情况下,API 16+会使用WAL,而API 16以下会使用日志模式。
- With WAL changes are made to a separate file (database file name suffixed with -wal). At times the changes will be applied to the actual database file (checkpointed). Rollback is basically removal of the data from the -wal file.
- 使用WAL时,更改会写入一个单独的文件(数据库文件名后缀为-wal)。有时,更改将应用于实际数据库文件(经过检查点)。回滚基本上是从-wal文件中删除数据。
- With Journal made, changes are applied to the database file and a record (the journal) is kept. Rollback is the process of undoing the changes made to the database file.
- 使用日志模式时,更改会应用于数据库文件,并保留记录(日志)。回滚是撤销对数据库文件所做更改的过程。
With WAL mode readers and writers do not typically lock each other. To quote the SQLite documentation
使用WAL模式时,读者和写者通常不会相互锁定。引用SQLite文档中的话:
> WAL provides more concurrency as readers do not block writers and a writer does not block readers. Reading and writing can proceed concurrently.
WAL提供更多的并发性,因为读者不会阻塞写者,而写者也不会阻塞读者。读和写可以同时进行。
Of course implicit with the above writers(LOCK) block writers. Explained later in the documentation by:-
当然,上述写入者(LOCK)会阻塞写入者。在文档中后面有解释:
> Writers merely append new content to the end of the WAL file. Because writers do nothing that would interfere with the actions of readers, writers and readers can run at the same time. However, since there is only one WAL file, there can only be one writer at a time.
写入者仅仅将新内容追加到WAL文件的末尾。由于写者不会执行会干扰读者操作的任何操作,因此写者和读者可以同时运行。然而,由于只有一个WAL文件,所以一次只能有一个写入者。
Implicitly, and as would be expected, readers do not block readers.
隐式地,如预期的那样,读者不会阻塞读者。
So if you have multiple asynchronous suspended functions that are updating the database, then a locked database may be encountered. Perhaps refer to Composing Suspending Functions
因此,如果您有多个更新数据库的异步挂起函数,那么可能会遇到锁定的数据库。也许可以参考组合挂起函数。
If you are using Journal Mode, see setJournalMode, then concurrency (locking) is more restrictive see File Locking and Concurrency
如果您使用的是日志模式,请参阅setJournalMode,那么并发性(锁定)更加受限,请参阅文件锁定和并发性。
英文:
> Will the SQLite database maybe locked?
Yes.
Typically Room will use WAL (Write-ahead logging) rather than Journal Mode. i.e. by default API 16+ will utilise WAL and under API 16 will utilise journal mode.
- With WAL changes are made to a separate file (database file name suffixed with -wal). At times the changes will be applied to the actual database file (checkpointed). Rollback is basically removal of the data from the -wal file.
- With Journal made, changes are applied to the database file and a record (the journal) is kept. Rollback is the process of undoing the changes made to the database file.
With WAL mode readers and writers do not typically lock each other. To quote the SQLite documentation
> WAL provides more concurrency as readers do not block writers and a writer does not block readers. Reading and writing can proceed concurrently.
Of course implicit with the above writers(LOCK) block writers. Explained later in the documentation by:-
> Writers merely append new content to the end of the WAL file. Because writers do nothing that would interfere with the actions of readers, writers and readers can run at the same time. However, since there is only one WAL file, there can only be one writer at a time.
Implicitly, and as would be expected, readers do not block readers.
So if you have multiple asynchronous suspended functions that are updating the database, then a locked database may be encountered. Perhaps refer to Composing Suspending Functions
If you are using Journal Mode, see setJournalMode, then concurrency (locking) is more restrictive see File Locking and Concurrency
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论