BoltDB支持并发查询以及对数据库的读取和更新吗?

huangapple go评论89阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2015年6月5日 16:25:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/30661777.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定