1 回答

TA貢獻1827條經驗 獲得超8個贊
根據您的回購協議中的評論,問題似乎出在這里:
tx, err := db.Begin(true)
if err != nil {
return fmt.Errorf("bolt: failed to start transaction")
}
bkt := tx.Bucket([]byte(bkt))
c := bkt.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
// do stuff with bucket...
fmt.Println(v) // check if v matches condition, delete if does
if err := tx.Commit(); err != nil { // BUG: commiting transaction in a loop
tx.Rollback()
return fmt.Errorf("bolt: failed to commit transaction: %w", err)
}
timeout = time.After(time.Second * 5)
}
循環可以迭代 0 次。
如果沒有迭代 -
tx
不提交timeout
也不重置(因此case <-timeout:
不會再次觸發)。如果有多次迭代 - 您將嘗試
tx.Commit()
多次(錯誤)。
這可能導致了您看到的問題;bolt
Close
功能:_
關閉釋放所有數據庫資源。在關閉數據庫之前必須關閉所有事務。
因此,如果有一個事務運行Close
塊直到完成(內部螺栓在事務開始時鎖定互斥鎖并在完成時釋放它)。
解決方案是確保事務始終關閉(并且只關閉一次)。
- 1 回答
- 0 關注
- 102 瀏覽
添加回答
舉報