Has a Badger Lock file?

(vladimir) #1

When I use the Badger database and make a query, I close the database at the end of all the code.

defer db.Close() //golang code

If the program fails before the end, the database loses all records.:weary: How can this be avoided if I need to make requests several times, and in the end write the result to the database?
The Dgraph has a LOCK file for this situation.

(James) #2

normally you need to commit your transaction, i think close is not needed.
Sorry im not sure if this applies to badger

(vladimir) #3

Golang code:

package main

import (

func main () {
	//Badger -----------
	opts := badger.DefaultOptions
	opts.Dir = "badger"
	opts.ValueDir = "badger"
	db, err := badger.Open(opts)
	if err != nil {
	defer db.Close()

	err = db.Update(func(txn *badger.Txn) error {
		err := txn.Set([]byte("name"), []byte("John"))
		return err
	var str string
	err = db.View(func(txn *badger.Txn) error {
		item, err := txn.Get([]byte("name"))
		if err != nil {
			return err
		val, err := item.ValueCopy(nil)
		if err != nil {
			return err
		str = string(val)
		return nil


I add time.Sleep to code.
If I interrupt the execution of the program, I get lost data and an error accessing the database.
Value log truncate required to run DB. This might result in data loss.