英文:
Does boltdb support concurrent queries for reading and updating the db?
问题
目前在存储桶中使用boltdb
来存储各种条目。
如何在读取和更新数据库时使用goroutines和channels?
英文:
Currently using boltdb
for storing various entries in the bucket.
How can I use goroutines and channels in reading and updating in the db?
答案1
得分: 10
一般来说,是可以的,只要你注意以下几点:
-
所有的访问应该在它们自己的事务中进行。事务不应该在 goroutine 之间共享(无论是只读还是读写)。
-
boltdb 在同一时间只允许一个写入操作。如果多个并发事务尝试同时写入,它们将被串行化。数据库的一致性是有保证的,但这会对性能产生影响,因为写操作无法并行化。
-
只有只读事务可以并发执行(并且可能并行化)。
-
在同一 goroutine 中同时只打开一个事务,以避免死锁情况的发生。
英文:
Generally, yes you can, provided you pay attention to the following points:
-
all accesses should be done in their own transactions. Transactions should not be shared between goroutines (whether they are read-only or read-write).
-
boltdb only tolerates one writer at a given point in time. If multiple concurrent transactions try to write at the same time, they will be serialized. The consistency of the database is guaranteed, but it has an impact on the performance, since write operations cannot be parallelized.
-
read-only transactions are executed concurrently (and potentially parallelized).
-
open only one transaction in a given goroutine at the same time to avoid deadlock situations.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论